Skip to content

Commit e24e926

Browse files
authored
Bug/fix connection adapter test case (#21)
* Fix ConnectionAdapterTestCase with ConnectionAdapter::selectAll returning array * Adjust TestDatabase to not require static methods * Update README example
1 parent 5a5c92d commit e24e926

File tree

4 files changed

+40
-311
lines changed

4 files changed

+40
-311
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +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 [cspray/database-testing-phpunit]() as our testing extension, it is ubiquitous and likely the framework you'll start off using with this library.
38+
use [cspray/database-testing-phpunit](https://github.com/cspray/databse-testing-phpunit) as our testing extension, it is ubiquitous and likely the framework you'll start off using with this library.
3939

4040
```php
4141
<?php declare(strict_types=1);
@@ -46,6 +46,7 @@ use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
4646
use Cspray\DatabaseTesting\Fixture\LoadFixture;
4747
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
4848
use Cspray\DatabaseTesting\TestDatabase;
49+
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
4950
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
5051
use PHPUnit\Framework\TestCase;
5152
use PDO;
@@ -60,13 +61,16 @@ use PDO;
6061
)]
6162
final class RepositoryTest extends TestCase {
6263

64+
#[InjectTestDatabase]
65+
private static TestDatabase $testDatabase;
66+
6367
private PDO $pdo;
6468
private MyRepository $myRepository;
6569

6670
protected function setUp() : void {
6771
// be sure to use the connection from TestDatabase! depending on CleanupStrategy,
6872
// using a different connection could wind up with a dirty database state
69-
$this->pdo = TestDatabase::connection();
73+
$this->pdo = self::$testDatabase->connection();
7074
$this->myRepository = new MyRepository($this->pdo);
7175
}
7276

@@ -79,7 +83,7 @@ final class RepositoryTest extends TestCase {
7983
])
8084
)]
8185
public function testTableHasCorrectlyLoadedFixtures() : void {
82-
$table = TestDatabase::table('my_table');
86+
$table = self::$testDatabase->table('my_table');
8387

8488
self::assertCount(1, $table);
8589

@@ -88,7 +92,7 @@ final class RepositoryTest extends TestCase {
8892
}
8993

9094
public function testTableCanBeReloadedToGetNewlyInsertedRecords() : void {
91-
$table = TestDatabase::table('my_table');
95+
$table = self::$testDatabase->table('my_table');
9296

9397
self::assertCount(0, $table);
9498

@@ -101,5 +105,3 @@ final class RepositoryTest extends TestCase {
101105

102106
}
103107
```
104-
105-

src/Internal/ConnectionAdapterTestCase.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public function testInsertAfterEstablishingConnectionResultsInAppropriateRecords
113113

114114
public function testBeginTransactionAndRollbackResultsInRecordsNotPersisted() : void {
115115
$this->connectionAdapter->establishConnection();
116-
//
116+
117117
$sql = 'SELECT * FROM my_table';
118-
// $records = $this->executeSelectSql($sql);
119-
//
120-
// self::assertEmpty($records);
118+
$records = $this->executeSelectSql($sql);
119+
120+
self::assertEmpty($records);
121121

122122
$this->connectionAdapter->beginTransaction();
123123

@@ -171,29 +171,26 @@ public function testFetchingTableFromConnectionAdapterAllowsInspectingCorrectRec
171171
new SingleRecordFixture('my_table', ['name' => 'Harry']),
172172
]);
173173

174-
$table = $this->connectionAdapter->selectAll('my_table');
174+
$data = $this->connectionAdapter->selectAll('my_table');
175175

176-
self::assertSame('my_table', $table->name());
177-
self::assertCount(1, $table);
178-
self::assertSame('Harry', $table->row(0)->get('name'));
176+
self::assertCount(1, $data);
177+
self::assertSame('Harry', $data[0]['name']);
179178

180179
$this->connectionAdapter->insert([
181180
new SingleRecordFixture('my_table', ['name' => 'Mack']),
182181
]);
183182

184-
$table->reload();
183+
$newData = $this->connectionAdapter->selectAll('my_table');
185184

186-
self::assertSame('my_table', $table->name());
187-
self::assertCount(2, $table);
188-
self::assertSame('Harry', $table->row(0)->get('name'));
189-
self::assertSame('Mack', $table->row(1)->get('name'));
185+
self::assertCount(2, $newData);
186+
self::assertSame('Harry', $newData[0]['name']);
187+
self::assertSame('Mack', $newData[1]['name']);
190188

191189
$this->connectionAdapter->truncateTable('my_table');
192190

193-
$table->reload();
191+
$truncatedData = $this->connectionAdapter->selectAll('my_table');
194192

195-
self::assertSame('my_table', $table->name());
196-
self::assertCount(0, $table);
193+
self::assertCount(0, $truncatedData);
197194
}
198195

199196

src/TestDatabase.php

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Cspray\DatabaseTesting;
44

55
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter;
6-
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterFactory;
7-
use Cspray\DatabaseTesting\DatabaseCleanup\CleanupStrategy;
86
use Cspray\DatabaseTesting\DatabaseRepresentation\Table;
9-
use Cspray\DatabaseTesting\Exception\ConnectionAlreadyEstablished;
10-
use Cspray\DatabaseTesting\Exception\ConnectionNotEstablished;
7+
use Cspray\DatabaseTesting\Fixture\Fixture;
118
use Cspray\DatabaseTesting\Internal\ClosureDataProviderTable;
129

1310
/**
@@ -18,80 +15,37 @@
1815
*/
1916
final class TestDatabase {
2017

21-
private static ?ConnectionAdapter $connectionAdapter = null;
22-
2318
private function __construct(
24-
private readonly string $testClass,
25-
private readonly ConnectionAdapterFactory $connectionAdapterFactory,
26-
private readonly CleanupStrategy $cleanupStrategy,
19+
private readonly ConnectionAdapter $connectionAdapter,
2720
) {}
2821

29-
/**
30-
* @param class-string $class
31-
* @param RequiresTestDatabaseSettings $requiresTestDatabase
32-
* @return self
33-
*@internal
34-
*/
35-
public static function createFromTestCaseRequiresDatabase(
36-
string $class,
37-
RequiresTestDatabaseSettings $requiresTestDatabase
38-
) : self {
39-
return new self(
40-
$class,
41-
$requiresTestDatabase->connectionAdapterFactory(),
42-
$requiresTestDatabase->cleanupStrategy()
43-
);
44-
}
45-
46-
/**
47-
* Allow for introspection of a database table.
48-
*
49-
* @param non-empty-string $name
50-
* @return Table
51-
*/
52-
public static function table(string $name) : Table {
53-
self::verifyConnectionEstablished(__METHOD__);
54-
return new ClosureDataProviderTable($name, fn() => self::$connectionAdapter->selectAll($name));
22+
public function connectionAdapter() : ConnectionAdapter {
23+
return $this->connectionAdapter;
5524
}
5625

5726
/**
5827
* @template UnderlyingConnection of object
5928
* @return UnderlyingConnection
6029
*/
61-
public static function connection() : object {
62-
self::verifyConnectionEstablished(__METHOD__);
63-
return self::$connectionAdapter->underlyingConnection();
64-
}
65-
66-
public function establishConnection() : void {
67-
if (self::$connectionAdapter !== null) {
68-
throw ConnectionAlreadyEstablished::fromConnectionAlreadyEstablished();
69-
}
70-
self::$connectionAdapter = $this->connectionAdapterFactory->createConnectionAdapter();
71-
self::$connectionAdapter->establishConnection();
30+
public function connection() : object {
31+
return $this->connectionAdapter->underlyingConnection();
7232
}
7333

74-
public function prepareForTest(DatabaseAwareTest $databaseAwareTest) : void {
75-
self::verifyConnectionEstablished(__METHOD__);
76-
$this->cleanupStrategy->cleanupBeforeTest($databaseAwareTest, self::$connectionAdapter);
77-
self::$connectionAdapter->insert($databaseAwareTest->fixtures());
78-
}
79-
80-
public function cleanupAfterTest(DatabaseAwareTest $databaseAwareTest) : void {
81-
self::verifyConnectionEstablished(__METHOD__);
82-
$this->cleanupStrategy->teardownAfterTest($databaseAwareTest, self::$connectionAdapter);
83-
}
84-
85-
public function closeConnection() : void {
86-
self::verifyConnectionEstablished(__METHOD__);
87-
self::$connectionAdapter->closeConnection();
88-
self::$connectionAdapter = null;
34+
/**
35+
* @param list<Fixture> $fixtures
36+
*/
37+
public function loadFixtures(array $fixtures) : void {
38+
$this->connectionAdapter->insert($fixtures);
8939
}
9040

91-
private static function verifyConnectionEstablished(string $method) : void {
92-
if (self::$connectionAdapter === null) {
93-
throw ConnectionNotEstablished::fromInvalidInvocationBeforeConnectionEstablished($method);
94-
}
41+
/**
42+
* Allow for introspection of a database table.
43+
*
44+
* @param non-empty-string $name
45+
* @return Table
46+
*/
47+
public function table(string $name) : Table {
48+
return new ClosureDataProviderTable($name, fn() => $this->connectionAdapter->selectAll($name));
9549
}
9650

9751
}

0 commit comments

Comments
 (0)