|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Lock\Tests\Store;
|
13 | 13 |
|
| 14 | +use Doctrine\DBAL\Connection; |
14 | 15 | use Doctrine\DBAL\DriverManager;
|
| 16 | +use Doctrine\DBAL\Exception\TableNotFoundException; |
| 17 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
15 | 18 | use Symfony\Component\Lock\Key;
|
16 | 19 | use Symfony\Component\Lock\PersistingStoreInterface;
|
17 | 20 | use Symfony\Component\Lock\Store\DoctrineDbalStore;
|
18 | 21 |
|
| 22 | +class_exists(\Doctrine\DBAL\Platforms\PostgreSqlPlatform::class); |
| 23 | + |
19 | 24 | /**
|
20 | 25 | * @author Jérémy Derussé <jeremy@derusse.com>
|
21 | 26 | *
|
@@ -87,4 +92,126 @@ public function provideDsn()
|
87 | 92 | yield ['sqlite3:///'.$dbFile.'3', $dbFile.'3'];
|
88 | 93 | yield ['sqlite://localhost/:memory:'];
|
89 | 94 | }
|
| 95 | + |
| 96 | + /** |
| 97 | + * @dataProvider providePlatforms |
| 98 | + */ |
| 99 | + public function testCreatesTableInTransaction(string $platform) |
| 100 | + { |
| 101 | + $conn = $this->createMock(Connection::class); |
| 102 | + $conn->expects($this->exactly(3)) |
| 103 | + ->method('executeStatement') |
| 104 | + ->withConsecutive( |
| 105 | + [$this->stringContains('INSERT INTO')], |
| 106 | + [$this->matches('create sql stmt')], |
| 107 | + [$this->stringContains('INSERT INTO')] |
| 108 | + ) |
| 109 | + ->will( |
| 110 | + $this->onConsecutiveCalls( |
| 111 | + $this->throwException( |
| 112 | + $this->createMock(TableNotFoundException::class) |
| 113 | + ), |
| 114 | + 1, |
| 115 | + 1 |
| 116 | + ) |
| 117 | + ); |
| 118 | + |
| 119 | + $conn->method('isTransactionActive') |
| 120 | + ->willReturn(true); |
| 121 | + |
| 122 | + $platform = $this->createMock($platform); |
| 123 | + $platform->method('getCreateTableSQL') |
| 124 | + ->willReturn(['create sql stmt']); |
| 125 | + |
| 126 | + $conn->method('getDatabasePlatform') |
| 127 | + ->willReturn($platform); |
| 128 | + |
| 129 | + $store = new DoctrineDbalStore($conn); |
| 130 | + |
| 131 | + $key = new Key(uniqid(__METHOD__, true)); |
| 132 | + |
| 133 | + $store->save($key); |
| 134 | + } |
| 135 | + |
| 136 | + public function providePlatforms() |
| 137 | + { |
| 138 | + yield [\Doctrine\DBAL\Platforms\PostgreSQLPlatform::class]; |
| 139 | + yield [\Doctrine\DBAL\Platforms\PostgreSQL94Platform::class]; |
| 140 | + yield [\Doctrine\DBAL\Platforms\SqlitePlatform::class]; |
| 141 | + yield [\Doctrine\DBAL\Platforms\SQLServerPlatform::class]; |
| 142 | + yield [\Doctrine\DBAL\Platforms\SQLServer2012Platform::class]; |
| 143 | + } |
| 144 | + |
| 145 | + public function testTableCreationInTransactionNotSupported() |
| 146 | + { |
| 147 | + $conn = $this->createMock(Connection::class); |
| 148 | + $conn->expects($this->exactly(2)) |
| 149 | + ->method('executeStatement') |
| 150 | + ->withConsecutive( |
| 151 | + [$this->stringContains('INSERT INTO')], |
| 152 | + [$this->stringContains('INSERT INTO')] |
| 153 | + ) |
| 154 | + ->will( |
| 155 | + $this->onConsecutiveCalls( |
| 156 | + $this->throwException( |
| 157 | + $this->createMock(TableNotFoundException::class) |
| 158 | + ), |
| 159 | + 1, |
| 160 | + 1 |
| 161 | + ) |
| 162 | + ); |
| 163 | + |
| 164 | + $conn->method('isTransactionActive') |
| 165 | + ->willReturn(true); |
| 166 | + |
| 167 | + $platform = $this->createMock(AbstractPlatform::class); |
| 168 | + $platform->method('getCreateTableSQL') |
| 169 | + ->willReturn(['create sql stmt']); |
| 170 | + |
| 171 | + $conn->expects($this->exactly(2)) |
| 172 | + ->method('getDatabasePlatform'); |
| 173 | + |
| 174 | + $store = new DoctrineDbalStore($conn); |
| 175 | + |
| 176 | + $key = new Key(uniqid(__METHOD__, true)); |
| 177 | + |
| 178 | + $store->save($key); |
| 179 | + } |
| 180 | + |
| 181 | + public function testCreatesTableOutsideTransaction() |
| 182 | + { |
| 183 | + $conn = $this->createMock(Connection::class); |
| 184 | + $conn->expects($this->exactly(3)) |
| 185 | + ->method('executeStatement') |
| 186 | + ->withConsecutive( |
| 187 | + [$this->stringContains('INSERT INTO')], |
| 188 | + [$this->matches('create sql stmt')], |
| 189 | + [$this->stringContains('INSERT INTO')] |
| 190 | + ) |
| 191 | + ->will( |
| 192 | + $this->onConsecutiveCalls( |
| 193 | + $this->throwException( |
| 194 | + $this->createMock(TableNotFoundException::class) |
| 195 | + ), |
| 196 | + 1, |
| 197 | + 1 |
| 198 | + ) |
| 199 | + ); |
| 200 | + |
| 201 | + $conn->method('isTransactionActive') |
| 202 | + ->willReturn(false); |
| 203 | + |
| 204 | + $platform = $this->createMock(AbstractPlatform::class); |
| 205 | + $platform->method('getCreateTableSQL') |
| 206 | + ->willReturn(['create sql stmt']); |
| 207 | + |
| 208 | + $conn->method('getDatabasePlatform') |
| 209 | + ->willReturn($platform); |
| 210 | + |
| 211 | + $store = new DoctrineDbalStore($conn); |
| 212 | + |
| 213 | + $key = new Key(uniqid(__METHOD__, true)); |
| 214 | + |
| 215 | + $store->save($key); |
| 216 | + } |
90 | 217 | }
|
0 commit comments