2
0

FileTest.php 5.2 KB

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