DefinitionTest.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\DependencyInjection;
  10. require_once __DIR__.'/../../bootstrap.php';
  11. use Symfony\Components\DependencyInjection\Definition;
  12. class DefinitionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $def = new Definition('stdClass');
  17. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  18. $def = new Definition('stdClass', array('foo'));
  19. $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
  20. }
  21. public function testSetGetConstructor()
  22. {
  23. $def = new Definition('stdClass');
  24. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConstructor('foo')), '->setConstructor() implements a fluent interface');
  25. $this->assertEquals('foo', $def->getConstructor(), '->getConstructor() returns the constructor name');
  26. }
  27. public function testSetGetClass()
  28. {
  29. $def = new Definition('stdClass');
  30. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setClass('foo')), '->setClass() implements a fluent interface');
  31. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  32. }
  33. public function testArguments()
  34. {
  35. $def = new Definition('stdClass');
  36. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setArguments(array('foo'))), '->setArguments() implements a fluent interface');
  37. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  38. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addArgument('bar')), '->addArgument() implements a fluent interface');
  39. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  40. }
  41. public function testMethodCalls()
  42. {
  43. $def = new Definition('stdClass');
  44. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), '->setMethodCalls() implements a fluent interface');
  45. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  46. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addMethodCall('bar', array('bar'))), '->addMethodCall() implements a fluent interface');
  47. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  48. }
  49. public function testSetGetFile()
  50. {
  51. $def = new Definition('stdClass');
  52. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFile('foo')), '->setFile() implements a fluent interface');
  53. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  54. }
  55. public function testSetIsShared()
  56. {
  57. $def = new Definition('stdClass');
  58. $this->assertEquals(true, $def->isShared(), '->isShared() returns true by default');
  59. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setShared(false)), '->setShared() implements a fluent interface');
  60. $this->assertEquals(false, $def->isShared(), '->isShared() returns false if the instance must not be shared');
  61. }
  62. public function testSetGetConfigurator()
  63. {
  64. $def = new Definition('stdClass');
  65. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConfigurator('foo')), '->setConfigurator() implements a fluent interface');
  66. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  67. }
  68. public function testAnnotations()
  69. {
  70. $def = new Definition('stdClass');
  71. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addAnnotation('foo')), '->addAnnotation() implements a fluent interface');
  72. $this->assertEquals(array(array()), $def->getAnnotation('foo'), '->getAnnotation() returns attributes for an annotation name');
  73. $def->addAnnotation('foo', array('foo' => 'bar'));
  74. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getAnnotation('foo'), '->addAnnotation() can adds the same annotation several times');
  75. $def->addAnnotation('bar', array('bar' => 'bar'));
  76. $this->assertEquals($def->getAnnotations(), array(
  77. 'foo' => array(array(), array('foo' => 'bar')),
  78. 'bar' => array(array('bar' => 'bar')),
  79. ), '->getAnnotations() returns all annotations');
  80. }
  81. }