Remove product literal strings in "pht()", part 18
[phabricator.git] / src / infrastructure / storage / __tests__ / AphrontIsolatedDatabaseConnectionTestCase.php
bloba0a64481bd93b99dd6e658dd6d329f324030e32f
1 <?php
3 final class AphrontIsolatedDatabaseConnectionTestCase
4 extends PhabricatorTestCase {
6 protected function getPhabricatorTestCaseConfiguration() {
7 return array(
8 // We disable this here because this test is unique (it is testing that
9 // isolation actually occurs) and must establish a live connection to the
10 // database to verify that.
11 self::PHABRICATOR_TESTCONFIG_ISOLATE_LISK => false,
15 public function testIsolation() {
16 // This will fail if the connection isn't isolated.
17 queryfx(
18 $this->newIsolatedConnection(),
19 'INSERT INVALID SYNTAX');
21 $this->assertTrue(true);
24 public function testInsertGeneratesID() {
25 $conn = $this->newIsolatedConnection();
27 queryfx($conn, 'INSERT');
28 $id1 = $conn->getInsertID();
30 queryfx($conn, 'INSERT');
31 $id2 = $conn->getInsertID();
33 $this->assertTrue((bool)$id1, pht('ID1 exists.'));
34 $this->assertTrue((bool)$id2, pht('ID2 exists.'));
35 $this->assertTrue(
36 $id1 != $id2,
37 pht("IDs '%s' and '%s' are distinct.", $id1, $id2));
40 public function testDeletePermitted() {
41 $conn = $this->newIsolatedConnection();
42 queryfx($conn, 'DELETE');
44 $this->assertTrue(true);
47 public function testTransactionStack() {
48 $conn = $this->newIsolatedConnection();
49 $conn->openTransaction();
50 queryfx($conn, 'INSERT');
51 $conn->saveTransaction();
52 $this->assertEqual(
53 array(
54 'START TRANSACTION',
55 'INSERT',
56 'COMMIT',
58 $conn->getQueryTranscript());
60 $conn = $this->newIsolatedConnection();
61 $conn->openTransaction();
62 queryfx($conn, 'INSERT 1');
63 $conn->openTransaction();
64 queryfx($conn, 'INSERT 2');
65 $conn->killTransaction();
66 $conn->openTransaction();
67 queryfx($conn, 'INSERT 3');
68 $conn->openTransaction();
69 queryfx($conn, 'INSERT 4');
70 $conn->saveTransaction();
71 $conn->saveTransaction();
72 $conn->openTransaction();
73 queryfx($conn, 'INSERT 5');
74 $conn->killTransaction();
75 queryfx($conn, 'INSERT 6');
76 $conn->saveTransaction();
78 $this->assertEqual(
79 array(
80 'START TRANSACTION',
81 'INSERT 1',
82 'SAVEPOINT Aphront_Savepoint_1',
83 'INSERT 2',
84 'ROLLBACK TO SAVEPOINT Aphront_Savepoint_1',
85 'SAVEPOINT Aphront_Savepoint_1',
86 'INSERT 3',
87 'SAVEPOINT Aphront_Savepoint_2',
88 'INSERT 4',
89 'SAVEPOINT Aphront_Savepoint_1',
90 'INSERT 5',
91 'ROLLBACK TO SAVEPOINT Aphront_Savepoint_1',
92 'INSERT 6',
93 'COMMIT',
95 $conn->getQueryTranscript());
98 public function testTransactionRollback() {
99 $check = array();
101 $phid = new HarbormasterScratchTable();
102 $phid->openTransaction();
103 for ($ii = 0; $ii < 3; $ii++) {
104 $key = $this->generateTestData();
106 $obj = new HarbormasterScratchTable();
107 $obj->setData($key);
108 $obj->save();
110 $check[] = $key;
112 $phid->killTransaction();
114 foreach ($check as $key) {
115 $this->assertNoSuchRow($key);
119 private function newIsolatedConnection() {
120 $config = array();
121 return new AphrontIsolatedDatabaseConnection($config);
124 private function generateTestData() {
125 return Filesystem::readRandomCharacters(20);
128 private function assertNoSuchRow($data) {
129 try {
130 $row = id(new HarbormasterScratchTable())->loadOneWhere(
131 'data = %s',
132 $data);
133 $this->assertEqual(
134 null,
135 $row,
136 pht('Expect fake row to exist only in isolation.'));
137 } catch (AphrontConnectionQueryException $ex) {
138 // If we can't connect to the database, conclude that the isolated
139 // connection actually is isolated. Philosophically, this perhaps allows
140 // us to claim this test does not depend on the database?