Skip to content

Commit ba2c522

Browse files
authored
Update README.md
1 parent 609dc91 commit ba2c522

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

README.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,89 @@ $bundles = [
2626
```
2727

2828
## Generate unit-tests from service classes
29-
From time to time it happens that a dev looses the strict tests-first pattern and writes a service
30-
without a proper test, and later on he wants to add a phpunit-test for this service.
31-
Your service probably has some dependencies in the constructor, and now you have to setup mocks for that.
29+
From time to time it happens that a dev is confronted with a brownfield service that has no unit-test, and wants to fix that. If the service has a lot of dependencies in the constructor, preparing the mocks for that can be really annoying (yes, the clean solution is to split the service to smaller parts with nice and easy-to-mock constructor). Anyways, if you are in a situation where you want to add a proper test for a monster-service, this tool can be ver handy.
30+
31+
Lets say you have a simple service like this:
32+
33+
```php
34+
<?php
35+
namespace Tps\UtilBundle\Tests\Fixtures;
36+
37+
use Doctrine\ORM\EntityManager;
38+
use Symfony\Bundle\TwigBundle\TwigEngine;
39+
use Symfony\Component\Form\Form;
40+
41+
class ExampleClass {
42+
/**
43+
* @var EntityManager
44+
*/
45+
private $testForm;
46+
/**
47+
* @var TwigEngine
48+
*/
49+
private $templating;
50+
/**
51+
* @var string
52+
*/
53+
private $primitiveParameter;
54+
55+
public function __construct(Form $testForm, TwigEngine $templating, $primitiveParameter)
56+
{
57+
$this->testForm = $testForm;
58+
$this->templating = $templating;
59+
$this->primitiveParameter = $primitiveParameter;
60+
}
61+
[...]
62+
```
63+
3264
To generate a base template for a service test, run the command
3365

34-
app/console tps:util:generate-service-test "Acme\DemoBundle\Service\MyService"
66+
app/console tps:util:generate-service-test 'Tps\UtilBundle\Tests\Fixtures\ExampleClass'
3567

68+
Generated output will be:
69+
70+
```php
71+
<?php
72+
73+
namespace Tps\UtilBundle\Tests\Tests\Fixtures;
74+
75+
use PHPUnit_Framework_MockObject_MockObject;
76+
use Tps\UtilBundle\Tests\Fixtures\ExampleClass;
77+
use Symfony\Component\Form\Form;
78+
use Symfony\Bundle\TwigBundle\TwigEngine;
79+
80+
class ExampleClassTest extends \PHPUnit_Framework_TestCase
81+
{
82+
/**
83+
* @var PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Form\Form
84+
*/
85+
private $formMock;
86+
/**
87+
* @var PHPUnit_Framework_MockObject_MockObject|\Symfony\Bundle\TwigBundle\TwigEngine
88+
*/
89+
private $twigEngineMock;
90+
/**
91+
* @var int|string|boolean|array
92+
*/
93+
private $primitiveParameter = null;
94+
95+
/**
96+
* @var PHPUnit_Framework_MockObject_MockObject|\Tps\UtilBundle\Tests\Fixtures\ExampleClass
97+
*/
98+
private $exampleClass;
99+
100+
public function setUp()
101+
{
102+
$this->formMock = $this->getMockBuilder(Form::class)
103+
->disableOriginalConstructor()->getMock();
104+
$this->twigEngineMock = $this->getMockBuilder(TwigEngine::class)
105+
->disableOriginalConstructor()->getMock();
106+
107+
$this->exampleClass = new ExampleClass(
108+
$this->formMock,
109+
$this->twigEngineMock,
110+
$this->primitiveParameter
111+
);
112+
}
113+
}
114+
```

0 commit comments

Comments
 (0)