MethodTest.php 5.2 KB

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