-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestDatabase.php
More file actions
97 lines (83 loc) · 3.44 KB
/
TestDatabase.php
File metadata and controls
97 lines (83 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types=1);
namespace Cspray\DatabaseTesting;
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter;
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterFactory;
use Cspray\DatabaseTesting\DatabaseCleanup\CleanupStrategy;
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 {
private static ?ConnectionAdapter $connectionAdapter = null;
private function __construct(
private readonly string $testClass,
private readonly ConnectionAdapterFactory $connectionAdapterFactory,
private readonly CleanupStrategy $cleanupStrategy,
) {}
/**
* @param class-string $class
* @param RequiresTestDatabaseSettings $requiresTestDatabase
* @return self
*@internal
*/
public static function createFromTestCaseRequiresDatabase(
string $class,
RequiresTestDatabaseSettings $requiresTestDatabase
) : self {
return new self(
$class,
$requiresTestDatabase->connectionAdapterFactory(),
$requiresTestDatabase->cleanupStrategy()
);
}
/**
* Allow for introspection of a database table.
*
* @param non-empty-string $name
* @return Table
*/
public static function table(string $name) : Table {
self::verifyConnectionEstablished(__METHOD__);
return new ClosureDataProviderTable($name, fn() => self::$connectionAdapter->selectAll($name));
}
/**
* @template UnderlyingConnection of object
* @return UnderlyingConnection
*/
public static function connection() : object {
self::verifyConnectionEstablished(__METHOD__);
return self::$connectionAdapter->underlyingConnection();
}
public function establishConnection() : void {
if (self::$connectionAdapter !== null) {
throw ConnectionAlreadyEstablished::fromConnectionAlreadyEstablished();
}
self::$connectionAdapter = $this->connectionAdapterFactory->createConnectionAdapter();
self::$connectionAdapter->establishConnection();
}
public function prepareForTest(DatabaseAwareTest $databaseAwareTest) : void {
self::verifyConnectionEstablished(__METHOD__);
$this->cleanupStrategy->cleanupBeforeTest($databaseAwareTest, self::$connectionAdapter);
self::$connectionAdapter->insert($databaseAwareTest->fixtures());
}
public function cleanupAfterTest(DatabaseAwareTest $databaseAwareTest) : void {
self::verifyConnectionEstablished(__METHOD__);
$this->cleanupStrategy->teardownAfterTest($databaseAwareTest, self::$connectionAdapter);
}
public function closeConnection() : void {
self::verifyConnectionEstablished(__METHOD__);
self::$connectionAdapter->closeConnection();
self::$connectionAdapter = null;
}
private static function verifyConnectionEstablished(string $method) : void {
if (self::$connectionAdapter === null) {
throw ConnectionNotEstablished::fromInvalidInvocationBeforeConnectionEstablished($method);
}
}
}