Skip to content

Commit 5a5c92d

Browse files
authored
Prepare for 0.5 release, adjust ConnectionAdapter::selectAll to return raw data as array instead of Table (#20)
1 parent 5da871f commit 5a5c92d

File tree

7 files changed

+83
-33
lines changed

7 files changed

+83
-33
lines changed

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,47 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased Changes
99

10+
## [0.5.0](https://github.com/cspray/database-testing/releases/tag/0.5.0)
11+
12+
This version represents a MAJOR change to the API and design of this library.
13+
You will NOT be able to simply upgrade to this version and keep everything working.
14+
It is HIGHLY recommended that you review the README for this version, which discusses
15+
the new design and how to get started.
16+
17+
### Added
18+
19+
- Created a new `Cspray\DatabaseTestin\DatabaseCleanup\CleanupStrategy` interface to allow more thorough control over how your database is prepared for tests.
20+
- Implemented a thorough "truncate tables" strategy, in addition to the existing "transaction with rollback".
21+
22+
### Changed
23+
24+
- Renamed the namespace from `Cspray\DatabaseTestCase` to `Cspray\DatabaseTesting`.
25+
- Updated the `ConnectionAdapter` interface to be more feature complete, to allow interacting with the database without assumption to the connection type.
26+
27+
### Removed
28+
29+
- All concrete `ConnectionAdapter` have been removed. Adapter-specific library will be provided and should be used instead.
30+
- The PHPUnit-supported `DatabaseTestCase` has been removed. Testing framework-specific library will be provided and should be used instead.
31+
32+
## [0.4.0](https://github.com/cspray/database-testing/releases/tag/0.4.0)
33+
34+
### Added
35+
36+
- Allow all implemented database adapters to provide an existing connection.
37+
38+
## [0.3.0](https://github.com/cspray/database-testing/releases/tag/0.3.0)
39+
40+
### Added
41+
42+
- Allows the `AmpPostgresConnectionAdapter` to use an existing connection
43+
44+
## [0.2.1](https://github.com/cspray/database-testing/releases/tag/0.2.1)
45+
46+
### Changed
47+
48+
- `Cspray\DatabaseTestCase\AmpPostgresConnectionAdapter` no longer prepares and
49+
executes insert statements in multiple steps. Makes direct use of `PostgresConnection::execute`
50+
1051
## [0.2.0](https://github.com/cspray/database-test-case/releases/tag/0.2.0) - 2023-03-02
1152

1253
### Added

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ listed, please submit an issue to this repository!
3535
## Quick Example
3636

3737
This example is intended to reflect what should be capable with this library. We're going to
38-
use PHPUnit as our testing framework, it is ubiquitous and likely the framework you'll start off
39-
using with this library.
38+
use [cspray/database-testing-phpunit]() as our testing extension, it is ubiquitous and likely the framework you'll start off using with this library.
4039

4140
```php
4241
<?php declare(strict_types=1);
@@ -46,8 +45,8 @@ namespace Cspray\DatabaseTesting\Demo;
4645
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
4746
use Cspray\DatabaseTesting\Fixture\LoadFixture;
4847
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
49-
use Cspray\DatabaseTesting\RequiresTestDatabase;
5048
use Cspray\DatabaseTesting\TestDatabase;
49+
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
5150
use PHPUnit\Framework\TestCase;
5251
use PDO;
5352

@@ -87,6 +86,18 @@ final class RepositoryTest extends TestCase {
8786
self::assertSame('cspray', $table->row(0)->get('name'))
8887
self::assertSame('website', $table->row(0)->get('website'));
8988
}
89+
90+
public function testTableCanBeReloadedToGetNewlyInsertedRecords() : void {
91+
$table = TestDatabase::table('my_table');
92+
93+
self::assertCount(0, $table);
94+
95+
$this->myRepository->save(new MyEntity());
96+
97+
$table->reload();
98+
99+
self::assertCount(1, $table);
100+
}
90101

91102
}
92103
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cspray/database-testing",
3-
"description": "",
3+
"description": "A framework-agnostic library for setting up a database suitable for automated testing.",
44
"type": "library",
55
"keywords": [
66
"testing",

src/ConnectionAdapter/ConnectionAdapter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ public function beginTransaction() : void;
2424

2525
public function rollback() : void;
2626

27+
/**
28+
* @param non-empty-string $table
29+
* @return void
30+
*/
2731
public function truncateTable(string $table) : void;
2832

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

34-
public function selectAll(string $name) : Table;
38+
/**
39+
* @param non-empty-string $name
40+
* @return list<array<non-empty-string, mixed>>
41+
*/
42+
public function selectAll(string $name) : array;
3543

3644
}

src/RequiresTestDatabase.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/TestDatabase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
use Cspray\DatabaseTesting\DatabaseRepresentation\Table;
99
use Cspray\DatabaseTesting\Exception\ConnectionAlreadyEstablished;
1010
use Cspray\DatabaseTesting\Exception\ConnectionNotEstablished;
11+
use Cspray\DatabaseTesting\Internal\ClosureDataProviderTable;
1112

1213
/**
14+
* Represents the public API testing framework extensions should interact with to establish test database connections
15+
* and ensure the state of the database before and after tests.
16+
*
1317
* @api
1418
*/
1519
final class TestDatabase {
@@ -47,7 +51,7 @@ public static function createFromTestCaseRequiresDatabase(
4751
*/
4852
public static function table(string $name) : Table {
4953
self::verifyConnectionEstablished(__METHOD__);
50-
return self::$connectionAdapter->selectAll($name);
54+
return new ClosureDataProviderTable($name, fn() => self::$connectionAdapter->selectAll($name));
5155
}
5256

5357
/**

tests/Unit/TestDatabaseTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,25 @@ public function testCallingTableWithoutEstablishingConnectionThrowsException() :
200200

201201
public function testCallingTableWithEstablishedConnectionReturnsTableFromConnectionAdapterCall() : void {
202202
[$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks();
203-
$table = Phake::mock(Table::class);
204-
Phake::when($connectionAdapter)->selectAll('table_name')->thenReturn($table);
203+
Phake::when($connectionAdapter)->selectAll('table_name')->thenReturn([
204+
['id' => 1, 'name' => 'foo'],
205+
['id' => 2, 'name' => 'bar'],
206+
['id' => 3, 'name' => 'baz'],
207+
]);
205208

206209
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
207210
$subject->establishConnection();
208211

209212
$actual = TestDatabase::table('table_name');
210213

211-
self::assertSame($table, $actual);
214+
self::assertSame('table_name', $actual->name());
215+
self::assertCount(3, $actual);
216+
self::assertSame(1, $actual->row(0)->get('id'));
217+
self::assertSame('foo', $actual->row(0)->get('name'));
218+
self::assertSame(2, $actual->row(1)->get('id'));
219+
self::assertSame('bar', $actual->row(1)->get('name'));
220+
self::assertSame(3, $actual->row(2)->get('id'));
221+
self::assertSame('baz', $actual->row(2)->get('name'));
212222
}
213223

214224
}

0 commit comments

Comments
 (0)