https://github.com/ramunasd/symfony-container-mocks
Symfony container for easy service mocking
https://github.com/ramunasd/symfony-container-mocks
mock-services mocking php phpunit symfony symfony-container tests
Last synced: 18 days ago
JSON representation
Symfony container for easy service mocking
- Host: GitHub
- URL: https://github.com/ramunasd/symfony-container-mocks
- Owner: ramunasd
- License: mit
- Created: 2016-02-23T22:17:16.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-07-24T17:34:39.000Z (almost 7 years ago)
- Last Synced: 2025-04-12T08:07:06.800Z (18 days ago)
- Topics: mock-services, mocking, php, phpunit, symfony, symfony-container, tests
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 22
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Symfony Container Mocks
[](https://travis-ci.org/ramunasd/symfony-container-mocks)
[](https://scrutinizer-ci.com/g/ramunasd/symfony-container-mocks/?branch=master)
[](https://php-eye.com/package/ramunasd/symfony-container-mocks)
[](https://coveralls.io/github/ramunasd/symfony-container-mocks?branch=coveralls)This container enables you to mock services in the Symfony dependency
injection container. It is particularly useful in functional tests.## Features
* Can replace any Symfony service or parameter
* Automatically detects service class from service definition
* Can be used with any mocking framework
* Compatible with Symfony versions 2.7 - 3.4
* Works on all supported version of PHP## OTB supported mocking frameworks
* [phpspec/prophecy](https://github.com/phpspec/prophecy)
## Installation
Add SymfonyContainerMocks using composer:
`composer require "ramunasd/symfony-container-mocks"`
or edit your composer.json:
```js
{
"require": {
"ramunasd/symfony-container-mocks": "*"
}
}
```Replace base container class for test environment in `app/AppKernel.php`
```php
client = static::createClient();
}public function tearDown()
{
$this->client->getContainer()->tearDown();
$this->client = null;parent::tearDown();
}public function testSomethingWithMockedService()
{
$this->client->getContainer()->prophesize('acme.service.custom', Custom::class)
->someMethod([])
->willReturn(false)
->shouldBeCalledTimes(2);// ...
}
}
```### Inject automatically mocked service
> feature works only with flag "debug" enabled.
```php
$mock = $this->client->getContainer()->prophesize('acme.service.custom');
$mock
->myMethod()
->willReturn(true);
```### Other mocking frameworks
```php
// create stub
$mock = $this->getMock(Custom::class);// inject service mock
self::$kernel->getContainer()->setMock('acme.service.custom', $mock);// reset container state
self::$kernel->getContainer()->unMock('acme.service.custom');```
### Set specific framework parameter
```php
// set custom value during test
self::$kernel->getContainer()->setMockedParameter('acme.service.parameter1', 'customValue1');// trigger service, assert results
// reset all parameters to original values
self::$kernel->getContainer()->clearMockedParameters();
```## Things TO DO
* Symfony 4.x support
* PSR-11 adoption## Similar/Related projects
* https://github.com/jakzal/phpunit-injector - inject Symfony services into PHPUnit test cases