|
15 | 15 |
|
16 | 16 | use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
|
17 | 17 | use Liip\Acme\Tests\App\AppKernel;
|
| 18 | +use Liip\Acme\Tests\App\Service\DependencyService; |
| 19 | +use Liip\Acme\Tests\App\Service\Service; |
18 | 20 | use Liip\FunctionalTestBundle\Test\WebTestCase;
|
19 | 21 | use PHPUnit\Framework\AssertionFailedError;
|
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use Symfony\Component\HttpFoundation\RequestStack; |
20 | 24 |
|
21 | 25 | /**
|
22 | 26 | * @IgnoreAnnotation("depends")
|
@@ -398,4 +402,92 @@ public function testJsonIsSuccessful(): void
|
398 | 402 | 'application/json'
|
399 | 403 | );
|
400 | 404 | }
|
| 405 | + |
| 406 | + public function testSetServiceMockCommand(): void |
| 407 | + { |
| 408 | + $mockedServiceClass = RequestStack::class; |
| 409 | + $mockedServiceName = 'request_stack'; |
| 410 | + |
| 411 | + $kernel = static::bootKernel(); |
| 412 | + $container = $kernel->getContainer(); |
| 413 | + $mock = $this->getMockBuilder('\stdClass')->getMock(); |
| 414 | + |
| 415 | + $this->assertInstanceOf($mockedServiceClass, $container->get($mockedServiceName)); |
| 416 | + $this->setServiceMock($container, $mockedServiceName, $mock); |
| 417 | + $this->assertInstanceOf(MockObject::class, $kernel->getContainer()->get($mockedServiceName)); |
| 418 | + } |
| 419 | + |
| 420 | + public static function provideSetServiceMockClientData(): array |
| 421 | + { |
| 422 | + return [ |
| 423 | + 'no mock' => [ |
| 424 | + 'dependency service result', |
| 425 | + null, |
| 426 | + ], |
| 427 | + 'mock service dependency' => [ |
| 428 | + 'mocked dependency service result', |
| 429 | + DependencyService::class, |
| 430 | + ], |
| 431 | + 'mock controller dependency' => [ |
| 432 | + 'mocked service result', |
| 433 | + Service::class, |
| 434 | + ], |
| 435 | + ]; |
| 436 | + } |
| 437 | + |
| 438 | + /** |
| 439 | + * @dataProvider provideSetServiceMockClientData |
| 440 | + */ |
| 441 | + public function testSetServiceMockClient(string $expectedOutput, ?string $mockedServiceName): void |
| 442 | + { |
| 443 | + $client = static::createClient(); |
| 444 | + |
| 445 | + // mock the service |
| 446 | + if ($mockedServiceName) { |
| 447 | + $mock = $this->getServiceMockBuilder($mockedServiceName)->getMock(); |
| 448 | + $mock->expects($this->once())->method('get')->willReturn($expectedOutput); |
| 449 | + $this->setServiceMock(static::$kernel->getContainer(), $mockedServiceName, $mock); |
| 450 | + } |
| 451 | + |
| 452 | + $client->request('GET', '/service'); |
| 453 | + $this->assertSame($expectedOutput, $client->getResponse()->getContent()); |
| 454 | + } |
| 455 | + |
| 456 | + public static function provideSetServiceMockKernelRebootData(): array |
| 457 | + { |
| 458 | + return [ |
| 459 | + // do a kernel reboot, expects 'get' method call on mock, expected output |
| 460 | + 'no kernel reboot' => [false, false, 'dependency service result'], |
| 461 | + 'reboot kernel' => [true, true, 'mocked result'], |
| 462 | + ]; |
| 463 | + } |
| 464 | + |
| 465 | + /** |
| 466 | + * @dataProvider provideSetServiceMockKernelRebootData |
| 467 | + */ |
| 468 | + public function testSetServiceMockKernelReboot( |
| 469 | + bool $rebootKernel, |
| 470 | + bool $expectedMethodCall, |
| 471 | + string $expectedOutput |
| 472 | + ): void { |
| 473 | + // use the real service |
| 474 | + $client = static::createClient(); |
| 475 | + $client->request('GET', '/service'); |
| 476 | + $this->assertSame('dependency service result', $client->getResponse()->getContent()); |
| 477 | + |
| 478 | + if ($rebootKernel) { |
| 479 | + static::ensureKernelShutdown(); |
| 480 | + $client = static::createClient(); |
| 481 | + } |
| 482 | + |
| 483 | + // mock the service |
| 484 | + $mock = $this->getServiceMockBuilder(Service::class)->getMock(); |
| 485 | + $mock->expects($expectedMethodCall ? $this->once() : $this->never()) |
| 486 | + ->method('get') |
| 487 | + ->willReturn('mocked result'); |
| 488 | + $this->setServiceMock(static::$kernel->getContainer(), Service::class, $mock); |
| 489 | + |
| 490 | + $client->request('GET', '/service'); |
| 491 | + $this->assertSame($expectedOutput, $client->getResponse()->getContent()); |
| 492 | + } |
401 | 493 | }
|
0 commit comments