/tests/ZendTest/Server/Reflection/ReflectionFunctionTest.php

https://github.com/zucchi/zf2 · PHP · 204 lines · 134 code · 31 blank · 39 comment · 0 complexity · 65f98338a795f16bee5d37aa9ebce697 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Server
  9. */
  10. namespace ZendTest\Server\Reflection;
  11. use Zend\Server\Reflection;
  12. /**
  13. * @category Zend
  14. * @package Zend_Server
  15. * @subpackage UnitTests
  16. * @group Zend_Server
  17. */
  18. class ReflectionFunctionTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function test__construct()
  21. {
  22. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  23. $r = new Reflection\ReflectionFunction($function);
  24. $this->assertTrue($r instanceof Reflection\ReflectionFunction);
  25. $this->assertTrue($r instanceof Reflection\AbstractFunction);
  26. $params = $r->getParameters();
  27. $r = new Reflection\ReflectionFunction($function, 'namespace');
  28. $this->assertEquals('namespace', $r->getNamespace());
  29. $argv = array('string1', 'string2');
  30. $r = new Reflection\ReflectionFunction($function, 'namespace', $argv);
  31. $this->assertTrue(is_array($r->getInvokeArguments()));
  32. $this->assertTrue($argv === $r->getInvokeArguments());
  33. $prototypes = $r->getPrototypes();
  34. $this->assertTrue(is_array($prototypes));
  35. $this->assertTrue(0 < count($prototypes));
  36. }
  37. public function testConstructorThrowsExceptionOnNonFunction()
  38. {
  39. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  40. $r = new Reflection\ReflectionFunction($function);
  41. $params = $r->getParameters();
  42. $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid reflection class');
  43. $r = new Reflection\ReflectionFunction($params[0]);
  44. }
  45. public function test__getSet()
  46. {
  47. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  48. $r = new Reflection\ReflectionFunction($function);
  49. $r->system = true;
  50. $this->assertTrue($r->system);
  51. }
  52. public function testNamespace()
  53. {
  54. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  55. $r = new Reflection\ReflectionFunction($function, 'namespace');
  56. $this->assertEquals('namespace', $r->getNamespace());
  57. $r->setNamespace('framework');
  58. $this->assertEquals('framework', $r->getNamespace());
  59. }
  60. public function testDescription()
  61. {
  62. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  63. $r = new Reflection\ReflectionFunction($function);
  64. $this->assertContains('function for reflection', $r->getDescription());
  65. $r->setDescription('Testing setting descriptions');
  66. $this->assertEquals('Testing setting descriptions', $r->getDescription());
  67. }
  68. public function testGetPrototypes()
  69. {
  70. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  71. $r = new Reflection\ReflectionFunction($function);
  72. $prototypes = $r->getPrototypes();
  73. $this->assertTrue(is_array($prototypes));
  74. $this->assertTrue(0 < count($prototypes));
  75. $this->assertEquals(8, count($prototypes));
  76. foreach ($prototypes as $p) {
  77. $this->assertTrue($p instanceof Reflection\Prototype);
  78. }
  79. }
  80. public function testGetPrototypes2()
  81. {
  82. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function2');
  83. $r = new Reflection\ReflectionFunction($function);
  84. $prototypes = $r->getPrototypes();
  85. $this->assertTrue(is_array($prototypes));
  86. $this->assertTrue(0 < count($prototypes));
  87. $this->assertEquals(1, count($prototypes));
  88. foreach ($prototypes as $p) {
  89. $this->assertTrue($p instanceof Reflection\Prototype);
  90. }
  91. }
  92. public function testGetInvokeArguments()
  93. {
  94. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  95. $r = new Reflection\ReflectionFunction($function);
  96. $args = $r->getInvokeArguments();
  97. $this->assertTrue(is_array($args));
  98. $this->assertEquals(0, count($args));
  99. $argv = array('string1', 'string2');
  100. $r = new Reflection\ReflectionFunction($function, null, $argv);
  101. $args = $r->getInvokeArguments();
  102. $this->assertTrue(is_array($args));
  103. $this->assertEquals(2, count($args));
  104. $this->assertTrue($argv === $args);
  105. }
  106. public function test__wakeup()
  107. {
  108. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  109. $r = new Reflection\ReflectionFunction($function);
  110. $s = serialize($r);
  111. $u = unserialize($s);
  112. $this->assertTrue($u instanceof Reflection\ReflectionFunction);
  113. $this->assertEquals('', $u->getNamespace());
  114. }
  115. public function testMultipleWhitespaceBetweenDoctagsAndTypes()
  116. {
  117. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function3');
  118. $r = new Reflection\ReflectionFunction($function);
  119. $prototypes = $r->getPrototypes();
  120. $this->assertTrue(is_array($prototypes));
  121. $this->assertTrue(0 < count($prototypes));
  122. $this->assertEquals(1, count($prototypes));
  123. $proto = $prototypes[0];
  124. $params = $proto->getParameters();
  125. $this->assertTrue(is_array($params));
  126. $this->assertEquals(1, count($params));
  127. $this->assertEquals('string', $params[0]->getType());
  128. }
  129. /**
  130. * @group ZF-6996
  131. */
  132. public function testParameterReflectionShouldReturnTypeAndVarnameAndDescription()
  133. {
  134. $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1');
  135. $r = new Reflection\ReflectionFunction($function);
  136. $prototypes = $r->getPrototypes();
  137. $prototype = $prototypes[0];
  138. $params = $prototype->getParameters();
  139. $param = $params[0];
  140. $this->assertContains('Some description', $param->getDescription(), var_export($param, 1));
  141. }
  142. }
  143. /**
  144. * \ZendTest\Server\Reflection\function1
  145. *
  146. * Test function for reflection unit tests
  147. *
  148. * @param string $var1 Some description
  149. * @param string|array $var2
  150. * @param array $var3
  151. * @return null|array
  152. */
  153. function function1($var1, $var2, $var3 = null)
  154. {
  155. }
  156. /**
  157. * \ZendTest\Server\Reflection\function2
  158. *
  159. * Test function for reflection unit tests; test what happens when no return
  160. * value or params specified in docblock.
  161. */
  162. function function2()
  163. {
  164. }
  165. /**
  166. * \ZendTest\Server\Reflection\function3
  167. *
  168. * @param string $var1
  169. * @return void
  170. */
  171. function function3($var1)
  172. {
  173. }