Commit | Line | Data |
---|---|---|
09e1f1e3 TO |
1 | <?php |
2 | namespace Civi\Test; | |
3 | ||
4 | /** | |
5 | * This is an example of a barebones test which uses a transaction (based on CiviTestListener). | |
6 | * | |
7 | * We check that the transaction works by creating some example records in setUp(). These | |
8 | * records should fetchable while the test executes, but not during tearDownAfterClass(). | |
9 | * | |
10 | * @group headless | |
11 | */ | |
a6439b6a | 12 | class ExampleTransactionalTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface { |
09e1f1e3 TO |
13 | |
14 | /** | |
15 | * @var array | |
16 | * Array(int $id). | |
17 | */ | |
9099cab3 | 18 | protected static $contactIds = []; |
09e1f1e3 TO |
19 | |
20 | public function setUpHeadless() { | |
728bbd5b | 21 | return \Civi\Test::headless()->apply(); |
09e1f1e3 TO |
22 | } |
23 | ||
24 | protected function setUp() { | |
25 | /** @var \CRM_Contact_DAO_Contact $contact */ | |
9099cab3 | 26 | $contact = \CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact', [ |
09e1f1e3 | 27 | 'contact_type' => 'Individual', |
9099cab3 | 28 | ]); |
09e1f1e3 TO |
29 | self::$contactIds[$this->getName()] = $contact->id; |
30 | } | |
31 | ||
32 | /** | |
33 | * In the first test, we make make testDummy1. He exists. | |
34 | */ | |
35 | public function testDummy1() { | |
36 | $this->assertTrue(is_numeric(self::$contactIds['testDummy1']) && self::$contactIds['testDummy1'] > 0); | |
37 | ||
38 | // Still inside transaction. Data exists. | |
39 | $dao = new \CRM_Contact_DAO_Contact(); | |
40 | $dao->id = self::$contactIds['testDummy1']; | |
41 | $this->assertTrue((bool) $dao->find()); | |
42 | } | |
43 | ||
44 | /** | |
45 | * We previously made testDummy1, but he's been lost (rolled-back). | |
46 | * However, testDummy2 now exists. | |
47 | */ | |
48 | public function testDummy2() { | |
49 | $this->assertTrue(is_numeric(self::$contactIds['testDummy1']) && self::$contactIds['testDummy1'] > 0); | |
50 | $this->assertTrue(is_numeric(self::$contactIds['testDummy2']) && self::$contactIds['testDummy2'] > 0); | |
51 | ||
52 | // Previous contact no longer exists | |
53 | $dao = new \CRM_Contact_DAO_Contact(); | |
54 | $dao->id = self::$contactIds['testDummy1']; | |
55 | $this->assertFalse((bool) $dao->find()); | |
56 | ||
57 | // Still inside transaction. Data exists. | |
58 | $dao = new \CRM_Contact_DAO_Contact(); | |
59 | $dao->id = self::$contactIds['testDummy2']; | |
60 | $this->assertTrue((bool) $dao->find()); | |
61 | } | |
62 | ||
63 | public function tearDown() { | |
64 | } | |
65 | ||
66 | /** | |
67 | * Both testDummy1 and testDummy2 have been created at some point (as part of the test runs), | |
68 | * but all the data was rolled-back | |
69 | * | |
70 | * @throws \Exception | |
71 | */ | |
72 | public static function tearDownAfterClass() { | |
73 | if (!is_numeric(self::$contactIds['testDummy1'])) { | |
74 | throw new \Exception("Uh oh! The static \$contactIds does not include testDummy1! Did the test fail to execute?"); | |
75 | } | |
76 | ||
77 | if (!is_numeric(self::$contactIds['testDummy2'])) { | |
78 | throw new \Exception("Uh oh! The static \$contactIds does not include testDummy2! Did the test fail to execute?"); | |
79 | } | |
80 | ||
81 | $dao = new \CRM_Contact_DAO_Contact(); | |
82 | $dao->id = self::$contactIds['testDummy1']; | |
83 | if ($dao->find()) { | |
84 | throw new \Exception("Uh oh! testDummy1 still exists!"); | |
85 | } | |
86 | ||
87 | $dao = new \CRM_Contact_DAO_Contact(); | |
88 | $dao->id = self::$contactIds['testDummy2']; | |
89 | if ($dao->find()) { | |
90 | throw new \Exception("Uh oh! testDummy2 still exists!"); | |
91 | } | |
92 | } | |
93 | ||
94 | } |