This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/DependencyInjection/DefinitionDecoratorTest.php
Jeremy Mikola 80f0b980ba [DependencyInjection] Fix DefinitionDecorator::getArgument() for replacements
While Definition::getArgument() could be used to fetch replaced values, it relied upon bad comparison logic (e.g. "index_1" > 1). Additionally, storing original arguments and replacements in the same array makes Definition::getArguments()'s bounds check unreliable. A single argument and its replacement would count twice, allowing getArgument(2) to pass the bounds check and result in an array index error.

With this new method, fetching of replacement arguments is more straightforward and bounds checking functions as it should.
2011-10-27 18:03:51 -04:00

111 lines
3.1 KiB
PHP

<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Tests\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
class DefinitionDecoratorTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$def = new DefinitionDecorator('foo');
$this->assertEquals('foo', $def->getParent());
$this->assertEquals(array(), $def->getChanges());
}
/**
* @dataProvider getPropertyTests
*/
public function testSetProperty($property, $changeKey)
{
$def = new DefinitionDecorator('foo');
$getter = 'get'.ucfirst($property);
$setter = 'set'.ucfirst($property);
$this->assertNull($def->$getter());
$this->assertSame($def, $def->$setter('foo'));
$this->assertEquals('foo', $def->$getter());
$this->assertEquals(array($changeKey => true), $def->getChanges());
}
public function getPropertyTests()
{
return array(
array('class', 'class'),
array('factoryClass', 'factory_class'),
array('factoryMethod', 'factory_method'),
array('factoryService', 'factory_service'),
array('configurator', 'configurator'),
array('file', 'file'),
);
}
public function testSetPublic()
{
$def = new DefinitionDecorator('foo');
$this->assertTrue($def->isPublic());
$this->assertSame($def, $def->setPublic(false));
$this->assertFalse($def->isPublic());
$this->assertEquals(array('public' => true), $def->getChanges());
}
public function testSetArgument()
{
$def = new DefinitionDecorator('foo');
$this->assertEquals(array(), $def->getArguments());
$this->assertSame($def, $def->replaceArgument(0, 'foo'));
$this->assertEquals(array('index_0' => 'foo'), $def->getArguments());
}
/**
* @expectedException InvalidArgumentException
*/
public function testReplaceArgumentShouldRequireIntegerIndex()
{
$def = new DefinitionDecorator('foo');
$def->replaceArgument('0', 'foo');
}
public function testReplaceArgument()
{
$def = new DefinitionDecorator('foo');
$def->setArguments(array(0 => 'foo', 1 => 'bar'));
$this->assertEquals('foo', $def->getArgument(0));
$this->assertEquals('bar', $def->getArgument(1));
$this->assertSame($def, $def->replaceArgument(1, 'baz'));
$this->assertEquals('foo', $def->getArgument(0));
$this->assertEquals('baz', $def->getArgument(1));
$this->assertEquals(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
}
/**
* @expectedException OutOfBoundsException
*/
public function testGetArgumentShouldCheckBounds()
{
$def = new DefinitionDecorator('foo');
$def->setArguments(array(0 => 'foo'));
$def->replaceArgument(0, 'foo');
$def->getArgument(1);
}
}