IntrospectorTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_Amf
  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. // Call Zend_Controller_Action_Helper_MultiPageFormTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Amf_Adobe_IntrospectorTest::main");
  25. }
  26. /**
  27. * @see Zend_Amf_Adobe_Introspector
  28. */
  29. require_once 'Zend/Amf/Adobe/Introspector.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Amf
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Amf
  37. */
  38. class Zend_Amf_Adobe_IntrospectorTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  43. PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function setUp()
  46. {
  47. $this->introspector = new Zend_Amf_Adobe_Introspector();
  48. }
  49. public function testIntrospectionDoesNotIncludeConstructor()
  50. {
  51. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  52. $this->assertNotContains('__construct', $xml);
  53. }
  54. public function testIntrospectionDoesNotIncludeMagicMethods()
  55. {
  56. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  57. $this->assertNotContains('__get', $xml);
  58. }
  59. public function testIntrospectionContainsPublicPropertiesOfReturnClassTypes()
  60. {
  61. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  62. $this->assertRegexp('/<type[^>]*(name="com_zend_framework_IntrospectorTestCustomType")/', $xml, $xml);
  63. $this->assertRegexp('/<property[^>]*(name="foo")/', $xml, $xml);
  64. $this->assertRegexp('/<property[^>]*(type="string")/', $xml, $xml);
  65. }
  66. public function testIntrospectionDoesNotContainNonPublicPropertiesOfReturnClassTypes()
  67. {
  68. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  69. $this->assertNotRegexp('/<property[^>]*(name="_bar")/', $xml, $xml);
  70. }
  71. public function testIntrospectionContainsPublicMethods()
  72. {
  73. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  74. $this->assertRegexp('/<operation[^>]*(name="foobar")/', $xml, $xml);
  75. $this->assertRegexp('/<operation[^>]*(name="barbaz")/', $xml, $xml);
  76. $this->assertRegexp('/<operation[^>]*(name="bazbat")/', $xml, $xml);
  77. }
  78. public function testIntrospectionContainsOperationForEachPrototypeOfAPublicMethod()
  79. {
  80. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  81. $this->assertEquals(4, substr_count($xml, 'name="foobar"'));
  82. $this->assertEquals(1, substr_count($xml, 'name="barbaz"'));
  83. $this->assertEquals(1, substr_count($xml, 'name="bazbat"'));
  84. }
  85. public function testPassingDirectoriesOptionShouldResolveServiceClassAndType()
  86. {
  87. require_once dirname(__FILE__) . '/_files/ZendAmfAdobeIntrospectorTestType.php';
  88. $xml = $this->introspector->introspect('ZendAmfAdobeIntrospectorTest', array(
  89. 'directories' => array(dirname(__FILE__) . '/_files'),
  90. ));
  91. $this->assertRegexp('/<operation[^>]*(name="foo")/', $xml, $xml);
  92. $this->assertRegexp('/<type[^>]*(name="ZendAmfAdobeIntrospectorTestType")/', $xml, $xml);
  93. $this->assertRegexp('/<property[^>]*(name="bar")/', $xml, $xml);
  94. }
  95. public function testMissingPropertyDocblockInTypedClassShouldReportTypeAsUnknown()
  96. {
  97. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  98. if (!preg_match('/(<property[^>]*(name="baz")[^>]*>)/', $xml, $matches)) {
  99. $this->fail('Baz property of com.zend.framework.IntrospectorTestCustomType not found');
  100. }
  101. $node = $matches[1];
  102. $this->assertContains('type="Unknown"', $node, $node);
  103. }
  104. public function testPropertyDocblockWithoutAnnotationInTypedClassShouldReportTypeAsUnknown()
  105. {
  106. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  107. if (!preg_match('/(<property[^>]*(name="bat")[^>]*>)/', $xml, $matches)) {
  108. $this->fail('Bat property of com.zend.framework.IntrospectorTestCustomType not found');
  109. }
  110. $node = $matches[1];
  111. $this->assertContains('type="Unknown"', $node, $node);
  112. }
  113. public function testTypedClassWithExplicitTypeShouldReportAsThatType()
  114. {
  115. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  116. $this->assertRegexp('/<type[^>]*(name="explicit")/', $xml, $xml);
  117. }
  118. /**
  119. * @group ZF-10365
  120. */
  121. public function testArgumentsWithArrayTypeHintsReflectedInReturnedXml()
  122. {
  123. require_once dirname(__FILE__) . '/TestAsset/ParameterHints.php';
  124. $xml = $this->introspector->introspect('Zend.Amf.Adobe.TestAsset.ParameterHints');
  125. $this->assertRegexp('/<argument[^>]*(name="arg1")[^>]*(type="Unknown\[\]")/', $xml, $xml);
  126. $this->assertRegexp('/<argument[^>]*(name="arg2")[^>]*(type="Unknown\[\]")/', $xml, $xml);
  127. }
  128. }
  129. class com_zend_framework_IntrospectorTest
  130. {
  131. /**
  132. * Constructor
  133. *
  134. * @return void
  135. */
  136. public function __construct()
  137. {
  138. }
  139. /**
  140. * Overloading: get properties
  141. *
  142. * @param string $name
  143. * @return mixed
  144. */
  145. public function __get($name)
  146. {
  147. $prop = '_' . $name;
  148. if (!isset($this->$prop)) {
  149. return null;
  150. }
  151. return $this->$prop;
  152. }
  153. /**
  154. * Foobar
  155. *
  156. * @param string|int $arg
  157. * @return string|stdClass
  158. */
  159. public function foobar($arg)
  160. {
  161. }
  162. /**
  163. * Barbaz
  164. *
  165. * @param com_zend_framework_IntrospectorTestCustomType $arg
  166. * @return boolean
  167. */
  168. public function barbaz($arg)
  169. {
  170. }
  171. /**
  172. * Bazbat
  173. *
  174. * @return com_zend_framework_IntrospectorTestExplicitType
  175. */
  176. public function bazbat()
  177. {
  178. }
  179. }
  180. class com_zend_framework_IntrospectorTestCustomType
  181. {
  182. /**
  183. * @var string
  184. */
  185. public $foo;
  186. public $baz;
  187. /**
  188. * Docblock without an annotation
  189. */
  190. public $bat;
  191. /**
  192. * @var bool
  193. */
  194. protected $_bar;
  195. }
  196. class com_zend_framework_IntrospectorTestExplicitType
  197. {
  198. public $_explicitType = 'explicit';
  199. }
  200. // Call Zend_Amf_Adobe_IntrospectorTest::main() if this source file is executed directly.
  201. if (PHPUnit_MAIN_METHOD == "Zend_Amf_Adobe_IntrospectorTest::main") {
  202. Zend_Amf_Adobe_IntrospectorTest::main();
  203. }