Skip to content

Commit e67b8fc

Browse files
committed
add support for any mocking framework
1 parent 69df030 commit e67b8fc

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

README.md

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This container enables you to mock services in the Symfony dependency
77
injection container. It is particularly useful in functional tests.
88

9-
## Supported mocking frameworks
9+
## OTB supported mocking frameworks
1010

1111
* phpspec/prophecy
1212

@@ -32,18 +32,31 @@ Replace base container class for test environment in `app/AppKernel.php`
3232
```php
3333
<?php
3434

35+
use Symfony\Component\HttpKernel\Kernel;
36+
use Symfony\Component\Config\Loader\LoaderInterface;
3537
use RDV\SymfonyContainerMocks\DependencyInjection\TestContainer;
3638

37-
/**
38-
* @return string
39-
*/
40-
protected function getContainerBaseClass()
39+
class AppKernel extends Kernel
4140
{
42-
if ('test' == $this->environment) {
43-
return TestContainer::class;
41+
public function registerBundles() {
42+
// TODO: Implement registerBundles() method.
4443
}
4544

46-
return parent::getContainerBaseClass();
45+
public function registerContainerConfiguration(LoaderInterface $loader) {
46+
// TODO: Implement registerContainerConfiguration() method.
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
protected function getContainerBaseClass()
53+
{
54+
if ('test' === $this->environment) {
55+
return TestContainer::class;
56+
}
57+
58+
return parent::getContainerBaseClass();
59+
}
4760
}
4861
```
4962

@@ -98,13 +111,25 @@ class AcmeControllerTest extends WebTestCase
98111

99112
### Class name autodetection
100113

101-
This feature works only with flag "debug" enabled.
114+
> feature works only with flag "debug" enabled.
102115
103116
```php
104-
105-
$mock = $this->client->getContainer()->prophesize('acme.service.custom');
106-
$mock
107-
->myMethod()
108-
->willReturn(true);
109-
117+
$mock = $this->client->getContainer()->prophesize('acme.service.custom');
118+
$mock
119+
->myMethod()
120+
->willReturn(true);
121+
```
122+
123+
### Custom mocking framework
124+
125+
```php
126+
// create stub
127+
$mock = $this->getMock(Custom::class);
128+
129+
// inject service mock
130+
self::$kernel->getContainer()->setMock('acme.service.custom', $mock);
131+
132+
// reset container state
133+
self::$kernel->getContainer()->unMock('acme.service.custom');
134+
110135
```

src/DependencyInjection/TestContainer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class TestContainer extends Container
2121
* @param string $id The service identifier
2222
* @param string|null $class Class or interface fully qualified name
2323
* @return \Prophecy\Prophecy\ObjectProphecy
24+
* @throws \InvalidArgumentException
25+
* @throws \BadMethodCallException
26+
* @throws \Prophecy\Exception\Prophecy\ObjectProphecyException
2427
*/
2528
public function prophesize($id, $class = null)
2629
{
@@ -58,6 +61,15 @@ public function reset()
5861
$this->mocked = array();
5962
}
6063

64+
/**
65+
* @param string $id
66+
* @param mixed $mock
67+
*/
68+
public function setMock($id, $mock)
69+
{
70+
$this->mocked[$id] = $mock;
71+
}
72+
6173
/**
6274
* @param string $id
6375
*/

tests/DependencyInjection/ClassDetectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RDV\Tests\SymfonyContainerMocks\DependencyInjection;
44

5-
use RDV\SymfonyContainerMocks\DependencyInjection\ContainerMocks;
5+
use RDV\SymfonyContainerMocks\DependencyInjection\TestContainer;
66
use RDV\SymfonyContainerMocks\DependencyInjection\DefinitionLoader;
77
use RDV\Tests\SymfonyContainerMocks\Fixtures\TestKernel;
88

@@ -17,7 +17,7 @@ protected function tearDown()
1717
public function testServiceClassIsDetectedCorrectly()
1818
{
1919
$kernel = $this->getKernel();
20-
/** @var ContainerMocks $container */
20+
/** @var TestContainer $container */
2121
$container = $kernel->getContainer();
2222
$mock = $container->prophesize('test_service');
2323
$this->assertInstanceOf('Prophecy\Prophecy\ObjectProphecy', $mock);

tests/DependencyInjection/TestContainerTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ public function tearDown()
3434
$this->container->tearDown();
3535
}
3636

37+
public function testCustomMockSetClear()
38+
{
39+
$id = mt_rand();
40+
$service = $this->createMock('Symfony\Component\Config\Loader\LoaderInterface');
41+
$service
42+
->expects($this->once())
43+
->method('load')
44+
->willReturn(true);
45+
$this->container->setMock($id, $service);
46+
47+
// simulate call on mock
48+
$this->assertTrue($this->container->get($id)->load('test'));
49+
50+
// clear container state
51+
$this->container->unMock($id);
52+
53+
// must fail cause service does not exist
54+
try {
55+
$this->container->get($id);
56+
} catch (\Exception $e) {
57+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e);
58+
}
59+
}
60+
3761
public function testBehaviorDoesNotChangeByDefault()
3862
{
3963
$this->assertTrue($this->container->has('service1'));
@@ -86,7 +110,7 @@ public function testMockCanBeRemovedAndContainerFallsBackToTheOriginalService()
86110
{
87111
$id = 'service1';
88112
$this->container->prophesize($id, 'stdClass');
89-
$this->container->unmock($id);
113+
$this->container->unMock($id);
90114

91115
$this->assertTrue($this->container->has($id));
92116
$this->assertEquals($this->services[$id], $this->container->get($id));

0 commit comments

Comments
 (0)