|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ipl\Tests\Sql; |
| 4 | + |
| 5 | +use ipl\Sql\Connection; |
| 6 | +use ipl\Sql\Select; |
| 7 | +use ipl\Sql\Test\SharedDatabases; |
| 8 | + |
| 9 | +/** |
| 10 | + * A test for a test component! Yay! |
| 11 | + */ |
| 12 | +class SharedDatabasesTest extends TestCase |
| 13 | +{ |
| 14 | + use SharedDatabases; |
| 15 | + |
| 16 | + /** @dataProvider sharedDatabases */ |
| 17 | + public function testInsert(Connection $db) |
| 18 | + { |
| 19 | + // This is the first case, so the table must have been dropped and be empty |
| 20 | + $result = $db->select((new Select())->columns('name')->from('test'))->fetchAll(); |
| 21 | + $this->assertEmpty($result); |
| 22 | + |
| 23 | + $db->insert('test', ['name' => 'test']); |
| 24 | + $db->insert('test', ['name' => 'test2']); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @depends testInsert |
| 29 | + * @dataProvider sharedDatabases |
| 30 | + */ |
| 31 | + public function testSelect(Connection $db) |
| 32 | + { |
| 33 | + // The previous case inserts "name=test" but tearDown removes it |
| 34 | + $result = $db->select((new Select())->columns('name')->from('test'))->fetchAll(); |
| 35 | + $this->assertCount(1, $result); |
| 36 | + $this->assertSame('test2', $result[0]['name']); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @depends testSelect |
| 41 | + * @dataProvider sharedDatabases |
| 42 | + */ |
| 43 | + public function testUpdate(Connection $db) |
| 44 | + { |
| 45 | + $stmt = $db->update('test', ['name' => 'test3'], ['name = ?' => 'test2']); |
| 46 | + $this->assertEquals(1, $stmt->rowCount()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @depends testUpdate |
| 51 | + * @dataProvider sharedDatabases |
| 52 | + */ |
| 53 | + public function testDelete(Connection $db) |
| 54 | + { |
| 55 | + $stmt = $db->delete('test', ['name = ?' => 'test3']); |
| 56 | + $this->assertEquals(1, $stmt->rowCount()); |
| 57 | + } |
| 58 | + |
| 59 | + protected static function setUpSchema(Connection $db, string $driver): void |
| 60 | + { |
| 61 | + $db->exec('CREATE TABLE test (name VARCHAR(255))'); |
| 62 | + } |
| 63 | + |
| 64 | + protected static function tearDownSchema(Connection $db, string $driver): void |
| 65 | + { |
| 66 | + $db->exec('DROP TABLE IF EXISTS test'); |
| 67 | + } |
| 68 | + |
| 69 | + public function tearDown(): void |
| 70 | + { |
| 71 | + $this->getConnection()->delete('test', ['name = ?' => ['test']]); |
| 72 | + } |
| 73 | +} |
0 commit comments