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: README.md
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ please attach a copy of the error message and the relevant lines in ``app/cache/
43
43
44
44
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).
45
45
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).
47
47
48
48
### Service validator factory
49
49
@@ -114,6 +114,29 @@ class SomeBundle extends Bundle
114
114
This compiler pass will throw an exception. The message of this exception will contain a list
115
115
of invalid service definitions.
116
116
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
+
117
140
### Configure the validator
118
141
119
142
Both ``ValidateServiceDefinitionsPass`` and ``ServiceDefinitionValidatorFactory`` accept a ``Configuration`` object.
0 commit comments