Skip to content

Commit 655236a

Browse files
authored
Merge pull request #278 from norkunas/compiler-pass-unit-tests
Add unit tests for compiler passes
2 parents 79f8b36 + eb1a423 commit 655236a

File tree

4 files changed

+205
-5
lines changed

4 files changed

+205
-5
lines changed

DependencyInjection/Compiler/AddProvidersPass.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
*/
2323
class AddProvidersPass implements CompilerPassInterface
2424
{
25-
/**
26-
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
27-
*/
28-
protected $container;
29-
3025
/**
3126
* Get all providers based on their tag (`bazinga_geocoder.provider`) and
3227
* register them.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Bazinga\GeocoderBundle\Tests\DependencyInjection\Compiler;
14+
15+
use Bazinga\GeocoderBundle\DependencyInjection\Compiler\AddProvidersPass;
16+
use Geocoder\Provider\BingMaps\BingMaps;
17+
use Geocoder\ProviderAggregator;
18+
use Http\Client\Curl\Client;
19+
use PHPUnit\Framework\TestCase;
20+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
21+
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Definition;
23+
24+
class AddProvidersPassTest extends TestCase
25+
{
26+
use SetUpTearDownTrait;
27+
28+
/**
29+
* @var AddProvidersPass
30+
*/
31+
private $compilerPass;
32+
33+
protected function doSetUp()
34+
{
35+
$this->compilerPass = new AddProvidersPass();
36+
}
37+
38+
public function testRegistersProviders()
39+
{
40+
$containerBuilder = new ContainerBuilder();
41+
$containerBuilder->setDefinition(ProviderAggregator::class, new Definition(ProviderAggregator::class));
42+
43+
$bing = $containerBuilder->setDefinition('bing_maps', new Definition(BingMaps::class, [new Client(), 'apikey']));
44+
$bing->addTag('bazinga_geocoder.provider');
45+
46+
$this->compilerPass->process($containerBuilder);
47+
48+
/** @var ProviderAggregator $providerAggregator */
49+
$providerAggregator = $containerBuilder->get(ProviderAggregator::class);
50+
$providers = $providerAggregator->getProviders();
51+
52+
$this->assertArrayHasKey('bing_maps', $providers);
53+
$this->assertInstanceOf(BingMaps::class, $providers['bing_maps']);
54+
}
55+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Bazinga\GeocoderBundle\Tests\DependencyInjection\Compiler;
14+
15+
use Bazinga\GeocoderBundle\DependencyInjection\Compiler\FactoryValidatorPass;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
20+
21+
class FactoryValidatorPassTest extends TestCase
22+
{
23+
use SetUpTearDownTrait;
24+
25+
/**
26+
* @var FactoryValidatorPass
27+
*/
28+
private $compilerPass;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $factoryId;
34+
35+
protected function doSetUp()
36+
{
37+
$this->compilerPass = new FactoryValidatorPass();
38+
$this->factoryId = 'dummy_factory_id';
39+
$this->compilerPass::addFactoryServiceId($this->factoryId);
40+
}
41+
42+
protected function doTearDown()
43+
{
44+
$reflection = new \ReflectionObject($this->compilerPass);
45+
$prop = $reflection->getProperty('factoryServiceIds');
46+
$prop->setAccessible(true);
47+
$prop->setValue([]);
48+
}
49+
50+
public function testProcessThrows()
51+
{
52+
$this->expectException(ServiceNotFoundException::class);
53+
$this->expectExceptionMessage("Factory with ID \"$this->factoryId\" could not be found");
54+
55+
$container = $this->createMock(ContainerBuilder::class);
56+
$container->expects($this->once())
57+
->method('hasAlias')
58+
->with($this->factoryId)
59+
->willReturn(false);
60+
$container->expects($this->once())
61+
->method('hasDefinition')
62+
->with($this->factoryId)
63+
->willReturn(false);
64+
65+
$this->compilerPass->process($container);
66+
}
67+
68+
public function testProcessDoesntThrowIfAliasExists()
69+
{
70+
$container = $this->createMock(ContainerBuilder::class);
71+
$container->expects($this->once())
72+
->method('hasAlias')
73+
->with($this->factoryId)
74+
->willReturn(true);
75+
$container->expects($this->never())
76+
->method('hasDefinition')
77+
->with($this->factoryId)
78+
->willReturn(false);
79+
80+
$this->compilerPass->process($container);
81+
}
82+
83+
public function testProcessDoesntThrowIfDefinitionExists()
84+
{
85+
$container = $this->createMock(ContainerBuilder::class);
86+
$container->expects($this->once())
87+
->method('hasAlias')
88+
->with($this->factoryId)
89+
->willReturn(false);
90+
$container->expects($this->once())
91+
->method('hasDefinition')
92+
->with($this->factoryId)
93+
->willReturn(true);
94+
95+
$this->compilerPass->process($container);
96+
}
97+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Bazinga\GeocoderBundle\Tests\DependencyInjection\Compiler;
14+
15+
use Bazinga\GeocoderBundle\DataCollector\GeocoderDataCollector;
16+
use Bazinga\GeocoderBundle\DependencyInjection\Compiler\ProfilerPass;
17+
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
class ProfilerPassTest extends TestCase
25+
{
26+
use SetUpTearDownTrait;
27+
28+
/**
29+
* @var ProfilerPass
30+
*/
31+
private $compilerPass;
32+
33+
protected function doSetUp()
34+
{
35+
$this->compilerPass = new ProfilerPass();
36+
}
37+
38+
public function testRegistersProviders()
39+
{
40+
$geocoderDataCollectorDefinition = new Definition(GeocoderDataCollector::class);
41+
42+
$containerBuilder = new ContainerBuilder();
43+
$containerBuilder->setDefinition(GeocoderDataCollector::class, $geocoderDataCollectorDefinition);
44+
45+
$bing = $containerBuilder->setDefinition('geocoder_profiling', new Definition(ProfilingPlugin::class, ['provider_id']));
46+
$bing->addTag('bazinga_geocoder.profiling_plugin');
47+
48+
$this->compilerPass->process($containerBuilder);
49+
50+
$this->assertTrue($geocoderDataCollectorDefinition->hasMethodCall('addInstance'));
51+
$this->assertInstanceOf(Reference::class, $geocoderDataCollectorDefinition->getMethodCalls()[0][1][0]);
52+
}
53+
}

0 commit comments

Comments
 (0)