MethodTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * @version $Id $
  21. */
  22. /**
  23. * @see TestHelper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** requires */
  27. require_once 'Zend/Reflection/Method.php';
  28. require_once 'Zend/CodeGenerator/Php/Method.php';
  29. require_once 'Zend/Reflection/Docblock.php';
  30. require_once 'Zend/Reflection/Class.php';
  31. require_once '_files/TestSampleSingleClass.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_CodeGenerator
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. *
  39. * @group Zend_CodeGenerator
  40. * @group Zend_CodeGenerator_Php
  41. */
  42. class Zend_CodeGenerator_Php_MethodTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_CodeGenerator_Php_Method
  46. */
  47. protected $_method = null;
  48. public function setup()
  49. {
  50. $this->_method = new Zend_CodeGenerator_Php_Method();
  51. }
  52. public function teardown()
  53. {
  54. $this->_method = null;
  55. }
  56. public function testMethodConstructor()
  57. {
  58. $codeGenMethod = new Zend_CodeGenerator_Php_Method();
  59. $this->isInstanceOf($codeGenMethod, 'Zend_CodeGenerator_Php_Method');
  60. }
  61. public function testMethodParameterAccessors()
  62. {
  63. $codeGen = new Zend_CodeGenerator_Php_Method();
  64. $codeGen->setParameters(array(
  65. array('name' => 'one')
  66. ));
  67. $params = $codeGen->getParameters();
  68. $param = array_shift($params);
  69. $this->assertTrue($param instanceof Zend_CodeGenerator_Php_Parameter, 'Failed because $param was not instance of Zend_CodeGenerator_Php_Property');
  70. }
  71. public function testMethodBodyGetterAndSetter()
  72. {
  73. $this->_method->setBody('Foo');
  74. $this->assertEquals('Foo', $this->_method->getBody());
  75. }
  76. public function testDocblockGetterAndSetter()
  77. {
  78. $d = new Zend_CodeGenerator_Php_Docblock();
  79. $this->_method->setDocblock($d);
  80. $this->assertTrue($d === $this->_method->getDocblock());
  81. }
  82. public function testMethodFromReflection()
  83. {
  84. $ref = new Zend_Reflection_Method('Zend_Reflection_TestSampleSingleClass', 'someMethod');
  85. $codeGenMethod = Zend_CodeGenerator_Php_Method::fromReflection($ref);
  86. $target = <<<EOS
  87. /**
  88. * Enter description here...
  89. *
  90. * @return bool
  91. *
  92. */
  93. public function someMethod()
  94. {
  95. /* test test */
  96. }
  97. EOS;
  98. $this->assertEquals($target, (string) $codeGenMethod);
  99. }
  100. /**
  101. * @group ZF-6444
  102. */
  103. public function testMethodWithStaticModifierIsEmitted()
  104. {
  105. $codeGen = new Zend_CodeGenerator_Php_Method();
  106. $codeGen->setName('foo');
  107. $codeGen->setParameters(array(
  108. array('name' => 'one')
  109. ));
  110. $codeGen->setStatic(true);
  111. $expected = <<<EOS
  112. public static function foo(\$one)
  113. {
  114. }
  115. EOS;
  116. $this->assertEquals($expected, $codeGen->generate());
  117. }
  118. /**
  119. * @group ZF-6444
  120. */
  121. public function testMethodWithFinalModifierIsEmitted()
  122. {
  123. $codeGen = new Zend_CodeGenerator_Php_Method();
  124. $codeGen->setName('foo');
  125. $codeGen->setParameters(array(
  126. array('name' => 'one')
  127. ));
  128. $codeGen->setFinal(true);
  129. $expected = <<<EOS
  130. final public function foo(\$one)
  131. {
  132. }
  133. EOS;
  134. $this->assertEquals($expected, $codeGen->generate());
  135. }
  136. /**
  137. * @group ZF-6444
  138. */
  139. public function testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract()
  140. {
  141. $codeGen = new Zend_CodeGenerator_Php_Method();
  142. $codeGen->setName('foo');
  143. $codeGen->setParameters(array(
  144. array('name' => 'one')
  145. ));
  146. $codeGen->setFinal(true);
  147. $codeGen->setAbstract(true);
  148. $expected = <<<EOS
  149. abstract public function foo(\$one)
  150. {
  151. }
  152. EOS;
  153. $this->assertEquals($expected, $codeGen->generate());
  154. }
  155. /**
  156. * @group ZF-7205
  157. */
  158. public function testMethodCanHaveDocblock()
  159. {
  160. $codeGenProperty = new Zend_CodeGenerator_Php_Method(array(
  161. 'name' => 'someFoo',
  162. 'static' => true,
  163. 'visibility' => 'protected',
  164. 'docblock' => '@var string $someVal This is some val'
  165. ));
  166. $expected = <<<EOS
  167. /**
  168. * @var string \$someVal This is some val
  169. */
  170. protected static function someFoo()
  171. {
  172. }
  173. EOS;
  174. $this->assertEquals($expected, $codeGenProperty->generate());
  175. }
  176. }