Skip to content

Commit a17bd8a

Browse files
committed
Move around things
1 parent 198b35a commit a17bd8a

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

testing.rst

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -99,49 +99,42 @@ Symfony tests have access to a special container that includes both the
9999
public services and the non-removed :ref:`private services <container-public>`
100100
services::
101101

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;
113104

114-
Mocking Services
115-
~~~~~~~~~~~~~~~~
116-
117-
TODO
118-
119-
.. _functional-tests:
105+
use App\Service\AcmeService;
106+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
120107

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();
123114

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);
127117

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+
}
132122

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::
136124

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):
138129

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.
140133

141134
Set-up your Test Environment
142135
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143136

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
145138
``test`` environment. Since Symfony loads the ``config/packages/test/*.yaml``
146139
in the ``test`` environment, you can tweak any of your application's settings
147140
specifically for testing.
@@ -189,7 +182,7 @@ You can also use a different environment entirely, or override the default
189182
debug mode (``true``) by passing each as options to the ``createClient()``
190183
method::
191184

192-
$client = static::createClient([
185+
self::bootKernel([
193186
'environment' => 'my_test_env',
194187
'debug' => false,
195188
]);
@@ -253,7 +246,7 @@ that ensures that each test is run with the same unmodified database:
253246
254247
$ composer require --dev dama/doctrine-test-bundle
255248
256-
Now, enable it as a PHPUnit extension or listener:
249+
Now, enable it as a PHPUnit extension:
257250

258251
.. code-block:: xml
259252
@@ -326,8 +319,32 @@ Empty the database and reload *all* the fixture classes with:
326319
327320
For more information, read the `DoctrineFixturesBundle documentation`_.
328321

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331348

332349
Application tests are PHP files that typically live in the ``tests/Controller``
333350
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::
353370
}
354371
}
355372

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-
367373
In the above example, you validated that the HTTP response was successful. The
368374
next step is to validate that the page actually contains the expected content.
369375
The ``createClient()`` method returns a client, which is like a browser that

0 commit comments

Comments
 (0)