FileTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-2010 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-2010 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. ///$this->markTestSkipped('skipme');
  94. $file = dirname(__FILE__) . '/_files/TestSampleSingleClass.php';
  95. require_once $file;
  96. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  97. $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
  98. $expectedOutput = <<<EOS
  99. <?php
  100. /**
  101. * File header here
  102. *
  103. * @author Ralph Schindler <ralph.schindler@zend.com>
  104. *
  105. */
  106. /**
  107. * class docblock
  108. *
  109. * @package Zend_Reflection_TestSampleSingleClass
  110. *
  111. */
  112. class Zend_Reflection_TestSampleSingleClass
  113. {
  114. /**
  115. * Enter description here...
  116. *
  117. * @return bool
  118. *
  119. */
  120. public function someMethod()
  121. {
  122. /* test test */
  123. }
  124. public function foobar()
  125. {
  126. }
  127. }
  128. EOS;
  129. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  130. }
  131. public function testFileLineEndingsAreAlwaysLineFeed()
  132. {
  133. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  134. 'requiredFiles' => array('SampleClass.php'),
  135. 'class' => array(
  136. 'abstract' => true,
  137. 'name' => 'SampleClass',
  138. 'extendedClass' => 'ExtendedClassName',
  139. 'implementedInterfaces' => array('Iterator', 'Traversable')
  140. )
  141. ));
  142. // explode by newline, this would leave CF in place if it were generated
  143. $lines = explode("\n", $codeGenFile);
  144. $targetLength = strlen('require_once \'SampleClass.php\';');
  145. $this->assertEquals($targetLength, strlen($lines[2]));
  146. $this->assertEquals(';', $lines[2]{$targetLength-1});
  147. }
  148. }