-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestDatabaseTest.php
More file actions
224 lines (174 loc) · 10.4 KB
/
TestDatabaseTest.php
File metadata and controls
224 lines (174 loc) · 10.4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php declare(strict_types=1);
namespace Cspray\DatabaseTesting\Tests\Unit;
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter;
use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterFactory;
use Cspray\DatabaseTesting\DatabaseAwareTest;
use Cspray\DatabaseTesting\DatabaseCleanup\CleanupStrategy;
use Cspray\DatabaseTesting\DatabaseRepresentation\Table;
use Cspray\DatabaseTesting\Exception\ConnectionAlreadyEstablished;
use Cspray\DatabaseTesting\Exception\ConnectionNotEstablished;
use Cspray\DatabaseTesting\Fixture\Fixture;
use Cspray\DatabaseTesting\RequiresTestDatabaseSettings;
use Cspray\DatabaseTesting\TestDatabase;
use Phake;
use Phake\IMock;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Runtime\PropertyGetHook;
use PHPUnit\Framework\TestCase;
#[CoversClass(TestDatabase::class)]
final class TestDatabaseTest extends TestCase {
protected function setUp() : void {
// Generally we wouldn't want our test to know this much about the internal state of our subject
// nor would we want to be using a static property. However, due to the unique constraints present in
// writing test adapters and how an end-user must be able to access the underlying connection without
// direct access to the TestDatabase _instance_, we must rely on access to the class for exposing certain
// portions of the API. In a realistic integration scenario, this should be carried out by the extension/plugin
// integrating with the testing framework by calling TestDatabase::closeConnection at the appropriate point
// in that framework's lifecycle
$reflection = new \ReflectionClass(TestDatabase::class);
$reflection->setStaticPropertyValue('connectionAdapter', null);
}
public function testAttemptingToCallConnectionWithoutProperlyEstablishingConnectionThrowsException() : void {
$this->expectException(ConnectionNotEstablished::class);
$this->expectExceptionMessage(
'A connection to the test database MUST be established before invoking ' . TestDatabase::class . '::connection'
);
TestDatabase::connection();
}
/**
* @return array{
* 0:RequiresTestDatabaseSettings&IMock,
* 1:ConnectionAdapter&IMock,
* 2:CleanupStrategy&IMock,
* 3:ConnectionAdapterFactory&IMock,
* }
*/
private function defaultMocks() : array {
$cleanupStrategy = Phake::mock(CleanupStrategy::class);
$connectionAdapterFactory = Phake::mock(ConnectionAdapterFactory::class);
$connectionAdapter = Phake::mock(ConnectionAdapter::class);
$requiresTestDatabase = Phake::mock(RequiresTestDatabaseSettings::class);
Phake::when($requiresTestDatabase)->connectionAdapterFactory()->thenReturn($connectionAdapterFactory);
Phake::when($requiresTestDatabase)->cleanupStrategy()->thenReturn($cleanupStrategy);
Phake::when($connectionAdapterFactory)->createConnectionAdapter()->thenReturn($connectionAdapter);
return [$requiresTestDatabase, $connectionAdapter, $cleanupStrategy, $connectionAdapterFactory];
}
public function testEstablishingConnectionCreatesConnectionAdapterAndConnectsToTestDatabase() : void {
[$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks();
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
Phake::verify($connectionAdapter, Phake::times(1))->establishConnection();
}
public function testCallingEstablishingConnectionMultipleTimesWithoutClosingConnectionThrowsException() : void {
[$requiresTestDatabase] = $this->defaultMocks();
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
$this->expectException(ConnectionAlreadyEstablished::class);
$this->expectExceptionMessage(
'Attempting to establish an already established connection. Please ensure you call ' . TestDatabase::class . '::closeConnection before calling establishConnection again.'
);
$subject->establishConnection();
}
public function testCallingConnectionAfterEstablishingConnectionReturnsTheCorrectConnectionObject() : void {
[$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks();
$connection = new \stdClass();
Phake::when($connectionAdapter)->underlyingConnection()->thenReturn($connection);
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
self::assertSame($connection, TestDatabase::connection());
}
public function testPrepareForTestWithoutEstablishingConnectionThrowsException() : void {
[$requiresTestDatabase] = $this->defaultMocks();
$databaseAwareTest = Phake::mock(DatabaseAwareTest::class);
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$this->expectExceptionMessage(ConnectionNotEstablished::class);
$this->expectExceptionMessage(
'A connection to the test database MUST be established before invoking ' . TestDatabase::class . '::prepareForTest'
);
$subject->prepareForTest($databaseAwareTest);
}
public function testPrepareForTestWithEstablishedConnectionCallsCorrectOperationsInOrder() : void {
[$requiresTestDatabase, $connectionAdapter, $cleanupStrategy] = $this->defaultMocks();
$databaseAwareTest = Phake::mock(DatabaseAwareTest::class);
$fixture = Phake::mock(Fixture::class);
Phake::when($databaseAwareTest)->fixtures()->thenReturn([$fixture]);
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
$subject->prepareForTest($databaseAwareTest);
Phake::inOrder(
Phake::verify($connectionAdapter, Phake::times(1))->establishConnection(),
Phake::verify($cleanupStrategy, Phake::times(1))->cleanupBeforeTest($databaseAwareTest, $connectionAdapter),
Phake::verify($connectionAdapter, Phake::times(1))->insert([$fixture]),
);
}
public function testCleanupAfterTestWithoutEstablishingConnectionThrowsException() : void {
[$requiresTestDatabase] = $this->defaultMocks();
$databaseAwareTest = Phake::mock(DatabaseAwareTest::class);
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$this->expectExceptionMessage(ConnectionNotEstablished::class);
$this->expectExceptionMessage(
'A connection to the test database MUST be established before invoking ' . TestDatabase::class . '::cleanupAfterTest'
);
$subject->cleanupAfterTest($databaseAwareTest);
}
public function testCleanupAfterTestWithEstablishedConnectionsCallsCorrectOperationsInOrder() : void {
[$requiresTestDatabase, $connectionAdapter, $cleanupStrategy] = $this->defaultMocks();
$databaseAwareTest = Phake::mock(DatabaseAwareTest::class);
$fixture = Phake::mock(Fixture::class);
Phake::when($databaseAwareTest)->fixtures()->thenReturn([$fixture]);
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
$subject->cleanupAfterTest($databaseAwareTest);
Phake::inOrder(
Phake::verify($connectionAdapter, Phake::times(1))->establishConnection(),
Phake::verify($cleanupStrategy, Phake::times(1))->teardownAfterTest($databaseAwareTest, $connectionAdapter)
);
}
public function testCloseConnectionWithoutEstablishingConnectionThrowsException() : void {
[$requiresTestDatabase] = $this->defaultMocks();
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$this->expectExceptionMessage(ConnectionNotEstablished::class);
$this->expectExceptionMessage(
'A connection to the test database MUST be established before invoking ' . TestDatabase::class . '::closeConnection'
);
$subject->closeConnection();
}
public function testCloseConnectionWithEstablishedConnectionCallsCorrectConnectionAdapterMethodAndNullsTheStaticConnectionAdapter() : void {
[$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks();
$subject = TestDatabase::createFromTestCaseRequiresDatabase(__CLASS__, $requiresTestDatabase);
$subject->establishConnection();
$subject->closeConnection();
Phake::inOrder(
Phake::verify($connectionAdapter, Phake::times(1))->establishConnection(),
Phake::verify($connectionAdapter, Phake::times(1))->closeConnection()
);
$reflection = new \ReflectionClass(TestDatabase::class);
self::assertNull($reflection->getStaticPropertyValue('connectionAdapter'));
}
public function testCallingTableWithoutEstablishingConnectionThrowsException() : void {
$this->expectExceptionMessage(ConnectionNotEstablished::class);
$this->expectExceptionMessage(
'A connection to the test database MUST be established before invoking ' . TestDatabase::class . '::table'
);
TestDatabase::table('name');
}
public function testCallingTableWithEstablishedConnectionReturnsTableFromConnectionAdapterCall() : void {
[$requiresTestDatabase, $connectionAdapter] = $this->defaultMocks();
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_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'));
}
}