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
Copy file name to clipboardExpand all lines: docs/1-essentials/07-testing.md
+89-24Lines changed: 89 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,56 +12,121 @@ Testing utilities specific to components are documented in their respective chap
12
12
13
13
## Running tests
14
14
15
-
If you created a Tempest application through the [recommended installation process](../0-getting-started/02-installation.md), you already have access to `tests/IntegrationTestCase`, which your application tests can inherit from.
15
+
Any test class that wants to interact with Tempest should extend from [`IntegrationTest`](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Framework/Testing/IntegrationTest.php). Next, any test class should end with the suffix `Test`.
16
16
17
-
In this case, you may use the `composer phpunit` command to run your test suite.
17
+
Running the test suite is done by running `composer phpunit`.
18
18
19
19
```sh
20
20
composer phpunit
21
21
```
22
22
23
-
## Creating new test files
23
+
## Test-specific discovery locations
24
+
25
+
Tempest will only discover non-dev namespaces defined in composer.json automatically. That means that `{:hl-keyword:require-dev:}` namespaces aren't discovered automatically. Whenever you need Tempest to discover test-specific locations, you may specify them within the `discoverTestLocations()` method of the provided `IntegrationTest` class.
24
26
25
-
By default, PHPUnit is configured to look for test files that end in `*Test.php` in the root `tests` directory. You may create a such a file and make it extend `IntegrationTestCase`.
27
+
On top of that, Tempest _will_ look for files in the `tests/Fixtures` directory and discover them by default. You can override this behavior by providing your own implementation of `discoverTestLocations()`, where you can return an array of `DiscoveryLocation` objects (or nothing).
26
28
27
29
```php tests/HomeControllerTest.php
28
-
use Tests\IntegrationTestCase;
30
+
use Tempest\Core\DiscoveryLocation;
31
+
use Tempest\Framework\Testing\IntegrationTest;
29
32
30
-
final class HomeControllerTest extends IntegrationTestCase
33
+
final class HomeControllerTest extends IntegrationTest
31
34
{
32
-
public function test_index(): void
35
+
protected function discoverTestLocations(): array
33
36
{
34
-
$this->http
35
-
->get('/')
36
-
->assertOk();
37
+
return [
38
+
new DiscoveryLocation('Tests\\OtherFixtures', __DIR__ . '/OtherFixtures'),
39
+
];
37
40
}
38
41
}
39
42
```
40
43
41
-
## Test-specific discovery locations
44
+
## Using the database
42
45
43
-
Tempest does not discover files outside of the namespaces defined in the `require` object of `composer.json`. If you need Tempest to discover test-specific fixture files, you may specify paths using the `discoveryLocations` property of the provided `IntegrationTestCase` class.
46
+
If you want to test code that interacts with the database, your test class can call the `setupDatabase()` method. This method will create and migrate a clean database for you on the fly.
44
47
45
-
For instance, you may create a `tests/config` directory that contains test-specific configuration files, and instruct Tempest to discover them:
48
+
```php
49
+
class TodoControllerTest extends IntegrationTest
50
+
{
51
+
protected function setUp(): void
52
+
{
53
+
parent::setUp();
46
54
47
-
```php tests/IntegrationTestCase.php
48
-
use Tempest\Core\DiscoveryLocation;
55
+
$this->setupDatabase();
56
+
}
57
+
}
58
+
```
49
59
50
-
final class IntegrationTestCase extends TestCase
51
-
{
52
-
protected string $root = __DIR__ . '/../';
60
+
Most likely, you'll want to use a test-specific database connection. You can create a `database.config.php` file anywhere within test-specific discovery locations, and Tempest will use that connection instead of the project's default. For example, you can create a file `tests/Fixtures/database.config.php` like so:
53
61
54
-
protected function setUp(): void
62
+
```php tests/Fixtures/database.config.php
63
+
<?php
64
+
65
+
use Tempest\Database\Config\SQLiteConfig;
66
+
67
+
return new SQLiteConfig(
68
+
path: __DIR__ . '/database-testing.sqlite'
69
+
);
70
+
```
71
+
72
+
By default, no tables will be migrated. You can choose to provide a list of migrations that will be run for every test that calls `setupDatabase()`, or you can run specific migrations on a per-test basis.
73
+
74
+
```php
75
+
class TodoControllerTest extends IntegrationTest
76
+
{
77
+
protected function migrateDatabase(): void
55
78
{
56
-
$this->discoveryLocations = [
57
-
new DiscoveryLocation(namespace: 'Tests\\Config', path: __DIR__ . '/config'),
58
-
];
79
+
$this->migrate(
80
+
CreateMigrationsTable::class,
81
+
CreateTodosTable::class,
82
+
);
83
+
}
84
+
}
85
+
```
59
86
60
-
parent::setUp();
87
+
```php
88
+
class TodoControllerTest extends IntegrationTest
89
+
{
90
+
public function test_create_todo(): void
91
+
{
92
+
$this->migrate(
93
+
CreateMigrationsTable::class,
94
+
CreateTodosTable::class,
95
+
);
96
+
97
+
// …
61
98
}
62
99
}
63
100
```
64
101
102
+
## Tester utilities
103
+
104
+
The `IntegrationTest` provides several utilities to make testing easier. You can read the details about each tester utility on the documentation page of its respective component. For example, there's the [http tester](../1-essentials/01-routing.md#testing) that helps you test HTTP requests:
105
+
106
+
```php
107
+
$this->http
108
+
->get('/account/profile')
109
+
->assertOk()
110
+
->assertSee('My Profile');
111
+
```
112
+
113
+
There's the [console tester](../1-essentials/04-console-commands.md#testing):
114
+
115
+
```php tests/ExportUsersCommandTest.php
116
+
$this->console
117
+
->call(ExportUsersCommand::class)
118
+
->assertSuccess()
119
+
->assertSee('12 users exported');
120
+
121
+
$this->console
122
+
->call(WipeDatabaseCommand::class)
123
+
->assertSee('caution')
124
+
->submit()
125
+
->assertSuccess();
126
+
```
127
+
128
+
And many, many more.
129
+
65
130
## Changing the location of tests
66
131
67
132
The `phpunit.xml` file contains a `{html}<testsuite>` element that configures the directory in which PHPUnit looks for test files. This may be changed to follow any rule of your convenience.
@@ -92,7 +157,7 @@ The next step is to create a `tests/Pest.php` file, which will instruct Pest how
0 commit comments