ParameterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2015 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/Parameter.php';
  24. require_once 'Zend/CodeGenerator/Php/Parameter.php';
  25. require_once '_files/TestSampleSingleClass.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_CodeGenerator
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. *
  33. * @group Zend_CodeGenerator
  34. * @group Zend_CodeGenerator_Php
  35. */
  36. class Zend_CodeGenerator_Php_ParameterTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * @var Zend_CodeGenerator_Php_Parameter
  40. */
  41. protected $_parameter = null;
  42. public function setup()
  43. {
  44. $this->_parameter = new Zend_CodeGenerator_Php_Parameter();
  45. }
  46. public function teardown()
  47. {
  48. $this->_parameter = null;
  49. }
  50. public function testTypeGetterAndSetterPersistValue()
  51. {
  52. $this->_parameter->setType('Foo');
  53. $this->assertEquals('Foo', $this->_parameter->getType());
  54. }
  55. public function testNameGetterAndSetterPersistValue()
  56. {
  57. $this->_parameter->setName('Foo');
  58. $this->assertEquals('Foo', $this->_parameter->getName());
  59. }
  60. public function testDefaultValueGetterAndSetterPersistValue()
  61. {
  62. $this->_parameter->setDefaultValue('Foo');
  63. $this->assertEquals('Foo', $this->_parameter->getDefaultValue());
  64. }
  65. public function testPositionGetterAndSetterPersistValue()
  66. {
  67. $this->_parameter->setPosition(2);
  68. $this->assertEquals(2, $this->_parameter->getPosition());
  69. }
  70. public function testGenerateIsCorrect()
  71. {
  72. $this->_parameter->setType('Foo');
  73. $this->_parameter->setName('bar');
  74. $this->_parameter->setDefaultValue(15);
  75. $this->assertEquals('Foo $bar = 15', $this->_parameter->generate());
  76. $this->_parameter->setDefaultValue('foo');
  77. $this->assertEquals('Foo $bar = \'foo\'', $this->_parameter->generate());
  78. }
  79. public function testFromReflection_GetParameterName()
  80. {
  81. $reflParam = $this->getFirstReflectionParameter('name');
  82. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  83. $this->assertEquals('param', $codeGenParam->getName());
  84. }
  85. public function testFromReflection_GetParameterType()
  86. {
  87. $reflParam = $this->getFirstReflectionParameter('type');
  88. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  89. $this->assertEquals('stdClass', $codeGenParam->getType());
  90. }
  91. public function testFromReflection_GetReference()
  92. {
  93. $reflParam = $this->getFirstReflectionParameter('reference');
  94. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  95. $this->assertTrue($codeGenParam->getPassedByReference());
  96. }
  97. public function testFromReflection_GetDefaultValue()
  98. {
  99. $reflParam = $this->getFirstReflectionParameter('defaultValue');
  100. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  101. $this->assertEquals('foo', $codeGenParam->getDefaultValue());
  102. }
  103. public function testFromReflection_GetArrayHint()
  104. {
  105. $reflParam = $this->getFirstReflectionParameter('fromArray');
  106. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  107. $this->assertEquals('array', $codeGenParam->getType());
  108. }
  109. public function testFromReflection_GetWithNativeType()
  110. {
  111. $reflParam = $this->getFirstReflectionParameter('hasNativeDocTypes');
  112. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  113. $this->assertNotEquals('int', $codeGenParam->getType());
  114. $this->assertEquals('', $codeGenParam->getType());
  115. }
  116. static public function dataFromReflection_Generate()
  117. {
  118. return array(
  119. array('name', '$param'),
  120. array('type', 'stdClass $bar'),
  121. array('reference', '&$baz'),
  122. array('defaultValue', '$value = \'foo\''),
  123. array('defaultNull', '$value = null'),
  124. array('fromArray', 'array $array'),
  125. array('hasNativeDocTypes', '$integer'),
  126. array('defaultArray', '$array = array ()'),
  127. array('defaultArrayWithValues', '$array = array ( 0 => 1, 1 => 2, 2 => 3,)'),
  128. array('defaultFalse', '$val = false'),
  129. array('defaultTrue', '$val = true'),
  130. array('defaultZero', '$number = 0'),
  131. array('defaultNumber', '$number = 1234'),
  132. array('defaultFloat', '$float = 1.34'),
  133. array('defaultConstant', '$con = \'foo\'')
  134. );
  135. }
  136. /**
  137. * @dataProvider dataFromReflection_Generate
  138. * @param string $methodName
  139. * @param string $expectedCode
  140. */
  141. public function testFromReflection_Generate($methodName, $expectedCode)
  142. {
  143. $reflParam = $this->getFirstReflectionParameter($methodName);
  144. $codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);
  145. $this->assertEquals($expectedCode, $codeGenParam->generate());
  146. }
  147. /**
  148. * @param string $method
  149. * @return Zend_Reflection_Parameter
  150. */
  151. private function getFirstReflectionParameter($method)
  152. {
  153. $reflClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_ParameterExample');
  154. $method = $reflClass->getMethod($method);
  155. $params = $method->getParameters();
  156. return array_shift($params);
  157. }
  158. }
  159. class Zend_CodeGenerator_Php_ParameterExample
  160. {
  161. public function name($param)
  162. {
  163. }
  164. public function type(stdClass $bar)
  165. {
  166. }
  167. public function reference(&$baz)
  168. {
  169. }
  170. public function defaultValue($value="foo")
  171. {
  172. }
  173. public function defaultNull($value=null)
  174. {
  175. }
  176. public function fromArray(array $array)
  177. {
  178. }
  179. public function defaultArray($array = array())
  180. {
  181. }
  182. public function defaultFalse($val = false)
  183. {
  184. }
  185. public function defaultTrue($val = true)
  186. {
  187. }
  188. public function defaultZero($number = 0)
  189. {
  190. }
  191. public function defaultNumber($number = 1234)
  192. {
  193. }
  194. public function defaultFloat($float = 1.34)
  195. {
  196. }
  197. public function defaultArrayWithValues($array = array(0 => 1, 1 => 2, 2 => 3))
  198. {
  199. }
  200. const FOO = "foo";
  201. public function defaultConstant($con = self::FOO)
  202. {
  203. }
  204. /**
  205. * @param int $integer
  206. */
  207. public function hasNativeDocTypes($integer)
  208. {
  209. }
  210. }