You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note, it is highly likely you'll need a library that provides an implementation of `Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter`! This library does not come with the adapter appropriate for your database connection!
14
+
15
+
16
+
## Review the tests!
17
+
18
+
If you're here to learn more about how to use `cspray/databse-testing` in your PHPUnit tests, check out the tests in this library! The `phpunit.xml` config and the implemented tests provide a working example on how to use it!
19
+
20
+
## Quick Start
21
+
22
+
The examples below uses a `ConnectionAdapter` and `ConnectionAdapterFactory` available from `cspray/database-testing-pdo`. If PDO is not an appropriate connection for your use case, please replace with the appropriate `ConnectionAdapter` implementation.
23
+
24
+
### Register your Extension
25
+
26
+
The first step is to register the extension in your PHPUnit configuration. Ensure the following XML is present in `phpunit.xml` (or the file you use for you PHPUnit config).
The next step is to add the `#[RequiresTestDatabase]` attribute to your PHPUnit TestCase. There's no inheritance or traits required, you don't need to change what `TestCase` you extend!
37
+
38
+
```php
39
+
<?php declare(strict_types=1);
40
+
41
+
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
42
+
43
+
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
44
+
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
45
+
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
46
+
use PHPUnit\Framework\TestCase;
47
+
48
+
#[RequiresTestDatabase(
49
+
new SqliteConnectionAdapterFactory(
50
+
':memory:',
51
+
'/path/to/my/schema'
52
+
),
53
+
new TransactionWithRollback()
54
+
)]
55
+
final class MyDemoTest extends TestCase {
56
+
57
+
}
58
+
```
59
+
60
+
### Inject the TestDatabase
61
+
62
+
To work with your test database there is a helper class with the type, `Cspray\DatabaseTesting\TestDatabase`. This object allows accessing the underlying test database connection, load fixtures, and access the data in a given table.
63
+
64
+
This object is managed by the extension. It is created when your TestCase has started, before any tests or setup have run. This property MUST BE static, the extension does not have access to the TestCase instance that is running.
65
+
66
+
```php
67
+
<?php declare(strict_types=1);
68
+
69
+
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
70
+
71
+
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
72
+
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
73
+
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
74
+
use Cspray\DatabaseTesting\TestDatabase;use PHPUnit\Framework\TestCase;
75
+
76
+
#[RequiresTestDatabase(
77
+
new SqliteConnectionAdapterFactory(
78
+
':memory:',
79
+
'/path/to/my/schema'
80
+
),
81
+
new TransactionWithRollback()
82
+
)]
83
+
final class MyDemoTest extends TestCase {
84
+
85
+
#[InjectTestDatabase]
86
+
private static TestDatabase $testDatabase;
87
+
88
+
}
89
+
```
90
+
91
+
### Load Fixtures and Run Test
92
+
93
+
In our example, we're going to load tests both in `setUp` and as an attribute on a given test. This example is meant to show in what order fixtures get ran, and to show that there are alternatives to loading fixtures if you do not like attributes.
94
+
95
+
```php
96
+
<?php declare(strict_types=1);
97
+
98
+
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
99
+
100
+
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
101
+
use Cspray\DatabaseTesting\Fixture\LoadFixture;use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
102
+
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
103
+
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
104
+
use Cspray\DatabaseTesting\TestDatabase;
105
+
use PHPUnit\Framework\TestCase;
106
+
107
+
#[RequiresTestDatabase(
108
+
new SqliteConnectionAdapterFactory(
109
+
':memory:',
110
+
'/path/to/my/schema'
111
+
),
112
+
new TransactionWithRollback()
113
+
)]
114
+
final class MyDemoTest extends TestCase {
115
+
116
+
#[InjectTestDatabase]
117
+
private static TestDatabase $testDatabase;
118
+
119
+
protected function setUp() : void{
120
+
parent::setUp(); // TODO: Change the autogenerated stub
121
+
self::$testDatabase->loadFixtures([
122
+
new SingleRecordFixture('my_table', ['name' => 'from setup'])
123
+
]);
124
+
}
125
+
126
+
#[LoadFixture(
127
+
new SingleRecordFixture('my_table', ['name' => 'from attribute'])
128
+
)]
129
+
public function testTableHasCorrectRecords() : void {
Generally speaking, I would not recommend mixing approaches to loading fixtures. Either use the `#[LoadFixture]` Attribute, or use `TestDatabase` helper in your setup or test methods.
0 commit comments