2
0

FileTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /**
  23. * @see TestHelper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** requires here */
  27. require_once 'Zend/CodeGenerator/Php/File.php';
  28. require_once 'Zend/Reflection/File.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_CodeGenerator
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. *
  36. * @group Zend_CodeGenerator
  37. * @group Zend_CodeGenerator_Php
  38. * @group Zend_CodeGenerator_Php_File
  39. */
  40. class Zend_CodeGenerator_Php_FileTest extends PHPUnit_Framework_TestCase
  41. {
  42. public function testConstruction()
  43. {
  44. $file = new Zend_CodeGenerator_Php_File();
  45. $this->assertEquals(get_class($file), 'Zend_CodeGenerator_Php_File');
  46. }
  47. public function testSourceContentGetterAndSetter()
  48. {
  49. $file = new Zend_CodeGenerator_Php_File();
  50. $file->setSourceContent('Foo');
  51. $this->assertEquals('Foo', $file->getSourceContent());
  52. }
  53. public function testIndentationGetterAndSetter()
  54. {
  55. $file = new Zend_CodeGenerator_Php_File();
  56. $file->setIndentation(' ');
  57. $this->assertEquals(' ', $file->getIndentation());
  58. }
  59. public function testToString()
  60. {
  61. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  62. 'requiredFiles' => array('SampleClass.php'),
  63. 'class' => array(
  64. 'abstract' => true,
  65. 'name' => 'SampleClass',
  66. 'extendedClass' => 'ExtendedClassName',
  67. 'implementedInterfaces' => array('Iterator', 'Traversable')
  68. )
  69. ));
  70. $expectedOutput = <<<EOS
  71. <?php
  72. require_once 'SampleClass.php';
  73. abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
  74. {
  75. }
  76. EOS;
  77. $output = $codeGenFile->generate();
  78. $this->assertEquals($expectedOutput, $output, $output);
  79. }
  80. public function testFromReflection()
  81. {
  82. $tempFile = tempnam(sys_get_temp_dir(), 'UnitFile');
  83. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  84. 'class' => array(
  85. 'name' => 'SampleClass'
  86. )
  87. ));
  88. file_put_contents($tempFile, $codeGenFile->generate());
  89. require_once $tempFile;
  90. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($tempFile));
  91. unlink($tempFile);
  92. $this->assertEquals(get_class($codeGenFileFromDisk), 'Zend_CodeGenerator_Php_File');
  93. $this->assertEquals(count($codeGenFileFromDisk->getClasses()), 1);
  94. }
  95. public function testFromReflectionFile()
  96. {
  97. ///$this->markTestSkipped('skipme');
  98. $file = dirname(__FILE__) . '/_files/TestSampleSingleClass.php';
  99. require_once $file;
  100. $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
  101. $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
  102. $expectedOutput = <<<EOS
  103. <?php
  104. /**
  105. * File header here
  106. *
  107. * @author Ralph Schindler <ralph.schindler@zend.com>
  108. *
  109. */
  110. /**
  111. * class docblock
  112. *
  113. * @package Zend_Reflection_TestSampleSingleClass
  114. *
  115. */
  116. class Zend_Reflection_TestSampleSingleClass
  117. {
  118. /**
  119. * Enter description here...
  120. *
  121. * @return bool
  122. *
  123. */
  124. public function someMethod()
  125. {
  126. /* test test */
  127. }
  128. public function foobar()
  129. {
  130. }
  131. }
  132. EOS;
  133. $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
  134. }
  135. public function testFileLineEndingsAreAlwaysLineFeed()
  136. {
  137. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  138. 'requiredFiles' => array('SampleClass.php'),
  139. 'class' => array(
  140. 'abstract' => true,
  141. 'name' => 'SampleClass',
  142. 'extendedClass' => 'ExtendedClassName',
  143. 'implementedInterfaces' => array('Iterator', 'Traversable')
  144. )
  145. ));
  146. // explode by newline, this would leave CF in place if it were generated
  147. $lines = explode("\n", $codeGenFile);
  148. $targetLength = strlen('require_once \'SampleClass.php\';');
  149. $this->assertEquals($targetLength, strlen($lines[2]));
  150. $this->assertEquals(';', $lines[2]{$targetLength-1});
  151. }
  152. }