Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php declare(strict_types=1);
Expand All @@ -46,8 +45,8 @@ namespace Cspray\DatabaseTesting\Demo;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Fixture\LoadFixture;
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
use Cspray\DatabaseTesting\RequiresTestDatabase;
use Cspray\DatabaseTesting\TestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use PHPUnit\Framework\TestCase;
use PDO;

Expand Down Expand Up @@ -87,6 +86,18 @@ final class RepositoryTest extends TestCase {
self::assertSame('cspray', $table->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);
}

}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 9 additions & 1 deletion src/ConnectionAdapter/ConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ public function beginTransaction() : void;

public function rollback() : void;

/**
* @param non-empty-string $table
* @return void
*/
public function truncateTable(string $table) : void;

/**
* @param non-empty-list<Fixture> $fixtures
*/
public function insert(array $fixtures) : void;

public function selectAll(string $name) : Table;
/**
* @param non-empty-string $name
* @return list<array<non-empty-string, mixed>>
*/
public function selectAll(string $name) : array;

}
24 changes: 0 additions & 24 deletions src/RequiresTestDatabase.php

This file was deleted.

6 changes: 5 additions & 1 deletion src/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}

/**
Expand Down
16 changes: 13 additions & 3 deletions tests/Unit/TestDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}