Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
changed compiler pass to have the api service become an alias of the …
Browse files Browse the repository at this point in the history
…mocked api service instead of changing classes
  • Loading branch information
Cas Leentfaar committed Feb 21, 2015
1 parent ad9c71b commit c262163
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
9 changes: 5 additions & 4 deletions DependencyInjection/Compiler/RegisterApiClientPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class RegisterApiClientPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$clientId = 'cl_slack.api_client';
$mockClientId = 'cl_slack.mock_api_client';

if (!$container->hasDefinition($clientId) ||
!$container->hasParameter('cl_slack.test') ||
!$container->hasParameter('cl_slack.mock_api_client.class')
!$container->hasDefinition($mockClientId) ||
!$container->hasParameter('cl_slack.test')
) {
return;
}
Expand All @@ -22,7 +23,7 @@ public function process(ContainerBuilder $container)
return;
}

$clientDefinition = $container->getDefinition($clientId);
$clientDefinition->setClass($container->getParameter('cl_slack.mock_api_client.class'));
$container->removeDefinition($clientId);
$container->setAlias($clientId, $mockClientId);
}
}
23 changes: 12 additions & 11 deletions Tests/DependencyInjection/Compiler/RegisterApiClientPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@

class RegisterApiClientPassTest extends AbstractCompilerPassTestCase
{
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new RegisterApiClientPass());
}
/**
* @test
*/
public function testApiClientIsMockedWhenTestIsTrue()
{
$collectingService = new Definition();
$this->setDefinition('cl_slack.api_client', $collectingService);
$apiClientId = 'cl_slack.api_client';
$mockApiClientId = 'cl_slack.mock_api_client';

$this->setDefinition($apiClientId, new Definition());
$this->setDefinition($mockApiClientId, new Definition());
$this->setParameter('cl_slack.test', true);
$this->setParameter('cl_slack.mock_api_client.class', 'TestClass');

$this->compile();

$this->assertContainerBuilderHasService('cl_slack.api_client', 'TestClass');
$this->assertContainerBuilderHasAlias($apiClientId, $mockApiClientId);
}

protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new RegisterApiClientPass());
}
}

0 comments on commit c262163

Please sign in to comment.