Skip to content

Commit cffffb0

Browse files
Merge pull request #34 from emmetog/patch-1
Add instructions to run validation in phpunit
2 parents 4205d1a + e71ab2f commit cffffb0

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ please attach a copy of the error message and the relevant lines in ``app/cache/
4343

4444
If you have an existing Symfony application and you want to ensure that the service definitions are always valid the easiest way is to add the compiler pass to your bundle as described [here](https://github.com/matthiasnoback/symfony-service-definition-validator#compiler-pass). That will validate your service definitions every time the bundle is compiled, which happens every single request if the cache is turned off (the default in debug mode).
4545

46-
If you want to validate the service definitions separately, like in a console command or in a test, you'll need to set it up yourself as described [here](https://github.com/matthiasnoback/symfony-service-definition-validator#service-validator-factory).
46+
If you want to validate the service definitions in phpunit for a Symfony project follow the steps [here](https://github.com/matthiasnoback/symfony-service-definition-validator#running-the-validator-in-phpunit).
4747

4848
### Service validator factory
4949

@@ -114,6 +114,29 @@ class SomeBundle extends Bundle
114114
This compiler pass will throw an exception. The message of this exception will contain a list
115115
of invalid service definitions.
116116

117+
### Running the validator in PHPUnit
118+
In Symfony, adding the compiler pass will validate your services each time the page is loaded in your browser or when you use a command. But what if you want to run the validator on demand using PHPUnit? To do this, first set up the compiler pass as explained above and then create a new PHPUnit test with this inside:
119+
```php
120+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
121+
122+
class ContainerServiceDefinitionsTest extends WebTestCase
123+
{
124+
/**
125+
* @test
126+
*/
127+
public function validateServiceDefinitions()
128+
{
129+
$kernel = static::createKernel();
130+
131+
$kernel->boot();
132+
}
133+
}
134+
```
135+
136+
This simple functional test just boots up the symfony kernel using the "test" environment. If you look back to the code you added to set up the compiler pass you'll see that we enabled the validator for the "test" environment too. So this simple PHPUnit test will validate the services each time it is run.
137+
138+
In fact, if you already have functional tests you don't need this test, since the kernel will be booted (and therefore the services will be validated) in the other functional tests. The example test above is only needed if you don't already have any Symfony functional tests of your application.
139+
117140
### Configure the validator
118141

119142
Both ``ValidateServiceDefinitionsPass`` and ``ServiceDefinitionValidatorFactory`` accept a ``Configuration`` object.

0 commit comments

Comments
 (0)