Remove product literal strings in "pht()", part 18
[phabricator.git] / src / infrastructure / storage / lisk / __tests__ / LiskFixtureTestCase.php
blob92c43fcd4b931ac40b9d231ee96fd8fd7c40249f
1 <?php
3 final class LiskFixtureTestCase extends PhabricatorTestCase {
5 protected function getPhabricatorTestCaseConfiguration() {
6 return array(
7 self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
8 );
11 public function testTransactionalIsolation1of2() {
12 // NOTE: These tests are verifying that data is destroyed between tests.
13 // If the user from either test persists, the other test will fail.
14 $this->assertEqual(
16 count(id(new HarbormasterScratchTable())->loadAll()));
18 id(new HarbormasterScratchTable())
19 ->setData('alincoln')
20 ->save();
23 public function testTransactionalIsolation2of2() {
24 $this->assertEqual(
26 count(id(new HarbormasterScratchTable())->loadAll()));
28 id(new HarbormasterScratchTable())
29 ->setData('ugrant')
30 ->save();
33 public function testFixturesBasicallyWork() {
34 $this->assertEqual(
36 count(id(new HarbormasterScratchTable())->loadAll()));
38 id(new HarbormasterScratchTable())
39 ->setData('gwashington')
40 ->save();
42 $this->assertEqual(
44 count(id(new HarbormasterScratchTable())->loadAll()));
47 public function testReadableTransactions() {
48 // TODO: When we have semi-durable fixtures, use those instead. This is
49 // extremely hacky.
51 LiskDAO::endIsolateAllLiskEffectsToTransactions();
52 try {
54 $data = Filesystem::readRandomCharacters(32);
56 $obj = new HarbormasterScratchTable();
57 $obj->openTransaction();
59 $obj->setData($data);
60 $obj->save();
62 $loaded = id(new HarbormasterScratchTable())->loadOneWhere(
63 'data = %s',
64 $data);
66 $obj->killTransaction();
68 $this->assertTrue(
69 ($loaded !== null),
70 pht('Reads inside transactions should have transaction visibility.'));
72 LiskDAO::beginIsolateAllLiskEffectsToTransactions();
73 } catch (Exception $ex) {
74 LiskDAO::beginIsolateAllLiskEffectsToTransactions();
75 throw $ex;
79 public function testGarbageLoadCalls() {
80 $obj = new HarbormasterObject();
81 $obj->save();
82 $id = $obj->getID();
84 $load = new HarbormasterObject();
86 $this->assertEqual(null, $load->load(0));
87 $this->assertEqual(null, $load->load(-1));
88 $this->assertEqual(null, $load->load(9999));
89 $this->assertEqual(null, $load->load(''));
90 $this->assertEqual(null, $load->load('cow'));
91 $this->assertEqual(null, $load->load($id.'cow'));
93 $this->assertTrue((bool)$load->load((int)$id));
94 $this->assertTrue((bool)$load->load((string)$id));
97 public function testCounters() {
98 $obj = new HarbormasterObject();
99 $conn_w = $obj->establishConnection('w');
101 // Test that the counter basically behaves as expected.
102 $this->assertEqual(1, LiskDAO::loadNextCounterValue($conn_w, 'a'));
103 $this->assertEqual(2, LiskDAO::loadNextCounterValue($conn_w, 'a'));
104 $this->assertEqual(3, LiskDAO::loadNextCounterValue($conn_w, 'a'));
106 // This first insert is primarily a test that the previous LAST_INSERT_ID()
107 // value does not bleed into the creation of a new counter.
108 $this->assertEqual(1, LiskDAO::loadNextCounterValue($conn_w, 'b'));
109 $this->assertEqual(2, LiskDAO::loadNextCounterValue($conn_w, 'b'));
111 // Test alternate access/overwrite methods.
112 $this->assertEqual(3, LiskDAO::loadCurrentCounterValue($conn_w, 'a'));
114 LiskDAO::overwriteCounterValue($conn_w, 'a', 42);
115 $this->assertEqual(42, LiskDAO::loadCurrentCounterValue($conn_w, 'a'));
116 $this->assertEqual(43, LiskDAO::loadNextCounterValue($conn_w, 'a'));
118 // These inserts alternate database connections. Since unit tests are
119 // transactional by default, we need to break out of them or we'll deadlock
120 // since the transactions don't normally close until we exit the test.
121 LiskDAO::endIsolateAllLiskEffectsToTransactions();
122 try {
124 $conn_1 = $obj->establishConnection('w', $force_new = true);
125 $conn_2 = $obj->establishConnection('w', $force_new = true);
127 $this->assertEqual(1, LiskDAO::loadNextCounterValue($conn_1, 'z'));
128 $this->assertEqual(2, LiskDAO::loadNextCounterValue($conn_2, 'z'));
129 $this->assertEqual(3, LiskDAO::loadNextCounterValue($conn_1, 'z'));
130 $this->assertEqual(4, LiskDAO::loadNextCounterValue($conn_2, 'z'));
131 $this->assertEqual(5, LiskDAO::loadNextCounterValue($conn_1, 'z'));
133 LiskDAO::beginIsolateAllLiskEffectsToTransactions();
134 } catch (Exception $ex) {
135 LiskDAO::beginIsolateAllLiskEffectsToTransactions();
136 throw $ex;
140 public function testNonmutableColumns() {
141 $object = id(new HarbormasterScratchTable())
142 ->setData('val1')
143 ->setNonmutableData('val1')
144 ->save();
146 $object->reload();
148 $this->assertEqual('val1', $object->getData());
149 $this->assertEqual('val1', $object->getNonmutableData());
151 $object
152 ->setData('val2')
153 ->setNonmutableData('val2')
154 ->save();
156 $object->reload();
158 $this->assertEqual('val2', $object->getData());
160 // NOTE: This is the important test: the nonmutable column should not have
161 // been affected by the update.
162 $this->assertEqual('val1', $object->getNonmutableData());