ParameterTest.php 7.2 KB

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