FunctionTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_Server
  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. require_once 'Zend/Server/Reflection/Function.php';
  23. /**
  24. * Test case for Zend_Server_Reflection_Function
  25. *
  26. * @category Zend
  27. * @package Zend_Server
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Server
  32. */
  33. class Zend_Server_Reflection_FunctionTest extends PHPUnit_Framework_TestCase
  34. {
  35. public function test__construct()
  36. {
  37. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  38. $r = new Zend_Server_Reflection_Function($function);
  39. $this->assertTrue($r instanceof Zend_Server_Reflection_Function);
  40. $this->assertTrue($r instanceof Zend_Server_Reflection_Function_Abstract);
  41. $params = $r->getParameters();
  42. try {
  43. $r = new Zend_Server_Reflection_Function($params[0]);
  44. $this->fail('Should not be able to construct with non-function');
  45. } catch (Exception $e) {
  46. // do nothing
  47. }
  48. $r = new Zend_Server_Reflection_Function($function, 'namespace');
  49. $this->assertEquals('namespace', $r->getNamespace());
  50. $argv = array('string1', 'string2');
  51. $r = new Zend_Server_Reflection_Function($function, 'namespace', $argv);
  52. $this->assertTrue(is_array($r->getInvokeArguments()));
  53. $this->assertTrue($argv === $r->getInvokeArguments());
  54. $prototypes = $r->getPrototypes();
  55. $this->assertTrue(is_array($prototypes));
  56. $this->assertTrue(0 < count($prototypes));
  57. }
  58. public function test__getSet()
  59. {
  60. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  61. $r = new Zend_Server_Reflection_Function($function);
  62. $r->system = true;
  63. $this->assertTrue($r->system);
  64. }
  65. public function testNamespace()
  66. {
  67. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  68. $r = new Zend_Server_Reflection_Function($function, 'namespace');
  69. $this->assertEquals('namespace', $r->getNamespace());
  70. $r->setNamespace('framework');
  71. $this->assertEquals('framework', $r->getNamespace());
  72. }
  73. public function testDescription()
  74. {
  75. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  76. $r = new Zend_Server_Reflection_Function($function);
  77. $this->assertContains('function for reflection', $r->getDescription());
  78. $r->setDescription('Testing setting descriptions');
  79. $this->assertEquals('Testing setting descriptions', $r->getDescription());
  80. }
  81. public function testGetPrototypes()
  82. {
  83. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  84. $r = new Zend_Server_Reflection_Function($function);
  85. $prototypes = $r->getPrototypes();
  86. $this->assertTrue(is_array($prototypes));
  87. $this->assertTrue(0 < count($prototypes));
  88. $this->assertEquals(8, count($prototypes));
  89. foreach ($prototypes as $p) {
  90. $this->assertTrue($p instanceof Zend_Server_Reflection_Prototype);
  91. }
  92. }
  93. public function testGetPrototypes2()
  94. {
  95. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function2');
  96. $r = new Zend_Server_Reflection_Function($function);
  97. $prototypes = $r->getPrototypes();
  98. $this->assertTrue(is_array($prototypes));
  99. $this->assertTrue(0 < count($prototypes));
  100. $this->assertEquals(1, count($prototypes));
  101. foreach ($prototypes as $p) {
  102. $this->assertTrue($p instanceof Zend_Server_Reflection_Prototype);
  103. }
  104. }
  105. public function testGetInvokeArguments()
  106. {
  107. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  108. $r = new Zend_Server_Reflection_Function($function);
  109. $args = $r->getInvokeArguments();
  110. $this->assertTrue(is_array($args));
  111. $this->assertEquals(0, count($args));
  112. $argv = array('string1', 'string2');
  113. $r = new Zend_Server_Reflection_Function($function, null, $argv);
  114. $args = $r->getInvokeArguments();
  115. $this->assertTrue(is_array($args));
  116. $this->assertEquals(2, count($args));
  117. $this->assertTrue($argv === $args);
  118. }
  119. public function test__wakeup()
  120. {
  121. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  122. $r = new Zend_Server_Reflection_Function($function);
  123. $s = serialize($r);
  124. $u = unserialize($s);
  125. $this->assertTrue($u instanceof Zend_Server_Reflection_Function);
  126. $this->assertEquals('', $u->getNamespace());
  127. }
  128. public function testMultipleWhitespaceBetweenDoctagsAndTypes()
  129. {
  130. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function3');
  131. $r = new Zend_Server_Reflection_Function($function);
  132. $prototypes = $r->getPrototypes();
  133. $this->assertTrue(is_array($prototypes));
  134. $this->assertTrue(0 < count($prototypes));
  135. $this->assertEquals(1, count($prototypes));
  136. $proto = $prototypes[0];
  137. $params = $proto->getParameters();
  138. $this->assertTrue(is_array($params));
  139. $this->assertEquals(1, count($params));
  140. $this->assertEquals('string', $params[0]->getType());
  141. }
  142. /**
  143. * @group ZF-6996
  144. */
  145. public function testParameterReflectionShouldReturnTypeAndVarnameAndDescription()
  146. {
  147. $function = new ReflectionFunction('Zend_Server_Reflection_FunctionTest_function');
  148. $r = new Zend_Server_Reflection_Function($function);
  149. $prototypes = $r->getPrototypes();
  150. $prototype = $prototypes[0];
  151. $params = $prototype->getParameters();
  152. $param = $params[0];
  153. $this->assertContains('Some description', $param->getDescription(), var_export($param, 1));
  154. }
  155. }
  156. /**
  157. * Zend_Server_Reflection_FunctionTest_function
  158. *
  159. * Test function for reflection unit tests
  160. *
  161. * @param string $var1 Some description
  162. * @param string|array $var2
  163. * @param array $var3
  164. * @return null|array
  165. */
  166. function Zend_Server_Reflection_FunctionTest_function($var1, $var2, $var3 = null)
  167. {
  168. }
  169. /**
  170. * Zend_Server_Reflection_FunctionTest_function2
  171. *
  172. * Test function for reflection unit tests; test what happens when no return
  173. * value or params specified in docblock.
  174. */
  175. function Zend_Server_Reflection_FunctionTest_function2()
  176. {
  177. }
  178. /**
  179. * Zend_Server_Reflection_FunctionTest_function3
  180. *
  181. * @param string $var1
  182. * @return void
  183. */
  184. function Zend_Server_Reflection_FunctionTest_function3($var1)
  185. {
  186. }