IntrospectorTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // Call Zend_Controller_Action_Helper_MultiPageFormTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Amf_Adobe_IntrospectorTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /**
  8. * @see Zend_Amf_Adobe_Introspector
  9. */
  10. require_once 'Zend/Amf/Adobe/Introspector.php';
  11. class Zend_Amf_Adobe_IntrospectorTest extends PHPUnit_Framework_TestCase
  12. {
  13. public static function main()
  14. {
  15. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  16. PHPUnit_TextUI_TestRunner::run($suite);
  17. }
  18. public function setUp()
  19. {
  20. $this->introspector = new Zend_Amf_Adobe_Introspector();
  21. }
  22. public function testIntrospectionDoesNotIncludeConstructor()
  23. {
  24. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  25. $this->assertNotContains('__construct', $xml);
  26. }
  27. public function testIntrospectionDoesNotIncludeMagicMethods()
  28. {
  29. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  30. $this->assertNotContains('__get', $xml);
  31. }
  32. public function testIntrospectionContainsPublicPropertiesOfReturnClassTypes()
  33. {
  34. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  35. $this->assertRegexp('/<type[^>]*(name="com_zend_framework_IntrospectorTestCustomType")/', $xml, $xml);
  36. $this->assertRegexp('/<property[^>]*(name="foo")/', $xml, $xml);
  37. $this->assertRegexp('/<property[^>]*(type="string")/', $xml, $xml);
  38. }
  39. public function testIntrospectionDoesNotContainNonPublicPropertiesOfReturnClassTypes()
  40. {
  41. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  42. $this->assertNotRegexp('/<property[^>]*(name="_bar")/', $xml, $xml);
  43. }
  44. public function testIntrospectionContainsPublicMethods()
  45. {
  46. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  47. $this->assertRegexp('/<operation[^>]*(name="foobar")/', $xml, $xml);
  48. $this->assertRegexp('/<operation[^>]*(name="barbaz")/', $xml, $xml);
  49. $this->assertRegexp('/<operation[^>]*(name="bazbat")/', $xml, $xml);
  50. }
  51. public function testIntrospectionContainsOperationForEachPrototypeOfAPublicMethod()
  52. {
  53. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  54. $this->assertEquals(4, substr_count($xml, 'name="foobar"'));
  55. $this->assertEquals(1, substr_count($xml, 'name="barbaz"'));
  56. $this->assertEquals(1, substr_count($xml, 'name="bazbat"'));
  57. }
  58. public function testPassingDirectoriesOptionShouldResolveServiceClassAndType()
  59. {
  60. require_once dirname(__FILE__) . '/_files/ZendAmfAdobeIntrospectorTestType.php';
  61. $xml = $this->introspector->introspect('ZendAmfAdobeIntrospectorTest', array(
  62. 'directories' => array(dirname(__FILE__) . '/_files'),
  63. ));
  64. $this->assertRegexp('/<operation[^>]*(name="foo")/', $xml, $xml);
  65. $this->assertRegexp('/<type[^>]*(name="ZendAmfAdobeIntrospectorTestType")/', $xml, $xml);
  66. $this->assertRegexp('/<property[^>]*(name="bar")/', $xml, $xml);
  67. }
  68. public function testMissingPropertyDocblockInTypedClassShouldReportTypeAsUnknown()
  69. {
  70. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  71. if (!preg_match('/(<property[^>]*(name="baz")[^>]*>)/', $xml, $matches)) {
  72. $this->fail('Baz property of com.zend.framework.IntrospectorTestCustomType not found');
  73. }
  74. $node = $matches[1];
  75. $this->assertContains('type="Unknown"', $node, $node);
  76. }
  77. public function testPropertyDocblockWithoutAnnotationInTypedClassShouldReportTypeAsUnknown()
  78. {
  79. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  80. if (!preg_match('/(<property[^>]*(name="bat")[^>]*>)/', $xml, $matches)) {
  81. $this->fail('Bat property of com.zend.framework.IntrospectorTestCustomType not found');
  82. }
  83. $node = $matches[1];
  84. $this->assertContains('type="Unknown"', $node, $node);
  85. }
  86. public function testTypedClassWithExplicitTypeShouldReportAsThatType()
  87. {
  88. $xml = $this->introspector->introspect('com.zend.framework.IntrospectorTest');
  89. $this->assertRegexp('/<type[^>]*(name="explicit")/', $xml, $xml);
  90. }
  91. }
  92. class com_zend_framework_IntrospectorTest
  93. {
  94. /**
  95. * Constructor
  96. *
  97. * @return void
  98. */
  99. public function __construct()
  100. {
  101. }
  102. /**
  103. * Overloading: get properties
  104. *
  105. * @param string $name
  106. * @return mixed
  107. */
  108. public function __get($name)
  109. {
  110. $prop = '_' . $name;
  111. if (!isset($this->$prop)) {
  112. return null;
  113. }
  114. return $this->$prop;
  115. }
  116. /**
  117. * Foobar
  118. *
  119. * @param string|int $arg
  120. * @return string|stdClass
  121. */
  122. public function foobar($arg)
  123. {
  124. }
  125. /**
  126. * Barbaz
  127. *
  128. * @param com_zend_framework_IntrospectorTestCustomType $arg
  129. * @return boolean
  130. */
  131. public function barbaz($arg)
  132. {
  133. }
  134. /**
  135. * Bazbat
  136. *
  137. * @return com_zend_framework_IntrospectorTestExplicitType
  138. */
  139. public function bazbat()
  140. {
  141. }
  142. }
  143. class com_zend_framework_IntrospectorTestCustomType
  144. {
  145. /**
  146. * @var string
  147. */
  148. public $foo;
  149. public $baz;
  150. /**
  151. * Docblock without an annotation
  152. */
  153. public $bat;
  154. /**
  155. * @var bool
  156. */
  157. protected $_bar;
  158. }
  159. class com_zend_framework_IntrospectorTestExplicitType
  160. {
  161. public $_explicitType = 'explicit';
  162. }
  163. // Call Zend_Amf_Adobe_IntrospectorTest::main() if this source file is executed directly.
  164. if (PHPUnit_MAIN_METHOD == "Zend_Amf_Adobe_IntrospectorTest::main") {
  165. Zend_Amf_Adobe_IntrospectorTest::main();
  166. }