bartko-s /
stefano-tree
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace StefanoTreeTest\Integration\Adapter; |
||
| 6 | |||
| 7 | use StefanoTree\NestedSet\Adapter\AdapterInterface; |
||
| 8 | use StefanoTreeTest\IntegrationTestCase; |
||
| 9 | |||
| 10 | abstract class AdapterAbstract extends IntegrationTestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var AdapterInterface |
||
| 14 | */ |
||
| 15 | protected $adapter; |
||
| 16 | |||
| 17 | protected $adapterCanQuoteIdentifier = true; |
||
| 18 | |||
| 19 | protected function setUp(): void |
||
| 20 | { |
||
| 21 | $this->adapter = null; |
||
| 22 | parent::setUp(); |
||
| 23 | } |
||
| 24 | |||
| 25 | protected function tearDown(): void |
||
| 26 | { |
||
| 27 | $this->adapter = null; |
||
| 28 | parent::tearDown(); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return AdapterInterface |
||
| 33 | */ |
||
| 34 | abstract protected function getAdapter(): AdapterInterface; |
||
| 35 | |||
| 36 | protected function getDataSet() |
||
| 37 | { |
||
| 38 | return $this->createArrayDataSet(include __DIR__.'/../_files/NestedSet/initDataSet.php'); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testIsInTransaction() |
||
| 42 | { |
||
| 43 | $adapter = $this->getAdapter(); |
||
| 44 | |||
| 45 | $this->assertFalse($adapter->isInTransaction()); |
||
| 46 | $adapter->beginTransaction(); |
||
| 47 | $this->assertTrue($adapter->isInTransaction()); |
||
| 48 | $adapter->rollbackTransaction(); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function testCommitTransaction() |
||
| 52 | { |
||
| 53 | $adapter = $this->getAdapter(); |
||
| 54 | |||
| 55 | $this->assertFalse($adapter->isInTransaction()); |
||
| 56 | $adapter->beginTransaction(); |
||
| 57 | $this->assertTrue($adapter->isInTransaction()); |
||
| 58 | $adapter->commitTransaction(); |
||
| 59 | $this->assertFalse($adapter->isInTransaction()); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testRollbackTransaction() |
||
| 63 | { |
||
| 64 | $adapter = $this->getAdapter(); |
||
| 65 | |||
| 66 | $this->assertFalse($adapter->isInTransaction()); |
||
| 67 | $adapter->beginTransaction(); |
||
| 68 | $this->assertTrue($adapter->isInTransaction()); |
||
| 69 | $adapter->rollbackTransaction(); |
||
| 70 | $this->assertFalse($adapter->isInTransaction()); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testHandleNestedTransaction() |
||
| 74 | { |
||
| 75 | $adapter = $this->getAdapter(); |
||
| 76 | if (!$adapter->canHandleNestedTransaction()) { |
||
| 77 | $this->markTestSkipped('Adapter does not support nested transaction'); |
||
| 78 | |||
| 79 | return; |
||
| 80 | } |
||
| 81 | $adapter->beginTransaction(); |
||
| 82 | $this->assertTrue($adapter->isInTransaction()); |
||
| 83 | $adapter->beginTransaction(); |
||
| 84 | $adapter->commitTransaction(); |
||
| 85 | $this->assertTrue($adapter->isInTransaction()); |
||
| 86 | $adapter->commitTransaction(); |
||
| 87 | $this->assertFalse($adapter->isInTransaction()); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testQuoteIdentifier() |
||
| 91 | { |
||
| 92 | $a = $this->getAdapter(); |
||
| 93 | |||
| 94 | if ($this->adapterCanQuoteIdentifier) { |
||
| 95 | if ('pgsql' == TEST_STEFANO_DB_VENDOR) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 96 | $this->assertEquals( |
||
| 97 | '"simple"', |
||
| 98 | $a->quoteIdentifier('simple') |
||
| 99 | ); |
||
| 100 | |||
| 101 | $this->assertEquals( |
||
| 102 | '"more"."complex"', |
||
| 103 | $a->quoteIdentifier('more.complex') |
||
| 104 | ); |
||
| 105 | } else { |
||
| 106 | $this->assertEquals( |
||
| 107 | '`simple`', |
||
| 108 | $a->quoteIdentifier('simple') |
||
| 109 | ); |
||
| 110 | |||
| 111 | $this->assertEquals( |
||
| 112 | '`more`.`complex`', |
||
| 113 | $a->quoteIdentifier('more.complex') |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | $this->assertEquals( |
||
| 118 | 'more.complex', |
||
| 119 | $a->quoteIdentifier('more.complex') |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | public function testExecuteInsertSQL() |
||
| 125 | { |
||
| 126 | $a = $this->getAdapter(); |
||
| 127 | |||
| 128 | $sql = 'INSERT INTO tree_traversal (name, lft, rgt, parent_id, level)' |
||
| 129 | .' VALUES(:name, :lft, :rgt, :parent_id, :level)'; |
||
| 130 | |||
| 131 | $data = array( |
||
| 132 | 'name' => 'ahoj', |
||
| 133 | 'lft' => 123, |
||
| 134 | 'rgt' => 456, |
||
| 135 | 'parent_id' => 10, |
||
| 136 | 'level' => 789, |
||
| 137 | ); |
||
| 138 | |||
| 139 | $id = $a->executeInsertSQL($sql, $data); |
||
| 140 | $this->assertEquals(26, $id); |
||
| 141 | |||
| 142 | $result = $a->executeSelectSQL('SELECT * FROM tree_traversal WHERE tree_traversal_id = 26'); |
||
| 143 | $this->assertEquals( |
||
| 144 | array( |
||
| 145 | array_merge($data, array('tree_traversal_id' => 26)), |
||
| 146 | ), |
||
| 147 | $result |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testExecuteSQL() |
||
| 152 | { |
||
| 153 | $a = $this->getAdapter(); |
||
| 154 | |||
| 155 | $sql = 'UPDATE tree_traversal SET name = :name WHERE tree_traversal_id = :id'; |
||
| 156 | |||
| 157 | $data = array( |
||
| 158 | 'name' => 'testujeme parne valce', |
||
| 159 | 'id' => 10, |
||
| 160 | ); |
||
| 161 | |||
| 162 | $a->executeSQL($sql, $data); |
||
| 163 | |||
| 164 | $result = $a->executeSelectSQL( |
||
| 165 | 'SELECT tree_traversal_id AS id, name FROM tree_traversal WHERE tree_traversal_id = :id', |
||
| 166 | array('id' => $data['id']) |
||
| 167 | ); |
||
| 168 | |||
| 169 | $this->assertEquals( |
||
| 170 | array( |
||
| 171 | $data, |
||
| 172 | ), |
||
| 173 | $result |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 |