InterfaceInjectorTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\InterfaceInjector;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. class InterfaceInjectorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::addMethodCall
  17. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::hasMethodCall
  18. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::removeMethodCall
  19. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::getMethodCalls
  20. *
  21. * @dataProvider getMethodCalls
  22. *
  23. * @param string $method
  24. * @param array $arguments
  25. */
  26. public function testAddRemoveGetMethodCalls($method, array $arguments = array())
  27. {
  28. $injector = new InterfaceInjector('stdClass');
  29. $injector->addMethodCall($method, $arguments);
  30. $this->assertTrue($injector->hasMethodCall($method), '->hasMethodCall() returns true for methods that were added on InterfaceInjector');
  31. $methodCalls = $injector->getMethodCalls();
  32. $this->assertEquals(1, count($methodCalls), '->getMethodCalls() returns array, where each entry is a method call');
  33. $this->assertEquals(array($method, $arguments), $methodCalls[0], '->getMethodCalls() has all methods added to InterfaceInjector instance');
  34. $injector->removeMethodCall($method);
  35. $this->assertFalse($injector->hasMethodCall($method), '->removeMethodClass() deletes the method call from InterfaceInjector');
  36. }
  37. /**
  38. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::processDefinition
  39. *
  40. * @dataProvider getInjectorsAndDefinitions
  41. *
  42. * @param InterfaceInjector $injector
  43. * @param Definition $definition
  44. * @param int $expectedMethodsCount
  45. */
  46. public function testProcessDefinition(InterfaceInjector $injector, Definition $definition)
  47. {
  48. $injector->processDefinition($definition);
  49. }
  50. /**
  51. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::supports
  52. *
  53. * @dataProvider getInjectorsAndClasses
  54. *
  55. * @param InterfaceInjector $injector
  56. * @param string $class
  57. * @param string $expectedResult
  58. */
  59. public function testSupports(InterfaceInjector $injector, $class, $expectedResult)
  60. {
  61. $this->assertEquals($expectedResult, $injector->supports($class), '->supports() must return true if injector is to be used on a class, false otherwise');
  62. }
  63. /**
  64. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::processDefinition
  65. */
  66. public function testProcessesDefinitionOnlyOnce()
  67. {
  68. $injector = new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service');
  69. $injector->addMethodCall('method');
  70. $definition = $this->getMockDefinition('Symfony\Tests\Component\DependencyInjection\Service', 1);
  71. $injector->processDefinition($definition);
  72. $injector->processDefinition($definition);
  73. }
  74. /**
  75. * @covers Symfony\Component\DependencyInjection\InterfaceInjector::merge
  76. */
  77. public function testMerge()
  78. {
  79. $injector1 = new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service');
  80. $injector1->addMethodCall('method_one');
  81. $injector2 = new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service');
  82. $injector2->addMethodCall('method_two');
  83. $injector1->merge($injector2);
  84. $methodCalls = $injector1->getMethodCalls();
  85. $this->assertEquals(2, count($methodCalls));
  86. $this->assertEquals(array(
  87. array('method_one', array()),
  88. array('method_two', array()),
  89. ), $methodCalls);
  90. }
  91. /**
  92. * @expectedException Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  93. */
  94. public function testSupportsThrowsExceptionOnInvalidArgument()
  95. {
  96. $injector = new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service');
  97. $injector->supports(array());
  98. }
  99. public function getMethodCalls()
  100. {
  101. return array(
  102. array('method', array()),
  103. array('method2', array('one', 'two')),
  104. array('method3', array('single')),
  105. );
  106. }
  107. public function getInjectorsAndDefinitions()
  108. {
  109. $injector = new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service');
  110. $injector->addMethodCall('method');
  111. $injector->addMethodCall('method');
  112. $injector->addMethodCall('method');
  113. $injector->addMethodCall('method');
  114. $definition1 = $this->getMockDefinition('stdClass', 0);
  115. $definition2 = $this->getMockDefinition('Symfony\Tests\Component\DependencyInjection\Service', 4);
  116. return array(
  117. array($injector, $definition1),
  118. array($injector, $definition2),
  119. );
  120. }
  121. public function getInjectorsAndClasses()
  122. {
  123. return array(
  124. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service'), 'Symfony\Tests\Component\DependencyInjection\Service', true),
  125. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\SubService'), 'Symfony\Tests\Component\DependencyInjection\Service', false),
  126. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\Service'), 'Symfony\Tests\Component\DependencyInjection\SubService', true),
  127. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\SubService'), 'Symfony\Tests\Component\DependencyInjection\SubService', true),
  128. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\FooInterface'), 'Symfony\Tests\Component\DependencyInjection\SubService', true),
  129. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\FooInterface'), 'Symfony\Tests\Component\DependencyInjection\Service', false),
  130. array(new InterfaceInjector('Symfony\Tests\Component\DependencyInjection\FooInterface'), 'Symfony\Tests\Component\DependencyInjection\ServiceWithConstructor', false),
  131. );
  132. }
  133. /**
  134. * @param string $class
  135. * @param int $methodCount
  136. * @return Symfony\Component\DependencyInjection\Definition
  137. */
  138. private function getMockDefinition($class, $methodCount)
  139. {
  140. $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition');
  141. $definition->expects($this->once())
  142. ->method('getClass')
  143. ->will($this->returnValue($class))
  144. ;
  145. $definition->expects($this->exactly($methodCount))
  146. ->method('addMethodCall')
  147. ;
  148. return $definition;
  149. }
  150. }
  151. class ServiceWithConstructor { public function __construct(\DateTime $required) {} }
  152. class Service {}
  153. class SubService extends Service implements FooInterface {}
  154. interface FooInterface {}