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

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