IntrospectorTest.php 7.2 KB

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