DefinitionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. use Symfony\Components\DependencyInjection\Definition;
  11. class DefinitionTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testConstructor()
  14. {
  15. $def = new Definition('stdClass');
  16. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  17. $def = new Definition('stdClass', array('foo'));
  18. $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
  19. }
  20. public function testSetGetConstructor()
  21. {
  22. $def = new Definition('stdClass');
  23. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConstructor('foo')), '->setConstructor() implements a fluent interface');
  24. $this->assertEquals('foo', $def->getConstructor(), '->getConstructor() returns the constructor name');
  25. }
  26. public function testSetGetClass()
  27. {
  28. $def = new Definition('stdClass');
  29. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setClass('foo')), '->setClass() implements a fluent interface');
  30. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  31. }
  32. public function testArguments()
  33. {
  34. $def = new Definition('stdClass');
  35. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setArguments(array('foo'))), '->setArguments() implements a fluent interface');
  36. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  37. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addArgument('bar')), '->addArgument() implements a fluent interface');
  38. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  39. }
  40. public function testMethodCalls()
  41. {
  42. $def = new Definition('stdClass');
  43. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), '->setMethodCalls() implements a fluent interface');
  44. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  45. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addMethodCall('bar', array('bar'))), '->addMethodCall() implements a fluent interface');
  46. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  47. }
  48. public function testSetGetFile()
  49. {
  50. $def = new Definition('stdClass');
  51. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFile('foo')), '->setFile() implements a fluent interface');
  52. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  53. }
  54. public function testSetIsShared()
  55. {
  56. $def = new Definition('stdClass');
  57. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  58. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setShared(false)), '->setShared() implements a fluent interface');
  59. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  60. }
  61. public function testSetGetConfigurator()
  62. {
  63. $def = new Definition('stdClass');
  64. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConfigurator('foo')), '->setConfigurator() implements a fluent interface');
  65. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  66. }
  67. public function testAnnotations()
  68. {
  69. $def = new Definition('stdClass');
  70. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addAnnotation('foo')), '->addAnnotation() implements a fluent interface');
  71. $this->assertEquals(array(array()), $def->getAnnotation('foo'), '->getAnnotation() returns attributes for an annotation name');
  72. $def->addAnnotation('foo', array('foo' => 'bar'));
  73. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getAnnotation('foo'), '->addAnnotation() can adds the same annotation several times');
  74. $def->addAnnotation('bar', array('bar' => 'bar'));
  75. $this->assertEquals($def->getAnnotations(), array(
  76. 'foo' => array(array(), array('foo' => 'bar')),
  77. 'bar' => array(array('bar' => 'bar')),
  78. ), '->getAnnotations() returns all annotations');
  79. }
  80. }