@@ -99,49 +99,42 @@ Symfony tests have access to a special container that includes both the
99
99
public services and the non-removed :ref: `private services <container-public >`
100
100
services::
101
101
102
- public function testSomething()
103
- {
104
- // this call is needed; otherwise the container will be empty
105
- self::bootKernel();
106
-
107
- $container = self::$container;
108
- $someService = $container->get('the-service-ID');
109
-
110
- $result = $someService->something();
111
- $this->assertTrue($result);
112
- }
102
+ // tests/Service/AcmeServiceTest.php
103
+ namespace App\Tests\Service;
113
104
114
- Mocking Services
115
- ~~~~~~~~~~~~~~~~
116
-
117
- TODO
118
-
119
- .. _functional-tests :
105
+ use App\Service\AcmeService;
106
+ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
120
107
121
- Application Tests
122
- -----------------
108
+ class AcmeServiceTest extends KernelTestCase
109
+ {
110
+ public function testSomething()
111
+ {
112
+ // this call is needed; otherwise the container will be empty
113
+ self::bootKernel();
123
114
124
- Application tests check the integration of the different layers of an
125
- application (from the routing to the views). They are no different from unit
126
- tests as far as PHPUnit is concerned, but they have a very specific workflow:
115
+ $container = self::$container;
116
+ $someService = $container->get(AcmeService::class);
127
117
128
- * Make a request ;
129
- * Click on a link or submit a form ;
130
- * Test the response;
131
- * Rinse and repeat.
118
+ $result = $someService->something() ;
119
+ $this->assertTrue($result) ;
120
+ }
121
+ }
132
122
133
- Before creating your first test, install the ``symfony/test-pack `` which
134
- requires multiple packages providing some of the utilities used in the
135
- tests:
123
+ .. tip ::
136
124
137
- .. code-block :: terminal
125
+ To run your application tests, the ``KernelTestCase `` class needs to know which
126
+ is the application kernel to bootstrap it. The kernel class is usually
127
+ defined in the ``KERNEL_CLASS `` environment variable (included in the
128
+ default ``.env.test `` file provided by Symfony Flex):
138
129
139
- $ composer require --dev symfony/test-pack
130
+ If your use case is more complex, you can also override the
131
+ ``createKernel() `` or ``getKernelClass() `` methods of your functional test,
132
+ which take precedence over the ``KERNEL_CLASS `` env var.
140
133
141
134
Set-up your Test Environment
142
135
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143
136
144
- The Client used by application tests creates a Kernel that runs in a special
137
+ The tests creates a Kernel that runs in a special
145
138
``test `` environment. Since Symfony loads the ``config/packages/test/*.yaml ``
146
139
in the ``test `` environment, you can tweak any of your application's settings
147
140
specifically for testing.
@@ -189,7 +182,7 @@ You can also use a different environment entirely, or override the default
189
182
debug mode (``true ``) by passing each as options to the ``createClient() ``
190
183
method::
191
184
192
- $client = static::createClient ([
185
+ self::bootKernel ([
193
186
'environment' => 'my_test_env',
194
187
'debug' => false,
195
188
]);
@@ -253,7 +246,7 @@ that ensures that each test is run with the same unmodified database:
253
246
254
247
$ composer require --dev dama/doctrine-test-bundle
255
248
256
- Now, enable it as a PHPUnit extension or listener :
249
+ Now, enable it as a PHPUnit extension:
257
250
258
251
.. code-block :: xml
259
252
@@ -326,8 +319,32 @@ Empty the database and reload *all* the fixture classes with:
326
319
327
320
For more information, read the `DoctrineFixturesBundle documentation `_.
328
321
329
- Write Your First Functional Test
330
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
322
+ .. _functional-tests :
323
+
324
+ Application Tests
325
+ -----------------
326
+
327
+ Application tests check the integration of all the different layers of the
328
+ application (from the routing to the views). They are no different from unit tests
329
+ or integration tests as far as PHPUnit is concerned, but they have a very specific
330
+ workflow:
331
+
332
+ * Make a request;
333
+ * Click on a link or submit a form;
334
+ * Test the response;
335
+ * Rinse and repeat.
336
+
337
+ Before creating your first test, install the ``symfony/test-pack `` which
338
+ requires multiple packages providing some of the utilities used in the
339
+ tests:
340
+
341
+ .. code-block :: terminal
342
+
343
+ $ composer require --dev symfony/test-pack
344
+
345
+
346
+ Write Your First Application Test
347
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331
348
332
349
Application tests are PHP files that typically live in the ``tests/Controller ``
333
350
directory of your application. If you want to test the pages handled by your
@@ -353,17 +370,6 @@ As an example, a test could look like this::
353
370
}
354
371
}
355
372
356
- .. tip ::
357
-
358
- To run your application tests, the ``WebTestCase `` class needs to know which
359
- is the application kernel to bootstrap it. The kernel class is usually
360
- defined in the ``KERNEL_CLASS `` environment variable (included in the
361
- default ``.env.test `` file provided by Symfony):
362
-
363
- If your use case is more complex, you can also override the
364
- ``createKernel() `` or ``getKernelClass() `` methods of your functional test,
365
- which take precedence over the ``KERNEL_CLASS `` env var.
366
-
367
373
In the above example, you validated that the HTTP response was successful. The
368
374
next step is to validate that the page actually contains the expected content.
369
375
The ``createClient() `` method returns a client, which is like a browser that
0 commit comments