FunctionTest.php 7.3 KB

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