Skip to content

Add helper method to set service mocks in the container. #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions doc/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,34 @@ class MyTestCase extends WebTestCase {
}
```

If you need to mock a service in your command, you can use the `setServiceMock` method.
It is important to set the `$reuseKernel` argument to `true` in the `runCommand` method call
if you want to keep your services mocked in the command.

```php
use Liip\FunctionalTestBundle\Test\WebTestCase;
use App\Service\YourService;

class MyTestCase extends WebTestCase
{
public function myTest()
{
// boot kernel
$kernel = static::bootKernel(['environment' => static::$env]);
$container = $kernel->getContainer();
// create a service mock
$mock = $this->getServiceMockBuilder(YourService::class)
->onlyMethods(['getId'])
->getMock();
$mock->expects($this->once())
->method('getId')
->willReturn(42);
// set the service mock in the container
$this->setServiceMock($container, YourService::class, $mock);
// now you have mocked service App\Service\YourService in your command
$this->runCommand('myCommand', [], true);
}
}
```

← [Basic usage](./basic.md) • [Logged client](./logged.md) →
19 changes: 19 additions & 0 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ protected function getServiceMockBuilder(string $id): MockBuilder
return $this->getMockBuilder($class)->disableOriginalConstructor();
}

/**
* Mock service in the container.
*/
protected function setServiceMock(
ContainerInterface $container,
string $serviceId,
object $mock
): void {
$containerRef = new \ReflectionObject($container);

if ($containerRef->hasProperty('services')) {
$servicesProperty = $containerRef->getProperty('services');
$servicesProperty->setAccessible(true);
$services = $servicesProperty->getValue($container);
$services[$serviceId] = $mock;
$servicesProperty->setValue($container, $services);
}
}

/**
* Builds up the environment to run the given command.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Liip\Acme\Tests\App\AppKernel;
use Liip\FunctionalTestBundle\Test\WebTestCase;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @IgnoreAnnotation("depends")
Expand Down Expand Up @@ -398,4 +400,18 @@ public function testJsonIsSuccessful(): void
'application/json'
);
}

public function testSetServiceMock(): void
{
$mockedServiceClass = RequestStack::class;
$mockedServiceName = 'request_stack';

$kernel = static::bootKernel();
$container = $kernel->getContainer();
$mock = $this->getMockBuilder('\stdClass')->getMock();

$this->assertInstanceOf($mockedServiceClass, $container->get($mockedServiceName));
$this->setServiceMock($container, $mockedServiceName, $mock);
$this->assertInstanceOf(MockObject::class, $kernel->getContainer()->get($mockedServiceName));
}
}