MethodTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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-2014 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-2014 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. public function someMethod()
  89. {
  90. /* test test */
  91. }
  92. EOS;
  93. $this->assertEquals($target, (string) $codeGenMethod);
  94. }
  95. /**
  96. * @group ZF-6444
  97. */
  98. public function testMethodWithStaticModifierIsEmitted()
  99. {
  100. $codeGen = new Zend_CodeGenerator_Php_Method();
  101. $codeGen->setName('foo');
  102. $codeGen->setParameters(array(
  103. array('name' => 'one')
  104. ));
  105. $codeGen->setStatic(true);
  106. $expected = <<<EOS
  107. public static function foo(\$one)
  108. {
  109. }
  110. EOS;
  111. $this->assertEquals($expected, $codeGen->generate());
  112. }
  113. /**
  114. * @group ZF-6444
  115. */
  116. public function testMethodWithFinalModifierIsEmitted()
  117. {
  118. $codeGen = new Zend_CodeGenerator_Php_Method();
  119. $codeGen->setName('foo');
  120. $codeGen->setParameters(array(
  121. array('name' => 'one')
  122. ));
  123. $codeGen->setFinal(true);
  124. $expected = <<<EOS
  125. final public function foo(\$one)
  126. {
  127. }
  128. EOS;
  129. $this->assertEquals($expected, $codeGen->generate());
  130. }
  131. /**
  132. * @group ZF-6444
  133. */
  134. public function testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract()
  135. {
  136. $codeGen = new Zend_CodeGenerator_Php_Method();
  137. $codeGen->setName('foo');
  138. $codeGen->setParameters(array(
  139. array('name' => 'one')
  140. ));
  141. $codeGen->setFinal(true);
  142. $codeGen->setAbstract(true);
  143. $expected = <<<EOS
  144. abstract public function foo(\$one)
  145. {
  146. }
  147. EOS;
  148. $this->assertEquals($expected, $codeGen->generate());
  149. }
  150. /**
  151. * @group ZF-7205
  152. */
  153. public function testMethodCanHaveDocblock()
  154. {
  155. $codeGenProperty = new Zend_CodeGenerator_Php_Method(array(
  156. 'name' => 'someFoo',
  157. 'static' => true,
  158. 'visibility' => 'protected',
  159. 'docblock' => '@var string $someVal This is some val'
  160. ));
  161. $expected = <<<EOS
  162. /**
  163. * @var string \$someVal This is some val
  164. */
  165. protected static function someFoo()
  166. {
  167. }
  168. EOS;
  169. $this->assertEquals($expected, $codeGenProperty->generate());
  170. }
  171. }