|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\Database\Tests; |
| 6 | + |
| 7 | +use Spiral\Database\Database; |
| 8 | +use Spiral\Database\Driver\Driver; |
| 9 | +use Spiral\Database\Exception\ReadonlyConnectionException; |
| 10 | +use Spiral\Database\Table; |
| 11 | + |
| 12 | +abstract class ReadonlyTest extends BaseTest |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $table = 'readonly_tests'; |
| 18 | + |
| 19 | + public function setUp(): void |
| 20 | + { |
| 21 | + $this->database = new Database('default', '', $this->getDriver(['readonly' => true])); |
| 22 | + |
| 23 | + $this->allowWrite(function () { |
| 24 | + $table = $this->database->table($this->table); |
| 25 | + $schema = $table->getSchema(); |
| 26 | + $schema->primary('id'); |
| 27 | + $schema->string('value')->nullable(); |
| 28 | + $schema->save(); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + private function allowWrite(\Closure $then): void |
| 33 | + { |
| 34 | + /** @var Driver $driver */ |
| 35 | + $driver = $this->database->getDriver(); |
| 36 | + |
| 37 | + (function (\Closure $then): void { |
| 38 | + $this->options['readonly'] = false; |
| 39 | + try { |
| 40 | + $then(); |
| 41 | + } finally { |
| 42 | + $this->options['readonly'] = true; |
| 43 | + } |
| 44 | + })->call($driver, $then); |
| 45 | + } |
| 46 | + |
| 47 | + public function tearDown(): void |
| 48 | + { |
| 49 | + $this->allowWrite(function () { |
| 50 | + $schema = $this->database->table($this->table) |
| 51 | + ->getSchema(); |
| 52 | + |
| 53 | + $schema->declareDropped(); |
| 54 | + $schema->save(); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + protected function table(): Table |
| 59 | + { |
| 60 | + return $this->database->table($this->table); |
| 61 | + } |
| 62 | + |
| 63 | + public function testTableAllowSelection(): void |
| 64 | + { |
| 65 | + $this->expectNotToPerformAssertions(); |
| 66 | + |
| 67 | + $this->table() |
| 68 | + ->select() |
| 69 | + ->run() |
| 70 | + ; |
| 71 | + } |
| 72 | + |
| 73 | + public function testTableAllowCount(): void |
| 74 | + { |
| 75 | + $this->expectNotToPerformAssertions(); |
| 76 | + |
| 77 | + $this->table() |
| 78 | + ->count() |
| 79 | + ; |
| 80 | + } |
| 81 | + |
| 82 | + public function testTableAllowExists(): void |
| 83 | + { |
| 84 | + $this->expectNotToPerformAssertions(); |
| 85 | + |
| 86 | + $this->table() |
| 87 | + ->exists() |
| 88 | + ; |
| 89 | + } |
| 90 | + |
| 91 | + public function testTableAllowGetPrimaryKeys(): void |
| 92 | + { |
| 93 | + $this->expectNotToPerformAssertions(); |
| 94 | + |
| 95 | + $this->table() |
| 96 | + ->getPrimaryKeys() |
| 97 | + ; |
| 98 | + } |
| 99 | + |
| 100 | + public function testTableAllowHasColumn(): void |
| 101 | + { |
| 102 | + $this->expectNotToPerformAssertions(); |
| 103 | + |
| 104 | + $this->table() |
| 105 | + ->hasColumn('column') |
| 106 | + ; |
| 107 | + } |
| 108 | + |
| 109 | + public function testTableAllowGetColumns(): void |
| 110 | + { |
| 111 | + $this->expectNotToPerformAssertions(); |
| 112 | + |
| 113 | + $this->table() |
| 114 | + ->getColumns() |
| 115 | + ; |
| 116 | + } |
| 117 | + |
| 118 | + public function testTableAllowHasIndex(): void |
| 119 | + { |
| 120 | + $this->expectNotToPerformAssertions(); |
| 121 | + |
| 122 | + $this->table() |
| 123 | + ->hasIndex(['column']) |
| 124 | + ; |
| 125 | + } |
| 126 | + |
| 127 | + public function testTableAllowGetIndexes(): void |
| 128 | + { |
| 129 | + $this->expectNotToPerformAssertions(); |
| 130 | + |
| 131 | + $this->table() |
| 132 | + ->getIndexes() |
| 133 | + ; |
| 134 | + } |
| 135 | + |
| 136 | + public function testTableAllowHasForeignKey(): void |
| 137 | + { |
| 138 | + $this->expectNotToPerformAssertions(); |
| 139 | + |
| 140 | + $this->table() |
| 141 | + ->hasForeignKey(['column']) |
| 142 | + ; |
| 143 | + } |
| 144 | + |
| 145 | + public function testTableAllowGetForeignKeys(): void |
| 146 | + { |
| 147 | + $this->expectNotToPerformAssertions(); |
| 148 | + |
| 149 | + $this->table() |
| 150 | + ->getForeignKeys() |
| 151 | + ; |
| 152 | + } |
| 153 | + |
| 154 | + public function testTableAllowGetDependencies(): void |
| 155 | + { |
| 156 | + $this->expectNotToPerformAssertions(); |
| 157 | + |
| 158 | + $this->table() |
| 159 | + ->getDependencies() |
| 160 | + ; |
| 161 | + } |
| 162 | + |
| 163 | + public function testTableRejectInsertOne(): void |
| 164 | + { |
| 165 | + $this->expectException(ReadonlyConnectionException::class); |
| 166 | + |
| 167 | + $this->table() |
| 168 | + ->insertOne(['value' => 'example']) |
| 169 | + ; |
| 170 | + } |
| 171 | + |
| 172 | + public function testTableRejectInsertMultiple(): void |
| 173 | + { |
| 174 | + $this->expectException(ReadonlyConnectionException::class); |
| 175 | + |
| 176 | + $this->table() |
| 177 | + ->insertMultiple(['value'], ['example']) |
| 178 | + ; |
| 179 | + } |
| 180 | + |
| 181 | + public function testTableRejectInsert(): void |
| 182 | + { |
| 183 | + $this->expectException(ReadonlyConnectionException::class); |
| 184 | + |
| 185 | + $this->table() |
| 186 | + ->insert() |
| 187 | + ->columns('value') |
| 188 | + ->values('example') |
| 189 | + ->run(); |
| 190 | + } |
| 191 | + |
| 192 | + public function testTableRejectUpdate(): void |
| 193 | + { |
| 194 | + $this->expectException(ReadonlyConnectionException::class); |
| 195 | + |
| 196 | + $this->table() |
| 197 | + ->update(['value' => 'updated']) |
| 198 | + ->run() |
| 199 | + ; |
| 200 | + } |
| 201 | + |
| 202 | + public function testTableRejectDelete(): void |
| 203 | + { |
| 204 | + $this->expectException(ReadonlyConnectionException::class); |
| 205 | + |
| 206 | + $this->table() |
| 207 | + ->delete() |
| 208 | + ->run() |
| 209 | + ; |
| 210 | + } |
| 211 | + |
| 212 | + public function testTableRejectEraseData(): void |
| 213 | + { |
| 214 | + $this->expectException(ReadonlyConnectionException::class); |
| 215 | + |
| 216 | + $this->table() |
| 217 | + ->eraseData() |
| 218 | + ; |
| 219 | + } |
| 220 | + |
| 221 | + public function testSchemaRejectSaving(): void |
| 222 | + { |
| 223 | + $this->expectException(ReadonlyConnectionException::class); |
| 224 | + |
| 225 | + $table = $this->database |
| 226 | + ->table('not_allowed_to_creation'); |
| 227 | + |
| 228 | + $schema = $table->getSchema(); |
| 229 | + $schema->primary('id'); |
| 230 | + $schema->string('value')->nullable(); |
| 231 | + $schema->save(); |
| 232 | + } |
| 233 | + |
| 234 | + public function testDatabaseAllowSelection(): void |
| 235 | + { |
| 236 | + $this->expectNotToPerformAssertions(); |
| 237 | + |
| 238 | + $this->database->select() |
| 239 | + ->from($this->table) |
| 240 | + ->run() |
| 241 | + ; |
| 242 | + } |
| 243 | + |
| 244 | + public function testDatabaseRejectUpdate(): void |
| 245 | + { |
| 246 | + $this->expectException(ReadonlyConnectionException::class); |
| 247 | + |
| 248 | + $this->database->update($this->table, ['value' => 'example']) |
| 249 | + ->run() |
| 250 | + ; |
| 251 | + } |
| 252 | + |
| 253 | + public function testDatabaseRejectInsert(): void |
| 254 | + { |
| 255 | + $this->expectException(ReadonlyConnectionException::class); |
| 256 | + |
| 257 | + $this->database->insert($this->table) |
| 258 | + ->columns('value') |
| 259 | + ->values('example') |
| 260 | + ->run() |
| 261 | + ; |
| 262 | + } |
| 263 | + |
| 264 | + public function testDatabaseRejectDelete(): void |
| 265 | + { |
| 266 | + $this->expectException(ReadonlyConnectionException::class); |
| 267 | + |
| 268 | + $this->database->delete($this->table) |
| 269 | + ->run() |
| 270 | + ; |
| 271 | + } |
| 272 | + |
| 273 | + public function testDatabaseAllowRawQuery(): void |
| 274 | + { |
| 275 | + $this->expectNotToPerformAssertions(); |
| 276 | + |
| 277 | + $this->database->query('SELECT 1'); |
| 278 | + } |
| 279 | + |
| 280 | + public function testDatabaseRejectRawExecution(): void |
| 281 | + { |
| 282 | + $this->expectException(ReadonlyConnectionException::class); |
| 283 | + |
| 284 | + $this->database->execute("DROP TABLE {$this->table}"); |
| 285 | + } |
| 286 | +} |
0 commit comments