MethodTest.php 5.1 KB

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