2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-07 02:07:59 +01:00
*
2010-03-18 07:48:03 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\DependencyInjection\Tests ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Definition ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
class DefinitionTest extends TestCase
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
{
$def = new Definition ( 'stdClass' );
$this -> assertEquals ( 'stdClass' , $def -> getClass (), '__construct() takes the class name as its first argument' );
2017-04-05 13:12:51 +01:00
$this -> assertSame ( array ( 'class' => true ), $def -> getChanges ());
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$def = new Definition ( 'stdClass' , array ( 'foo' ));
$this -> assertEquals ( array ( 'foo' ), $def -> getArguments (), '__construct() takes an optional array of arguments as its second argument' );
}
2010-03-18 07:48:03 +00:00
2014-04-13 16:18:39 +01:00
public function testSetGetFactory ()
{
2017-04-05 13:12:51 +01:00
$def = new Definition ();
2014-09-27 17:06:30 +01:00
2014-04-13 16:18:39 +01:00
$this -> assertSame ( $def , $def -> setFactory ( 'foo' ), '->setFactory() implements a fluent interface' );
$this -> assertEquals ( 'foo' , $def -> getFactory (), '->getFactory() returns the factory' );
2014-09-27 17:06:30 +01:00
$def -> setFactory ( 'Foo::bar' );
$this -> assertEquals ( array ( 'Foo' , 'bar' ), $def -> getFactory (), '->setFactory() converts string static method call to the array' );
2017-04-05 13:12:51 +01:00
$this -> assertSame ( array ( 'factory' => true ), $def -> getChanges ());
2014-04-13 16:18:39 +01:00
}
2010-05-06 12:25:53 +01:00
public function testSetGetClass ()
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> setClass ( 'foo' ), '->setClass() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $def -> getClass (), '->getClass() returns the class name' );
}
2010-03-18 07:48:03 +00:00
2014-03-31 16:58:40 +01:00
public function testSetGetDecoratedService ()
{
2015-07-31 02:41:40 +01:00
$def = new Definition ( 'stdClass' );
$this -> assertNull ( $def -> getDecoratedService ());
$def -> setDecoratedService ( 'foo' , 'foo.renamed' , 5 );
$this -> assertEquals ( array ( 'foo' , 'foo.renamed' , 5 ), $def -> getDecoratedService ());
$def -> setDecoratedService ( null );
$this -> assertNull ( $def -> getDecoratedService ());
2014-03-31 16:58:40 +01:00
$def = new Definition ( 'stdClass' );
$this -> assertNull ( $def -> getDecoratedService ());
$def -> setDecoratedService ( 'foo' , 'foo.renamed' );
2015-07-31 02:41:40 +01:00
$this -> assertEquals ( array ( 'foo' , 'foo.renamed' , 0 ), $def -> getDecoratedService ());
2014-03-31 16:58:40 +01:00
$def -> setDecoratedService ( null );
$this -> assertNull ( $def -> getDecoratedService ());
$def = new Definition ( 'stdClass' );
$def -> setDecoratedService ( 'foo' );
2015-07-31 02:41:40 +01:00
$this -> assertEquals ( array ( 'foo' , null , 0 ), $def -> getDecoratedService ());
2014-03-31 16:58:40 +01:00
$def -> setDecoratedService ( null );
$this -> assertNull ( $def -> getDecoratedService ());
$def = new Definition ( 'stdClass' );
2017-02-20 14:23:32 +00:00
if ( method_exists ( $this , 'expectException' )) {
$this -> expectException ( 'InvalidArgumentException' );
$this -> expectExceptionMessage ( 'The decorated service inner name for "foo" must be different than the service name itself.' );
} else {
$this -> setExpectedException ( 'InvalidArgumentException' , 'The decorated service inner name for "foo" must be different than the service name itself.' );
}
2014-03-31 16:58:40 +01:00
$def -> setDecoratedService ( 'foo' , 'foo' );
}
2010-05-06 12:25:53 +01:00
public function testArguments ()
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> setArguments ( array ( 'foo' )), '->setArguments() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'foo' ), $def -> getArguments (), '->getArguments() returns the arguments' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> addArgument ( 'bar' ), '->addArgument() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'foo' , 'bar' ), $def -> getArguments (), '->addArgument() adds an argument' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testMethodCalls ()
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> setMethodCalls ( array ( array ( 'foo' , array ( 'foo' )))), '->setMethodCalls() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( array ( 'foo' , array ( 'foo' ))), $def -> getMethodCalls (), '->getMethodCalls() returns the methods to call' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> addMethodCall ( 'bar' , array ( 'bar' )), '->addMethodCall() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( array ( 'foo' , array ( 'foo' )), array ( 'bar' , array ( 'bar' ))), $def -> getMethodCalls (), '->addMethodCall() adds a method to call' );
2010-10-17 15:40:32 +01:00
$this -> assertTrue ( $def -> hasMethodCall ( 'bar' ), '->hasMethodCall() returns true if first argument is a method to call registered' );
$this -> assertFalse ( $def -> hasMethodCall ( 'no_registered' ), '->hasMethodCall() returns false if first argument is not a method to call registered' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> removeMethodCall ( 'bar' ), '->removeMethodCall() implements a fluent interface' );
2010-10-17 15:40:32 +01:00
$this -> assertEquals ( array ( array ( 'foo' , array ( 'foo' ))), $def -> getMethodCalls (), '->removeMethodCall() removes a method to call' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2011-11-30 13:11:00 +00:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
2011-11-30 13:11:00 +00:00
* @ expectedExceptionMessage Method name cannot be empty .
*/
public function testExceptionOnEmptyMethodCall ()
{
$def = new Definition ( 'stdClass' );
$def -> addMethodCall ( '' );
}
2010-05-06 12:25:53 +01:00
public function testSetGetFile ()
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> setFile ( 'foo' ), '->setFile() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $def -> getFile (), '->getFile() returns the file to include' );
}
2010-03-18 07:48:03 +00:00
2015-06-15 03:41:59 +01:00
public function testSetIsShared ()
{
$def = new Definition ( 'stdClass' );
$this -> assertTrue ( $def -> isShared (), '->isShared() returns true by default' );
$this -> assertSame ( $def , $def -> setShared ( false ), '->setShared() implements a fluent interface' );
$this -> assertFalse ( $def -> isShared (), '->isShared() returns false if the instance must not be shared' );
}
2010-12-29 19:12:24 +00:00
public function testSetIsPublic ()
{
$def = new Definition ( 'stdClass' );
$this -> assertTrue ( $def -> isPublic (), '->isPublic() returns true by default' );
$this -> assertSame ( $def , $def -> setPublic ( false ), '->setPublic() implements a fluent interface' );
$this -> assertFalse ( $def -> isPublic (), '->isPublic() returns false if the instance must not be public.' );
}
2011-01-23 11:06:23 +00:00
public function testSetIsSynthetic ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isSynthetic (), '->isSynthetic() returns false by default' );
$this -> assertSame ( $def , $def -> setSynthetic ( true ), '->setSynthetic() implements a fluent interface' );
2013-02-06 20:42:01 +00:00
$this -> assertTrue ( $def -> isSynthetic (), '->isSynthetic() returns true if the service is synthetic.' );
}
2013-03-29 23:21:12 +00:00
public function testSetIsLazy ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isLazy (), '->isLazy() returns false by default' );
$this -> assertSame ( $def , $def -> setLazy ( true ), '->setLazy() implements a fluent interface' );
$this -> assertTrue ( $def -> isLazy (), '->isLazy() returns true if the service is lazy.' );
}
2011-01-26 23:14:31 +00:00
public function testSetIsAbstract ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isAbstract (), '->isAbstract() returns false by default' );
$this -> assertSame ( $def , $def -> setAbstract ( true ), '->setAbstract() implements a fluent interface' );
$this -> assertTrue ( $def -> isAbstract (), '->isAbstract() returns true if the instance must not be public.' );
}
2015-08-07 16:07:23 +01:00
public function testSetIsDeprecated ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isDeprecated (), '->isDeprecated() returns false by default' );
$this -> assertSame ( $def , $def -> setDeprecated ( true ), '->setDeprecated() implements a fluent interface' );
$this -> assertTrue ( $def -> isDeprecated (), '->isDeprecated() returns true if the instance should not be used anymore.' );
2015-08-10 13:35:12 +01:00
$this -> assertSame ( 'The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.' , $def -> getDeprecationMessage ( 'deprecated_service' ), '->getDeprecationMessage() should return a formatted message template' );
}
/**
* @ dataProvider invalidDeprecationMessageProvider
2016-09-24 10:47:20 +01:00
* @ expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
2015-08-10 13:35:12 +01:00
*/
public function testSetDeprecatedWithInvalidDeprecationTemplate ( $message )
{
$def = new Definition ( 'stdClass' );
$def -> setDeprecated ( false , $message );
}
public function invalidDeprecationMessageProvider ()
{
return array (
" With \r s " => array ( " invalid \r message %service_id% " ),
" With \n s " => array ( " invalid \n message %service_id% " ),
'With */s' => array ( 'invalid */ message %service_id%' ),
'message not containing require %service_id% variable' => array ( 'this is deprecated' ),
);
2015-08-07 16:07:23 +01:00
}
2010-05-06 12:25:53 +01:00
public function testSetGetConfigurator ()
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> setConfigurator ( 'foo' ), '->setConfigurator() implements a fluent interface' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $def -> getConfigurator (), '->getConfigurator() returns the configurator' );
}
2010-03-18 07:48:03 +00:00
2010-08-05 06:34:53 +01:00
public function testClearTags ()
2010-06-27 17:28:29 +01:00
{
$def = new Definition ( 'stdClass' );
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> clearTags (), '->clearTags() implements a fluent interface' );
2010-08-05 06:34:53 +01:00
$def -> addTag ( 'foo' , array ( 'foo' => 'bar' ));
$def -> clearTags ();
$this -> assertEquals ( array (), $def -> getTags (), '->clearTags() removes all current tags' );
2010-06-27 17:28:29 +01:00
}
2012-04-14 07:11:21 +01:00
public function testClearTag ()
{
$def = new Definition ( 'stdClass' );
$this -> assertSame ( $def , $def -> clearTags (), '->clearTags() implements a fluent interface' );
$def -> addTag ( '1foo1' , array ( 'foo1' => 'bar1' ));
$def -> addTag ( '2foo2' , array ( 'foo2' => 'bar2' ));
$def -> addTag ( '3foo3' , array ( 'foo3' => 'bar3' ));
$def -> clearTag ( '2foo2' );
$this -> assertTrue ( $def -> hasTag ( '1foo1' ));
$this -> assertFalse ( $def -> hasTag ( '2foo2' ));
$this -> assertTrue ( $def -> hasTag ( '3foo3' ));
$def -> clearTag ( '1foo1' );
$this -> assertFalse ( $def -> hasTag ( '1foo1' ));
$this -> assertTrue ( $def -> hasTag ( '3foo3' ));
}
2010-08-05 06:34:53 +01:00
public function testTags ()
2010-05-06 12:25:53 +01:00
{
$def = new Definition ( 'stdClass' );
2010-08-05 06:34:53 +01:00
$this -> assertEquals ( array (), $def -> getTag ( 'foo' ), '->getTag() returns an empty array if the tag is not defined' );
2011-01-24 08:23:15 +00:00
$this -> assertFalse ( $def -> hasTag ( 'foo' ));
2010-11-20 01:09:20 +00:00
$this -> assertSame ( $def , $def -> addTag ( 'foo' ), '->addTag() implements a fluent interface' );
2011-01-24 08:23:15 +00:00
$this -> assertTrue ( $def -> hasTag ( 'foo' ));
2010-08-05 06:34:53 +01:00
$this -> assertEquals ( array ( array ()), $def -> getTag ( 'foo' ), '->getTag() returns attributes for a tag name' );
$def -> addTag ( 'foo' , array ( 'foo' => 'bar' ));
$this -> assertEquals ( array ( array (), array ( 'foo' => 'bar' )), $def -> getTag ( 'foo' ), '->addTag() can adds the same tag several times' );
$def -> addTag ( 'bar' , array ( 'bar' => 'bar' ));
$this -> assertEquals ( $def -> getTags (), array (
2010-05-06 12:25:53 +01:00
'foo' => array ( array (), array ( 'foo' => 'bar' )),
'bar' => array ( array ( 'bar' => 'bar' )),
2010-08-05 06:34:53 +01:00
), '->getTags() returns all tags' );
2010-05-06 12:25:53 +01:00
}
2011-01-18 17:53:11 +00:00
public function testSetArgument ()
{
$def = new Definition ( 'stdClass' );
$def -> addArgument ( 'foo' );
$this -> assertSame ( array ( 'foo' ), $def -> getArguments ());
2011-04-19 22:29:10 +01:00
$this -> assertSame ( $def , $def -> replaceArgument ( 0 , 'moo' ));
2011-01-18 17:53:11 +00:00
$this -> assertSame ( array ( 'moo' ), $def -> getArguments ());
$def -> addArgument ( 'moo' );
$def
2011-04-19 22:29:10 +01:00
-> replaceArgument ( 0 , 'foo' )
-> replaceArgument ( 1 , 'bar' )
2011-01-18 17:53:11 +00:00
;
$this -> assertSame ( array ( 'foo' , 'bar' ), $def -> getArguments ());
}
2011-03-10 14:31:00 +00:00
2011-10-27 22:59:47 +01:00
/**
2013-11-25 08:44:14 +00:00
* @ expectedException \OutOfBoundsException
2011-10-27 22:59:47 +01:00
*/
public function testGetArgumentShouldCheckBounds ()
{
$def = new Definition ( 'stdClass' );
$def -> addArgument ( 'foo' );
$def -> getArgument ( 1 );
}
/**
2013-11-25 08:44:14 +00:00
* @ expectedException \OutOfBoundsException
2017-01-26 10:48:39 +00:00
* @ expectedExceptionMessage The index " 1 " is not in the range [ 0 , 0 ] .
2011-10-27 22:59:47 +01:00
*/
public function testReplaceArgumentShouldCheckBounds ()
{
$def = new Definition ( 'stdClass' );
$def -> addArgument ( 'foo' );
$def -> replaceArgument ( 1 , 'bar' );
}
2017-01-26 10:48:39 +00:00
/**
* @ expectedException \OutOfBoundsException
* @ expectedExceptionMessage Cannot replace arguments if none have been configured yet .
*/
public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds ()
{
$def = new Definition ( 'stdClass' );
$def -> replaceArgument ( 0 , 'bar' );
}
2011-03-10 14:31:00 +00:00
public function testSetGetProperties ()
{
$def = new Definition ( 'stdClass' );
$this -> assertEquals ( array (), $def -> getProperties ());
$this -> assertSame ( $def , $def -> setProperties ( array ( 'foo' => 'bar' )));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $def -> getProperties ());
}
public function testSetProperty ()
{
$def = new Definition ( 'stdClass' );
$this -> assertEquals ( array (), $def -> getProperties ());
$this -> assertSame ( $def , $def -> setProperty ( 'foo' , 'bar' ));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $def -> getProperties ());
}
2015-08-24 02:36:41 +01:00
public function testAutowired ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isAutowired ());
2017-02-25 16:12:16 +00:00
2015-08-24 02:36:41 +01:00
$def -> setAutowired ( true );
$this -> assertTrue ( $def -> isAutowired ());
2016-10-05 21:46:29 +01:00
$def -> setAutowired ( false );
$this -> assertFalse ( $def -> isAutowired ());
2015-08-24 02:36:41 +01:00
}
2017-04-05 13:12:51 +01:00
public function testChangesNoChanges ()
{
$def = new Definition ();
$this -> assertSame ( array (), $def -> getChanges ());
}
public function testGetChangesWithChanges ()
{
$def = new Definition ( 'stdClass' , array ( 'fooarg' ));
$def -> setAbstract ( true );
$def -> setAutowired ( true );
$def -> setConfigurator ( 'configuration_func' );
$def -> setDecoratedService ( null );
$def -> setDeprecated ( true );
$def -> setFactory ( 'factory_func' );
$def -> setFile ( 'foo.php' );
$def -> setLazy ( true );
$def -> setPublic ( true );
$def -> setShared ( true );
$def -> setSynthetic ( true );
// changes aren't tracked for these, class or arguments
$def -> setInstanceofConditionals ( array ());
$def -> addTag ( 'foo_tag' );
$def -> addMethodCall ( 'methodCall' );
$def -> setProperty ( 'fooprop' , true );
2017-03-31 11:45:36 +01:00
$def -> setAutoconfigured ( true );
2017-04-05 13:12:51 +01:00
$this -> assertSame ( array (
'class' => true ,
'autowired' => true ,
'configurator' => true ,
'decorated_service' => true ,
'deprecated' => true ,
'factory' => true ,
'file' => true ,
'lazy' => true ,
'public' => true ,
'shared' => true ,
2017-03-31 11:45:36 +01:00
'autoconfigured' => true ,
2017-04-05 13:12:51 +01:00
), $def -> getChanges ());
$def -> setChanges ( array ());
$this -> assertSame ( array (), $def -> getChanges ());
}
2017-03-31 11:45:36 +01:00
public function testShouldAutoconfigure ()
{
$def = new Definition ( 'stdClass' );
$this -> assertFalse ( $def -> isAutoconfigured ());
$def -> setAutoconfigured ( true );
$this -> assertTrue ( $def -> isAutoconfigured ());
}
2017-09-22 18:54:05 +01:00
public function testAddError ()
{
$def = new Definition ( 'stdClass' );
$this -> assertEmpty ( $def -> getErrors ());
$def -> addError ( 'First error' );
$def -> addError ( 'Second error' );
$this -> assertSame ( array ( 'First error' , 'Second error' ), $def -> getErrors ());
}
2010-03-18 07:48:03 +00:00
}