2011-03-08 03:10:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-08 14:43:54 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-03-08 03:10:01 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2011-03-08 03:10:01 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class ProfilerPassTest extends TestCase
|
2011-03-08 03:10:01 +00:00
|
|
|
{
|
2014-09-29 16:33:55 +01:00
|
|
|
private $profilerDefinition;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->profilerDefinition = new Definition('ProfilerClass');
|
|
|
|
}
|
|
|
|
|
2011-03-08 03:10:01 +00:00
|
|
|
/**
|
|
|
|
* Tests that collectors that specify a template but no "id" will throw
|
2014-12-21 17:00:50 +00:00
|
|
|
* an exception (both are needed if the template is specified).
|
|
|
|
*
|
|
|
|
* Thus, a fully-valid tag looks something like this:
|
2011-03-08 03:10:01 +00:00
|
|
|
*
|
2011-04-04 10:10:56 +01:00
|
|
|
* <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" />
|
2011-03-08 03:10:01 +00:00
|
|
|
*/
|
|
|
|
public function testTemplateNoIdThrowsException()
|
|
|
|
{
|
|
|
|
// one service, with a template key, but no id
|
|
|
|
$services = array(
|
|
|
|
'my_collector_service' => array(0 => array('template' => 'foo')),
|
|
|
|
);
|
|
|
|
|
2014-09-29 16:33:55 +01:00
|
|
|
$builder = $this->createContainerMock($services);
|
2011-03-08 03:10:01 +00:00
|
|
|
|
2017-02-20 14:23:32 +00:00
|
|
|
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
|
2011-03-08 03:10:01 +00:00
|
|
|
|
|
|
|
$profilerPass = new ProfilerPass();
|
|
|
|
$profilerPass->process($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidCollector()
|
|
|
|
{
|
|
|
|
// one service, with a template key, but no id
|
|
|
|
$services = array(
|
|
|
|
'my_collector_service' => array(0 => array('template' => 'foo', 'id' => 'my_collector')),
|
|
|
|
);
|
|
|
|
|
2014-09-29 16:33:55 +01:00
|
|
|
$container = $this->createContainerMock($services);
|
2011-03-08 03:10:01 +00:00
|
|
|
|
|
|
|
// fake the getDefinition() to return a Profiler definition
|
|
|
|
$container->expects($this->atLeastOnce())
|
2014-09-29 16:33:55 +01:00
|
|
|
->method('getDefinition');
|
2011-03-08 03:10:01 +00:00
|
|
|
|
|
|
|
// assert that the data_collector.templates parameter should be set
|
|
|
|
$container->expects($this->once())
|
|
|
|
->method('setParameter')
|
|
|
|
->with('data_collector.templates', array('my_collector_service' => array('my_collector', 'foo')));
|
|
|
|
|
|
|
|
$profilerPass = new ProfilerPass();
|
|
|
|
$profilerPass->process($container);
|
|
|
|
|
|
|
|
// grab the method calls off of the "profiler" definition
|
2014-09-29 16:33:55 +01:00
|
|
|
$methodCalls = $this->profilerDefinition->getMethodCalls();
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $methodCalls);
|
2011-03-08 03:10:01 +00:00
|
|
|
$this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call
|
|
|
|
}
|
2014-09-29 16:33:55 +01:00
|
|
|
|
|
|
|
private function createContainerMock($services)
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'setParameter'))->getMock();
|
2014-09-29 16:33:55 +01:00
|
|
|
$container->expects($this->any())
|
|
|
|
->method('hasDefinition')
|
|
|
|
->with($this->equalTo('profiler'))
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$container->expects($this->any())
|
|
|
|
->method('getDefinition')
|
|
|
|
->will($this->returnValue($this->profilerDefinition));
|
|
|
|
$container->expects($this->atLeastOnce())
|
|
|
|
->method('findTaggedServiceIds')
|
|
|
|
->will($this->returnValue($services));
|
|
|
|
|
|
|
|
return $container;
|
|
|
|
}
|
2011-03-08 03:10:01 +00:00
|
|
|
}
|