FileTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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-2015 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-2015 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. * class docblock
  106. *
  107. * @package Zend_Reflection_TestSampleSingleClass
  108. */
  109. class Zend_Reflection_TestSampleSingleClass
  110. {
  111. /**
  112. * Enter description here...
  113. *
  114. * @return bool
  115. */
  116. public function someMethod()
  117. {
  118. /* test test */
  119. }
  120. public function foobar()
  121. {
  122. }
  123. }
  124. EOS;
  125. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  126. }
  127. /**
  128. * @group ZF-7369
  129. * @group ZF-6982
  130. */
  131. public function testFromReflectionFileKeepsIndents()
  132. {
  133. $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
  134. require_once $file;
  135. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  136. $expectedOutput = <<<EOS
  137. <?php
  138. /**
  139. * File header here
  140. *
  141. * @author Ralph Schindler <ralph.schindler@zend.com>
  142. */
  143. /**
  144. * class docblock
  145. *
  146. * @package Zend_Reflection_TestClassWithCodeInMethod
  147. */
  148. class Zend_Reflection_TestClassWithCodeInMethod
  149. {
  150. /**
  151. * Enter description here...
  152. *
  153. * @return bool
  154. */
  155. public function someMethod()
  156. {
  157. /* test test */
  158. \$foo = 'bar';
  159. }
  160. }
  161. EOS;
  162. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  163. }
  164. /**
  165. * @group ZF-7369
  166. * @group ZF-6982
  167. */
  168. public function testFromReflectionFilePreservesIndentsWhenAdditionalMethodAdded()
  169. {
  170. $file = dirname(__FILE__) . '/_files/TestClassWithCodeInMethod.php';
  171. require_once $file;
  172. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  173. $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
  174. $expectedOutput = <<<EOS
  175. <?php
  176. /**
  177. * File header here
  178. *
  179. * @author Ralph Schindler <ralph.schindler@zend.com>
  180. */
  181. /**
  182. * class docblock
  183. *
  184. * @package Zend_Reflection_TestClassWithCodeInMethod
  185. */
  186. class Zend_Reflection_TestClassWithCodeInMethod
  187. {
  188. /**
  189. * Enter description here...
  190. *
  191. * @return bool
  192. */
  193. public function someMethod()
  194. {
  195. /* test test */
  196. \$foo = 'bar';
  197. }
  198. public function foobar()
  199. {
  200. }
  201. }
  202. EOS;
  203. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  204. }
  205. public function testFileLineEndingsAreAlwaysLineFeed()
  206. {
  207. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  208. 'requiredFiles' => array('SampleClass.php'),
  209. 'class' => array(
  210. 'abstract' => true,
  211. 'name' => 'SampleClass',
  212. 'extendedClass' => 'ExtendedClassName',
  213. 'implementedInterfaces' => array('Iterator', 'Traversable')
  214. )
  215. ));
  216. // explode by newline, this would leave CF in place if it were generated
  217. $lines = explode("\n", $codeGenFile);
  218. $targetLength = strlen('require_once \'SampleClass.php\';');
  219. $this->assertEquals($targetLength, strlen($lines[2]));
  220. $this->assertEquals(';', $lines[2]{$targetLength-1});
  221. }
  222. /**
  223. * @group ZF-11703
  224. */
  225. public function testNewMethodKeepDocBlock(){
  226. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703.php', true, true);
  227. $target = <<<EOS
  228. <?php
  229. /**
  230. * For manipulating files.
  231. */
  232. class Foo
  233. {
  234. public function bar()
  235. {
  236. // action body
  237. }
  238. public function bar2()
  239. {
  240. // action body
  241. }
  242. }
  243. EOS;
  244. $codeGenFile->getClass()->setMethod(array(
  245. 'name' => 'bar2',
  246. 'body' => '// action body'
  247. ));
  248. $this->assertEquals($target, $codeGenFile->generate());
  249. }
  250. /**
  251. * @group ZF-11703
  252. */
  253. public function testNewMethodKeepTwoDocBlock(){
  254. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703_1.php', true, true);
  255. $target = <<<EOS
  256. <?php
  257. /**
  258. * For manipulating files.
  259. */
  260. /**
  261. * Class Foo1
  262. */
  263. class Foo1
  264. {
  265. public function bar()
  266. {
  267. // action body
  268. }
  269. public function bar2()
  270. {
  271. // action body
  272. }
  273. }
  274. EOS;
  275. $codeGenFile->getClass()->setMethod(array(
  276. 'name' => 'bar2',
  277. 'body' => '// action body'
  278. ));
  279. $this->assertEquals($target, $codeGenFile->generate());
  280. }
  281. }