/sites/all/modules/service_container/modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016 · PHP · 330 lines · 203 code · 39 blank · 88 comment · 0 complexity · 844f2b5942f4a519430cd1ac7d960020 MD5 · raw file

  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\Component\DependencyInjection\Tests;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. class DefinitionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Symfony\Component\DependencyInjection\Definition::__construct
  16. */
  17. public function testConstructor()
  18. {
  19. $def = new Definition('stdClass');
  20. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  21. $def = new Definition('stdClass', array('foo'));
  22. $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
  23. }
  24. /**
  25. * @covers Symfony\Component\DependencyInjection\Definition::setFactory
  26. * @covers Symfony\Component\DependencyInjection\Definition::getFactory
  27. */
  28. public function testSetGetFactory()
  29. {
  30. $def = new Definition('stdClass');
  31. $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
  32. $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
  33. $def->setFactory('Foo::bar');
  34. $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
  35. }
  36. /**
  37. * @covers Symfony\Component\DependencyInjection\Definition::setClass
  38. * @covers Symfony\Component\DependencyInjection\Definition::getClass
  39. */
  40. public function testSetGetClass()
  41. {
  42. $def = new Definition('stdClass');
  43. $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
  44. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  45. }
  46. public function testSetGetDecoratedService()
  47. {
  48. $def = new Definition('stdClass');
  49. $this->assertNull($def->getDecoratedService());
  50. $def->setDecoratedService('foo', 'foo.renamed');
  51. $this->assertEquals(array('foo', 'foo.renamed'), $def->getDecoratedService());
  52. $def->setDecoratedService(null);
  53. $this->assertNull($def->getDecoratedService());
  54. $def = new Definition('stdClass');
  55. $def->setDecoratedService('foo');
  56. $this->assertEquals(array('foo', null), $def->getDecoratedService());
  57. $def->setDecoratedService(null);
  58. $this->assertNull($def->getDecoratedService());
  59. $def = new Definition('stdClass');
  60. $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
  61. $def->setDecoratedService('foo', 'foo');
  62. }
  63. /**
  64. * @covers Symfony\Component\DependencyInjection\Definition::setArguments
  65. * @covers Symfony\Component\DependencyInjection\Definition::getArguments
  66. * @covers Symfony\Component\DependencyInjection\Definition::addArgument
  67. */
  68. public function testArguments()
  69. {
  70. $def = new Definition('stdClass');
  71. $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
  72. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  73. $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
  74. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  75. }
  76. /**
  77. * @covers Symfony\Component\DependencyInjection\Definition::setMethodCalls
  78. * @covers Symfony\Component\DependencyInjection\Definition::addMethodCall
  79. * @covers Symfony\Component\DependencyInjection\Definition::hasMethodCall
  80. * @covers Symfony\Component\DependencyInjection\Definition::removeMethodCall
  81. */
  82. public function testMethodCalls()
  83. {
  84. $def = new Definition('stdClass');
  85. $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
  86. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  87. $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
  88. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  89. $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
  90. $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
  91. $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
  92. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
  93. }
  94. /**
  95. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  96. * @expectedExceptionMessage Method name cannot be empty.
  97. */
  98. public function testExceptionOnEmptyMethodCall()
  99. {
  100. $def = new Definition('stdClass');
  101. $def->addMethodCall('');
  102. }
  103. /**
  104. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  105. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  106. */
  107. public function testSetGetFile()
  108. {
  109. $def = new Definition('stdClass');
  110. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  111. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  112. }
  113. /**
  114. * @covers Symfony\Component\DependencyInjection\Definition::setScope
  115. * @covers Symfony\Component\DependencyInjection\Definition::getScope
  116. */
  117. public function testSetGetScope()
  118. {
  119. $def = new Definition('stdClass');
  120. $this->assertEquals('container', $def->getScope());
  121. $this->assertSame($def, $def->setScope('foo'));
  122. $this->assertEquals('foo', $def->getScope());
  123. }
  124. /**
  125. * @covers Symfony\Component\DependencyInjection\Definition::setPublic
  126. * @covers Symfony\Component\DependencyInjection\Definition::isPublic
  127. */
  128. public function testSetIsPublic()
  129. {
  130. $def = new Definition('stdClass');
  131. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  132. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  133. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  134. }
  135. /**
  136. * @covers Symfony\Component\DependencyInjection\Definition::setSynthetic
  137. * @covers Symfony\Component\DependencyInjection\Definition::isSynthetic
  138. */
  139. public function testSetIsSynthetic()
  140. {
  141. $def = new Definition('stdClass');
  142. $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
  143. $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
  144. $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
  145. }
  146. /**
  147. * @covers Symfony\Component\DependencyInjection\Definition::setSynchronized
  148. * @covers Symfony\Component\DependencyInjection\Definition::isSynchronized
  149. * @group legacy
  150. */
  151. public function testLegacySetIsSynchronized()
  152. {
  153. $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
  154. $def = new Definition('stdClass');
  155. $this->assertFalse($def->isSynchronized(), '->isSynchronized() returns false by default');
  156. $this->assertSame($def, $def->setSynchronized(true), '->setSynchronized() implements a fluent interface');
  157. $this->assertTrue($def->isSynchronized(), '->isSynchronized() returns true if the service is synchronized.');
  158. }
  159. /**
  160. * @covers Symfony\Component\DependencyInjection\Definition::setLazy
  161. * @covers Symfony\Component\DependencyInjection\Definition::isLazy
  162. */
  163. public function testSetIsLazy()
  164. {
  165. $def = new Definition('stdClass');
  166. $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
  167. $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
  168. $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
  169. }
  170. /**
  171. * @covers Symfony\Component\DependencyInjection\Definition::setAbstract
  172. * @covers Symfony\Component\DependencyInjection\Definition::isAbstract
  173. */
  174. public function testSetIsAbstract()
  175. {
  176. $def = new Definition('stdClass');
  177. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  178. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  179. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  180. }
  181. /**
  182. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  183. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  184. */
  185. public function testSetGetConfigurator()
  186. {
  187. $def = new Definition('stdClass');
  188. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  189. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  190. }
  191. /**
  192. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  193. */
  194. public function testClearTags()
  195. {
  196. $def = new Definition('stdClass');
  197. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  198. $def->addTag('foo', array('foo' => 'bar'));
  199. $def->clearTags();
  200. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  201. }
  202. /**
  203. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  204. */
  205. public function testClearTag()
  206. {
  207. $def = new Definition('stdClass');
  208. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  209. $def->addTag('1foo1', array('foo1' => 'bar1'));
  210. $def->addTag('2foo2', array('foo2' => 'bar2'));
  211. $def->addTag('3foo3', array('foo3' => 'bar3'));
  212. $def->clearTag('2foo2');
  213. $this->assertTrue($def->hasTag('1foo1'));
  214. $this->assertFalse($def->hasTag('2foo2'));
  215. $this->assertTrue($def->hasTag('3foo3'));
  216. $def->clearTag('1foo1');
  217. $this->assertFalse($def->hasTag('1foo1'));
  218. $this->assertTrue($def->hasTag('3foo3'));
  219. }
  220. /**
  221. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  222. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  223. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  224. * @covers Symfony\Component\DependencyInjection\Definition::hasTag
  225. */
  226. public function testTags()
  227. {
  228. $def = new Definition('stdClass');
  229. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  230. $this->assertFalse($def->hasTag('foo'));
  231. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  232. $this->assertTrue($def->hasTag('foo'));
  233. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  234. $def->addTag('foo', array('foo' => 'bar'));
  235. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  236. $def->addTag('bar', array('bar' => 'bar'));
  237. $this->assertEquals($def->getTags(), array(
  238. 'foo' => array(array(), array('foo' => 'bar')),
  239. 'bar' => array(array('bar' => 'bar')),
  240. ), '->getTags() returns all tags');
  241. }
  242. /**
  243. * @covers Symfony\Component\DependencyInjection\Definition::replaceArgument
  244. */
  245. public function testSetArgument()
  246. {
  247. $def = new Definition('stdClass');
  248. $def->addArgument('foo');
  249. $this->assertSame(array('foo'), $def->getArguments());
  250. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  251. $this->assertSame(array('moo'), $def->getArguments());
  252. $def->addArgument('moo');
  253. $def
  254. ->replaceArgument(0, 'foo')
  255. ->replaceArgument(1, 'bar')
  256. ;
  257. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  258. }
  259. /**
  260. * @expectedException \OutOfBoundsException
  261. */
  262. public function testGetArgumentShouldCheckBounds()
  263. {
  264. $def = new Definition('stdClass');
  265. $def->addArgument('foo');
  266. $def->getArgument(1);
  267. }
  268. /**
  269. * @expectedException \OutOfBoundsException
  270. */
  271. public function testReplaceArgumentShouldCheckBounds()
  272. {
  273. $def = new Definition('stdClass');
  274. $def->addArgument('foo');
  275. $def->replaceArgument(1, 'bar');
  276. }
  277. public function testSetGetProperties()
  278. {
  279. $def = new Definition('stdClass');
  280. $this->assertEquals(array(), $def->getProperties());
  281. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  282. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  283. }
  284. public function testSetProperty()
  285. {
  286. $def = new Definition('stdClass');
  287. $this->assertEquals(array(), $def->getProperties());
  288. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  289. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  290. }
  291. }