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

Commit

Permalink
improved compiler test
Browse files Browse the repository at this point in the history
  • Loading branch information
Cas Leentfaar committed Feb 21, 2015
1 parent c262163 commit 66cd5fd
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions Tests/DependencyInjection/Compiler/RegisterApiClientPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,58 @@

use CL\Bundle\SlackBundle\DependencyInjection\Compiler\RegisterApiClientPass;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasAliasConstraint;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class RegisterApiClientPassTest extends AbstractCompilerPassTestCase
{
public function testApiClientIsMockedWhenTestIsTrue()
const API_CLIENT_ID = 'cl_slack.api_client';
const MOCK_API_CLIENT_ID = 'cl_slack.mock_api_client';

private $apiClientClass;
private $mockApiClientClass;

protected function setUp()
{
parent::setUp();

$this->apiClientClass = 'CL\Slack\Transport\ApiClient';
$this->mockApiClientClass = 'CL\Slack\Test\Transport\MockApiClient';

$this->setDefinition(self::API_CLIENT_ID, new Definition($this->apiClientClass));
$this->setDefinition(self::MOCK_API_CLIENT_ID, new Definition($this->mockApiClientClass));

}

public function testApiClientIsNotMockedWhenTestIsFalse()
{
$apiClientId = 'cl_slack.api_client';
$mockApiClientId = 'cl_slack.mock_api_client';
$this->setParameter('cl_slack.test', false);

$this->setDefinition($apiClientId, new Definition());
$this->setDefinition($mockApiClientId, new Definition());
$this->compile();

$this->assertContainerBuilderNotHasAlias(self::API_CLIENT_ID, $this->mockApiClientClass);
$this->assertContainerBuilderHasService(self::API_CLIENT_ID, $this->apiClientClass);
$this->assertContainerBuilderHasService(self::MOCK_API_CLIENT_ID, $this->mockApiClientClass);
}

public function testApiClientIsMockedWhenTestIsTrue()
{
$this->setParameter('cl_slack.test', true);

$this->compile();

$this->assertContainerBuilderHasAlias($apiClientId, $mockApiClientId);
$this->assertContainerBuilderHasAlias(self::API_CLIENT_ID, self::MOCK_API_CLIENT_ID);
$this->assertArrayNotHasKey(self::API_CLIENT_ID, $this->container->getDefinitions());
$this->assertContainerBuilderHasService(self::MOCK_API_CLIENT_ID, $this->mockApiClientClass);
}

protected function assertContainerBuilderNotHasAlias($aliasId, $expectedServiceId)
{
self::assertThat(
$this->container,
new \PHPUnit_Framework_Constraint_Not(new ContainerBuilderHasAliasConstraint($aliasId, $expectedServiceId))
);
}

protected function registerCompilerPass(ContainerBuilder $container)
Expand Down

0 comments on commit 66cd5fd

Please sign in to comment.