PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/zendframework/zendframework/tests/ZendTest/Di/ServiceLocator/GeneratorTest.php

https://bitbucket.org/pcelta/zf2
PHP | 395 lines | 337 code | 35 blank | 23 comment | 31 complexity | 86327bb136ae8c3252dbe792f4082f09 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Di
  9. */
  10. namespace ZendTest\Di\ServiceLocator;
  11. use Zend\Di\Di;
  12. use Zend\Di\Config;
  13. use Zend\Di\ServiceLocator\Generator as ContainerGenerator;
  14. use Zend\Di\Definition\BuilderDefinition as Definition;
  15. use Zend\Di\Definition\Builder;
  16. use PHPUnit_Framework_TestCase as TestCase;
  17. class GeneratorTest extends TestCase
  18. {
  19. protected $tmpFile = false;
  20. /**
  21. * @var \Zend\Di\Di
  22. */
  23. protected $di = null;
  24. public function setUp()
  25. {
  26. $this->tmpFile = false;
  27. $this->di = new Di;
  28. }
  29. public function tearDown()
  30. {
  31. if ($this->tmpFile) {
  32. unlink($this->tmpFile);
  33. $this->tmpFile = false;
  34. }
  35. }
  36. public function getTmpFile()
  37. {
  38. $this->tmpFile = tempnam(sys_get_temp_dir(), 'zdi');
  39. return $this->tmpFile;
  40. }
  41. public function createDefinitions()
  42. {
  43. $inspect = new Builder\PhpClass();
  44. $inspect->setName('ZendTest\Di\TestAsset\InspectedClass');
  45. $inspectCtor = new Builder\InjectionMethod();
  46. $inspectCtor->setName('__construct')
  47. ->addParameter('foo', 'composed')
  48. ->addParameter('baz', null);
  49. $inspect->addInjectionMethod($inspectCtor);
  50. $composed = new Builder\PhpClass();
  51. $composed->setName('ZendTest\Di\TestAsset\ComposedClass');
  52. $struct = new Builder\PhpClass();
  53. $struct->setName('ZendTest\Di\TestAsset\Struct');
  54. $structCtor = new Builder\InjectionMethod();
  55. $structCtor->setName('__construct')
  56. ->addParameter('param1', null)
  57. ->addParameter('param2', 'inspect');
  58. $definition = new Definition();
  59. $definition->addClass($inspect)
  60. ->addClass($composed)
  61. ->addClass($struct);
  62. $this->di->definitions()->unshift($definition);
  63. $data = array(
  64. 'instance' => array(
  65. 'alias' => array(
  66. 'composed' => 'ZendTest\Di\TestAsset\ComposedClass',
  67. 'inspect' => 'ZendTest\Di\TestAsset\InspectedClass',
  68. 'struct' => 'ZendTest\Di\TestAsset\Struct',
  69. ),
  70. 'preferences' => array(
  71. 'composed' => array('composed'),
  72. 'inspect' => array('inspect'),
  73. 'struct' => array('struct'),
  74. ),
  75. 'ZendTest\Di\TestAsset\InspectedClass' => array( 'parameters' => array(
  76. 'baz' => 'BAZ',
  77. )),
  78. 'ZendTest\Di\TestAsset\Struct' => array( 'parameters' => array(
  79. 'param1' => 'foo',
  80. )),
  81. ),
  82. );
  83. $configuration = new Config($data);
  84. $configuration->configure($this->di);
  85. }
  86. public function buildContainerClass($name = 'Application')
  87. {
  88. $this->createDefinitions();
  89. $builder = new ContainerGenerator($this->di);
  90. $builder->setContainerClass($name);
  91. $builder->getCodeGenerator($this->getTmpFile())->write();
  92. $this->assertFileExists($this->tmpFile);
  93. }
  94. /**
  95. * @group one
  96. */
  97. public function testCreatesContainerClassFromConfiguredDependencyInjector()
  98. {
  99. $this->buildContainerClass();
  100. $tokens = token_get_all(file_get_contents($this->tmpFile));
  101. $count = count($tokens);
  102. $found = false;
  103. $value = false;
  104. for ($i = 0; $i < $count; $i++) {
  105. $token = $tokens[$i];
  106. if (is_string($token)) {
  107. continue;
  108. }
  109. if (T_CLASS == $token[0]) {
  110. $found = true;
  111. $value = false;
  112. do {
  113. $i++;
  114. $token = $tokens[$i];
  115. if (is_string($token)) {
  116. $id = null;
  117. } else {
  118. list($id, $value) = $token;
  119. }
  120. } while (($i < $count) && ($id != T_STRING));
  121. break;
  122. }
  123. }
  124. $this->assertTrue($found, "Class token not found");
  125. $this->assertContains('Application', $value);
  126. }
  127. public function testCreatesContainerClassWithCasesForEachService()
  128. {
  129. $this->buildContainerClass();
  130. $tokens = token_get_all(file_get_contents($this->tmpFile));
  131. $count = count($tokens);
  132. $services = array();
  133. for ($i = 0; $i < $count; $i++) {
  134. $token = $tokens[$i];
  135. if (is_string($token)) {
  136. continue;
  137. }
  138. if ('T_CASE' == token_name($token[0])) {
  139. do {
  140. $i++;
  141. if ($i >= $count) {
  142. break;
  143. }
  144. $token = $tokens[$i];
  145. if (is_string($token)) {
  146. $id = $token;
  147. } else {
  148. $id = $token[0];
  149. }
  150. } while (($i < $count) && ($id != T_CONSTANT_ENCAPSED_STRING));
  151. if (is_array($token)) {
  152. $services[] = preg_replace('/\\\'/', '', $token[1]);
  153. }
  154. }
  155. }
  156. $expected = array(
  157. 'composed',
  158. 'ZendTest\Di\TestAsset\ComposedClass',
  159. 'inspect',
  160. 'ZendTest\Di\TestAsset\InspectedClass',
  161. 'struct',
  162. 'ZendTest\Di\TestAsset\Struct',
  163. );
  164. $this->assertEquals(count($expected), count($services), var_export($services, 1));
  165. foreach ($expected as $service) {
  166. $this->assertContains($service, $services);
  167. }
  168. }
  169. public function testCreatesContainerClassWithMethodsForEachServiceAndAlias()
  170. {
  171. $this->buildContainerClass();
  172. $tokens = token_get_all(file_get_contents($this->tmpFile));
  173. $count = count($tokens);
  174. $methods = array();
  175. for ($i = 0; $i < $count; $i++) {
  176. $token = $tokens[$i];
  177. if (is_string($token)) {
  178. continue;
  179. }
  180. if ("T_FUNCTION" == token_name($token[0])) {
  181. $value = false;
  182. do {
  183. $i++;
  184. $token = $tokens[$i];
  185. if (is_string($token)) {
  186. $id = null;
  187. } else {
  188. list($id, $value) = $token;
  189. }
  190. } while (($i < $count) && (token_name($id) != 'T_STRING'));
  191. if ($value) {
  192. $methods[] = $value;
  193. }
  194. }
  195. }
  196. $expected = array(
  197. 'get',
  198. 'getZendTestDiTestAssetComposedClass',
  199. 'getComposed',
  200. 'getZendTestDiTestAssetInspectedClass',
  201. 'getInspect',
  202. 'getZendTestDiTestAssetStruct',
  203. 'getStruct',
  204. );
  205. $this->assertEquals(count($expected), count($methods), var_export($methods, 1));
  206. foreach ($expected as $method) {
  207. $this->assertContains($method, $methods);
  208. }
  209. }
  210. public function testAllowsRetrievingClassFileCodeGenerationObject()
  211. {
  212. $this->createDefinitions();
  213. $builder = new ContainerGenerator($this->di);
  214. $builder->setContainerClass('Application');
  215. $codegen = $builder->getCodeGenerator();
  216. $this->assertInstanceOf('Zend\Code\Generator\FileGenerator', $codegen);
  217. }
  218. public function testCanSpecifyNamespaceForGeneratedPhpClassfile()
  219. {
  220. $this->createDefinitions();
  221. $builder = new ContainerGenerator($this->di);
  222. $builder->setContainerClass('Context')
  223. ->setNamespace('Application');
  224. $codegen = $builder->getCodeGenerator();
  225. $this->assertEquals('Application', $codegen->getNamespace());
  226. }
  227. /**
  228. * @group nullargs
  229. */
  230. public function testNullAsOnlyArgumentResultsInEmptyParameterList()
  231. {
  232. $this->markTestIncomplete('Null arguments are currently unsupported');
  233. $opt = new Builder\PhpClass();
  234. $opt->setName('ZendTest\Di\TestAsset\OptionalArg');
  235. $optCtor = new Builder\InjectionMethod();
  236. $optCtor->setName('__construct')
  237. ->addParameter('param', null);
  238. $opt->addInjectionMethod($optCtor);
  239. $def = new Definition();
  240. $def->addClass($opt);
  241. $this->di->setDefinition($def);
  242. $cfg = new Config(array(
  243. 'instance' => array(
  244. 'alias' => array('optional' => 'ZendTest\Di\TestAsset\OptionalArg'),
  245. ),
  246. 'properties' => array(
  247. 'ZendTest\Di\TestAsset\OptionalArg' => array('param' => null),
  248. ),
  249. ));
  250. $cfg->configure($this->di);
  251. $builder = new ContainerGenerator($this->di);
  252. $builder->setContainerClass('Container');
  253. $codeGen = $builder->getCodeGenerator();
  254. $classBody = $codeGen->generate();
  255. $this->assertNotContains('NULL)', $classBody, $classBody);
  256. }
  257. /**
  258. * @group nullargs
  259. */
  260. public function testNullAsLastArgumentsResultsInTruncatedParameterList()
  261. {
  262. $this->markTestIncomplete('Null arguments are currently unsupported');
  263. $struct = new Builder\PhpClass();
  264. $struct->setName('ZendTest\Di\TestAsset\Struct');
  265. $structCtor = new Builder\InjectionMethod();
  266. $structCtor->setName('__construct')
  267. ->addParameter('param1', null)
  268. ->addParameter('param2', null);
  269. $struct->addInjectionMethod($structCtor);
  270. $dummy = new Builder\PhpClass();
  271. $dummy->setName('ZendTest\Di\TestAsset\DummyParams')
  272. ->setInstantiator(array('ZendTest\Di\TestAsset\StaticFactory', 'factory'));
  273. $staticFactory = new Builder\PhpClass();
  274. $staticFactory->setName('ZendTest\Di\TestAsset\StaticFactory');
  275. $factory = new Builder\InjectionMethod();
  276. $factory->setName('factory')
  277. ->addParameter('struct', 'struct')
  278. ->addParameter('params', null);
  279. $staticFactory->addInjectionMethod($factory);
  280. $def = new Definition();
  281. $def->addClass($struct)
  282. ->addClass($dummy)
  283. ->addClass($staticFactory);
  284. $this->di->setDefinition($def);
  285. $cfg = new Config(array(
  286. 'instance' => array(
  287. 'alias' => array(
  288. 'struct' => 'ZendTest\Di\TestAsset\Struct',
  289. 'dummy' => 'ZendTest\Di\TestAsset\DummyParams',
  290. 'factory' => 'ZendTest\Di\TestAsset\StaticFactory',
  291. ),
  292. 'properties' => array(
  293. 'ZendTest\Di\TestAsset\Struct' => array(
  294. 'param1' => 'foo',
  295. 'param2' => 'bar',
  296. ),
  297. 'ZendTest\Di\TestAsset\StaticFactory' => array(
  298. 'params' => null,
  299. ),
  300. ),
  301. ),
  302. ));
  303. $cfg->configure($this->di);
  304. $builder = new ContainerGenerator($this->di);
  305. $builder->setContainerClass('Container');
  306. $codeGen = $builder->getCodeGenerator();
  307. $classBody = $codeGen->generate();
  308. $this->assertNotContains('NULL)', $classBody, $classBody);
  309. }
  310. /**
  311. * @group nullargs
  312. */
  313. public function testNullArgumentsResultInEmptyMethodParameterList()
  314. {
  315. $this->markTestIncomplete('Null arguments are currently unsupported');
  316. $opt = new Builder\PhpClass();
  317. $opt->setName('ZendTest\Di\TestAsset\OptionalArg');
  318. $optCtor = new Builder\InjectionMethod();
  319. $optCtor->setName('__construct')
  320. ->addParameter('param', null);
  321. $optInject = new Builder\InjectionMethod();
  322. $optInject->setName('inject')
  323. ->addParameter('param1', null)
  324. ->addParameter('param2', null);
  325. $opt->addInjectionMethod($optCtor)
  326. ->addInjectionMethod($optInject);
  327. $def = new Definition();
  328. $def->addClass($opt);
  329. $this->di->setDefinition($def);
  330. $cfg = new Config(array(
  331. 'instance' => array(
  332. 'alias' => array('optional' => 'ZendTest\Di\TestAsset\OptionalArg'),
  333. ),
  334. 'properties' => array(
  335. 'ZendTest\Di\TestAsset\OptionalArg' => array(
  336. 'param' => null,
  337. 'param1' => null,
  338. 'param2' => null,
  339. ),
  340. ),
  341. ));
  342. $cfg->configure($this->di);
  343. $builder = new ContainerGenerator($this->di);
  344. $builder->setContainerClass('Container');
  345. $codeGen = $builder->getCodeGenerator();
  346. $classBody = $codeGen->generate();
  347. $this->assertNotContains('NULL)', $classBody, $classBody);
  348. }
  349. public function testClassNamesInstantiatedDirectlyShouldBeFullyQualified()
  350. {
  351. $this->createDefinitions();
  352. $builder = new ContainerGenerator($this->di);
  353. $builder->setContainerClass('Context')
  354. ->setNamespace('Application');
  355. $content = $builder->getCodeGenerator()->generate();
  356. $count = substr_count($content, '\ZendTest\Di\TestAsset\\');
  357. $this->assertEquals(3, $count, $content);
  358. $this->assertNotContains('\\\\', $content);
  359. }
  360. }