PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/CodeGenerator/Php/FileTest.php

https://bitbucket.org/ksekar/campus
PHP | 381 lines | 173 code | 91 blank | 117 comment | 0 complexity | d45b385dbf8ed629d8ef9f38294af8bc MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodeGenerator
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. /** requires here */
  23. require_once 'Zend/CodeGenerator/Php/File.php';
  24. require_once 'Zend/Reflection/File.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_CodeGenerator
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. *
  32. * @group Zend_CodeGenerator
  33. * @group Zend_CodeGenerator_Php
  34. * @group Zend_CodeGenerator_Php_File
  35. */
  36. class Zend_CodeGenerator_Php_FileTest extends PHPUnit_Framework_TestCase
  37. {
  38. public function testConstruction()
  39. {
  40. $file = new Zend_CodeGenerator_Php_File();
  41. $this->assertEquals(get_class($file), 'Zend_CodeGenerator_Php_File');
  42. }
  43. public function testSourceContentGetterAndSetter()
  44. {
  45. $file = new Zend_CodeGenerator_Php_File();
  46. $file->setSourceContent('Foo');
  47. $this->assertEquals('Foo', $file->getSourceContent());
  48. }
  49. public function testIndentationGetterAndSetter()
  50. {
  51. $file = new Zend_CodeGenerator_Php_File();
  52. $file->setIndentation(' ');
  53. $this->assertEquals(' ', $file->getIndentation());
  54. }
  55. public function testToString()
  56. {
  57. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  58. 'requiredFiles' => array('SampleClass.php'),
  59. 'class' => array(
  60. 'abstract' => true,
  61. 'name' => 'SampleClass',
  62. 'extendedClass' => 'ExtendedClassName',
  63. 'implementedInterfaces' => array('Iterator', 'Traversable')
  64. )
  65. ));
  66. $expectedOutput = <<<EOS
  67. <?php
  68. require_once 'SampleClass.php';
  69. abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
  70. {
  71. }
  72. EOS;
  73. $output = $codeGenFile->generate();
  74. $this->assertEquals($expectedOutput, $output, $output);
  75. }
  76. public function testFromReflection()
  77. {
  78. $tempFile = tempnam(sys_get_temp_dir(), 'UnitFile');
  79. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  80. 'class' => array(
  81. 'name' => 'SampleClass'
  82. )
  83. ));
  84. file_put_contents($tempFile, $codeGenFile->generate());
  85. require_once $tempFile;
  86. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($tempFile));
  87. unlink($tempFile);
  88. $this->assertEquals(get_class($codeGenFileFromDisk), 'Zend_CodeGenerator_Php_File');
  89. $this->assertEquals(count($codeGenFileFromDisk->getClasses()), 1);
  90. }
  91. public function testFromReflectionFile()
  92. {
  93. $file = dirname(__FILE__) . '/_files/TestSampleSingleClass.php';
  94. require_once $file;
  95. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  96. $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
  97. $expectedOutput = <<<EOS
  98. <?php
  99. /**
  100. * File header here
  101. *
  102. * @author Ralph Schindler <ralph.schindler@zend.com>
  103. *
  104. */
  105. /**
  106. * class docblock
  107. *
  108. * @package Zend_Reflection_TestSampleSingleClass
  109. *
  110. */
  111. class Zend_Reflection_TestSampleSingleClass
  112. {
  113. /**
  114. * Enter description here...
  115. *
  116. * @return bool
  117. *
  118. */
  119. public function someMethod()
  120. {
  121. /* test test */
  122. }
  123. public function foobar()
  124. {
  125. }
  126. }
  127. EOS;
  128. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  129. }
  130. /**
  131. * @group ZF-7369
  132. * @group ZF-6982
  133. */
  134. public function testFromReflectionFileKeepsIndents()
  135. {
  136. $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
  137. require_once $file;
  138. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  139. $expectedOutput = <<<EOS
  140. <?php
  141. /**
  142. * File header here
  143. *
  144. * @author Ralph Schindler <ralph.schindler@zend.com>
  145. */
  146. /**
  147. * class docblock
  148. *
  149. * @package Zend_Reflection_TestClassWithCodeInMethod
  150. */
  151. class Zend_Reflection_TestClassWithCodeInMethod
  152. {
  153. /**
  154. * Enter description here...
  155. *
  156. * @return bool
  157. */
  158. public function someMethod()
  159. {
  160. /* test test */
  161. \$foo = 'bar';
  162. }
  163. }
  164. EOS;
  165. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  166. }
  167. /**
  168. * @group ZF-7369
  169. * @group ZF-6982
  170. */
  171. public function testFromReflectionFilePreservesIndentsWhenAdditionalMethodAdded()
  172. {
  173. $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
  174. require_once $file;
  175. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  176. $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
  177. $expectedOutput = <<<EOS
  178. <?php
  179. /**
  180. * File header here
  181. *
  182. * @author Ralph Schindler <ralph.schindler@zend.com>
  183. *
  184. */
  185. /**
  186. * class docblock
  187. *
  188. * @package Zend_Reflection_TestClassWithCodeInMethod
  189. *
  190. */
  191. class Zend_Reflection_TestClassWithCodeInMethod
  192. {
  193. /**
  194. * Enter description here...
  195. *
  196. * @return bool
  197. *
  198. */
  199. public function someMethod()
  200. {
  201. /* test test */
  202. \$foo = 'bar';
  203. }
  204. public function foobar()
  205. {
  206. }
  207. }
  208. EOS;
  209. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  210. }
  211. public function testFileLineEndingsAreAlwaysLineFeed()
  212. {
  213. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  214. 'requiredFiles' => array('SampleClass.php'),
  215. 'class' => array(
  216. 'abstract' => true,
  217. 'name' => 'SampleClass',
  218. 'extendedClass' => 'ExtendedClassName',
  219. 'implementedInterfaces' => array('Iterator', 'Traversable')
  220. )
  221. ));
  222. // explode by newline, this would leave CF in place if it were generated
  223. $lines = explode("\n", $codeGenFile);
  224. $targetLength = strlen('require_once \'SampleClass.php\';');
  225. $this->assertEquals($targetLength, strlen($lines[2]));
  226. $this->assertEquals(';', $lines[2]{$targetLength-1});
  227. }
  228. /**
  229. * @group ZF-11703
  230. */
  231. public function testNewMethodKeepDocBlock(){
  232. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703.php', true, true);
  233. $target = <<<EOS
  234. <?php
  235. /**
  236. * For manipulating files.
  237. *
  238. */
  239. class Foo
  240. {
  241. public function bar()
  242. {
  243. // action body
  244. }
  245. public function bar2()
  246. {
  247. // action body
  248. }
  249. }
  250. EOS;
  251. $codeGenFile->getClass()->setMethod(array(
  252. 'name' => 'bar2',
  253. 'body' => '// action body'
  254. ));
  255. $this->assertEquals($target, $codeGenFile->generate());
  256. }
  257. /**
  258. * @group ZF-11703
  259. */
  260. public function testNewMethodKeepTwoDocBlock(){
  261. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703_1.php', true, true);
  262. $target = <<<EOS
  263. <?php
  264. /**
  265. * For manipulating files.
  266. *
  267. */
  268. /**
  269. * Class Foo1
  270. *
  271. */
  272. class Foo1
  273. {
  274. public function bar()
  275. {
  276. // action body
  277. }
  278. public function bar2()
  279. {
  280. // action body
  281. }
  282. }
  283. EOS;
  284. $codeGenFile->getClass()->setMethod(array(
  285. 'name' => 'bar2',
  286. 'body' => '// action body'
  287. ));
  288. $this->assertEquals($target, $codeGenFile->generate());
  289. }
  290. }