From 68d7bad2264063f2b49dfef31e85015efc26408c Mon Sep 17 00:00:00 2001 From: Charles Sprayberry Date: Thu, 8 May 2025 10:45:21 -0400 Subject: [PATCH] Prepare for 0.5 release, adjust ConnectionAdapter::selectAll to return raw data as array instead of Table --- CHANGELOG.md | 41 +++++++++++++++++++++ README.md | 17 +++++++-- composer.json | 2 +- src/ConnectionAdapter/ConnectionAdapter.php | 10 ++++- src/RequiresTestDatabase.php | 24 ------------ src/TestDatabase.php | 6 ++- tests/Unit/TestDatabaseTest.php | 16 ++++++-- 7 files changed, 83 insertions(+), 33 deletions(-) delete mode 100644 src/RequiresTestDatabase.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 66efae6..db29ff5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,47 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased Changes +## [0.5.0](https://github.com/cspray/database-testing/releases/tag/0.5.0) + +This version represents a MAJOR change to the API and design of this library. +You will NOT be able to simply upgrade to this version and keep everything working. +It is HIGHLY recommended that you review the README for this version, which discusses +the new design and how to get started. + +### Added + +- Created a new `Cspray\DatabaseTestin\DatabaseCleanup\CleanupStrategy` interface to allow more thorough control over how your database is prepared for tests. +- Implemented a thorough "truncate tables" strategy, in addition to the existing "transaction with rollback". + +### Changed + +- Renamed the namespace from `Cspray\DatabaseTestCase` to `Cspray\DatabaseTesting`. +- Updated the `ConnectionAdapter` interface to be more feature complete, to allow interacting with the database without assumption to the connection type. + +### Removed + +- All concrete `ConnectionAdapter` have been removed. Adapter-specific library will be provided and should be used instead. +- The PHPUnit-supported `DatabaseTestCase` has been removed. Testing framework-specific library will be provided and should be used instead. + +## [0.4.0](https://github.com/cspray/database-testing/releases/tag/0.4.0) + +### Added + +- Allow all implemented database adapters to provide an existing connection. + +## [0.3.0](https://github.com/cspray/database-testing/releases/tag/0.3.0) + +### Added + +- Allows the `AmpPostgresConnectionAdapter` to use an existing connection + +## [0.2.1](https://github.com/cspray/database-testing/releases/tag/0.2.1) + +### Changed + +- `Cspray\DatabaseTestCase\AmpPostgresConnectionAdapter` no longer prepares and + executes insert statements in multiple steps. Makes direct use of `PostgresConnection::execute` + ## [0.2.0](https://github.com/cspray/database-test-case/releases/tag/0.2.0) - 2023-03-02 ### Added diff --git a/README.md b/README.md index 5574741..dd30a09 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,7 @@ listed, please submit an issue to this repository! ## Quick Example This example is intended to reflect what should be capable with this library. We're going to -use PHPUnit as our testing framework, it is ubiquitous and likely the framework you'll start off -using with this library. +use [cspray/database-testing-phpunit]() as our testing extension, it is ubiquitous and likely the framework you'll start off using with this library. ```php row(0)->get('name')) self::assertSame('website', $table->row(0)->get('website')); } + + public function testTableCanBeReloadedToGetNewlyInsertedRecords() : void { + $table = TestDatabase::table('my_table'); + + self::assertCount(0, $table); + + $this->myRepository->save(new MyEntity()); + + $table->reload(); + + self::assertCount(1, $table); + } } ``` diff --git a/composer.json b/composer.json index 7f9b300..0617641 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "cspray/database-testing", - "description": "", + "description": "A framework-agnostic library for setting up a database suitable for automated testing.", "type": "library", "keywords": [ "testing", diff --git a/src/ConnectionAdapter/ConnectionAdapter.php b/src/ConnectionAdapter/ConnectionAdapter.php index 388c85d..ab2024f 100644 --- a/src/ConnectionAdapter/ConnectionAdapter.php +++ b/src/ConnectionAdapter/ConnectionAdapter.php @@ -24,6 +24,10 @@ public function beginTransaction() : void; public function rollback() : void; + /** + * @param non-empty-string $table + * @return void + */ public function truncateTable(string $table) : void; /** @@ -31,6 +35,10 @@ public function truncateTable(string $table) : void; */ public function insert(array $fixtures) : void; - public function selectAll(string $name) : Table; + /** + * @param non-empty-string $name + * @return list> + */ + public function selectAll(string $name) : array; } \ No newline at end of file diff --git a/src/RequiresTestDatabase.php b/src/RequiresTestDatabase.php deleted file mode 100644 index b3581d6..0000000 --- a/src/RequiresTestDatabase.php +++ /dev/null @@ -1,24 +0,0 @@ -connectionAdapterFactory; - } - - public function cleanupStrategy() : CleanupStrategy { - return $this->cleanupStrategy; - } -} \ No newline at end of file diff --git a/src/TestDatabase.php b/src/TestDatabase.php index 93e583a..cad5536 100644 --- a/src/TestDatabase.php +++ b/src/TestDatabase.php @@ -8,8 +8,12 @@ use Cspray\DatabaseTesting\DatabaseRepresentation\Table; use Cspray\DatabaseTesting\Exception\ConnectionAlreadyEstablished; use Cspray\DatabaseTesting\Exception\ConnectionNotEstablished; +use Cspray\DatabaseTesting\Internal\ClosureDataProviderTable; /** + * Represents the public API testing framework extensions should interact with to establish test database connections + * and ensure the state of the database before and after tests. + * * @api */ final class TestDatabase { @@ -47,7 +51,7 @@ public static function createFromTestCaseRequiresDatabase( */ public static function table(string $name) : Table { self::verifyConnectionEstablished(__METHOD__); - return self::$connectionAdapter->selectAll($name); + return new ClosureDataProviderTable($name, fn() => self::$connectionAdapter->selectAll($name)); } /** diff --git a/tests/Unit/TestDatabaseTest.php b/tests/Unit/TestDatabaseTest.php index 629d9fc..72f9112 100644 --- a/tests/Unit/TestDatabaseTest.php +++ b/tests/Unit/TestDatabaseTest.php @@ -200,15 +200,25 @@ public function testCallingTableWithoutEstablishingConnectionThrowsException() : public function testCallingTableWithEstablishedConnectionReturnsTableFromConnectionAdapterCall() : void { [$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks(); - $table = Phake::mock(Table::class); - Phake::when($connectionAdapter)->selectAll('table_name')->thenReturn($table); + Phake::when($connectionAdapter)->selectAll('table_name')->thenReturn([ + ['id' => 1, 'name' => 'foo'], + ['id' => 2, 'name' => 'bar'], + ['id' => 3, 'name' => 'baz'], + ]); $subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase); $subject->establishConnection(); $actual = TestDatabase::table('table_name'); - self::assertSame($table, $actual); + self::assertSame('table_name', $actual->name()); + self::assertCount(3, $actual); + self::assertSame(1, $actual->row(0)->get('id')); + self::assertSame('foo', $actual->row(0)->get('name')); + self::assertSame(2, $actual->row(1)->get('id')); + self::assertSame('bar', $actual->row(1)->get('name')); + self::assertSame(3, $actual->row(2)->get('id')); + self::assertSame('baz', $actual->row(2)->get('name')); } } \ No newline at end of file