InterfaceInjectorTest.php 6.4 KB

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