IntrospectorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-2009 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-2009 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. class com_zend_framework_IntrospectorTest
  121. {
  122. /**
  123. * Constructor
  124. *
  125. * @return void
  126. */
  127. public function __construct()
  128. {
  129. }
  130. /**
  131. * Overloading: get properties
  132. *
  133. * @param string $name
  134. * @return mixed
  135. */
  136. public function __get($name)
  137. {
  138. $prop = '_' . $name;
  139. if (!isset($this->$prop)) {
  140. return null;
  141. }
  142. return $this->$prop;
  143. }
  144. /**
  145. * Foobar
  146. *
  147. * @param string|int $arg
  148. * @return string|stdClass
  149. */
  150. public function foobar($arg)
  151. {
  152. }
  153. /**
  154. * Barbaz
  155. *
  156. * @param com_zend_framework_IntrospectorTestCustomType $arg
  157. * @return boolean
  158. */
  159. public function barbaz($arg)
  160. {
  161. }
  162. /**
  163. * Bazbat
  164. *
  165. * @return com_zend_framework_IntrospectorTestExplicitType
  166. */
  167. public function bazbat()
  168. {
  169. }
  170. }
  171. class com_zend_framework_IntrospectorTestCustomType
  172. {
  173. /**
  174. * @var string
  175. */
  176. public $foo;
  177. public $baz;
  178. /**
  179. * Docblock without an annotation
  180. */
  181. public $bat;
  182. /**
  183. * @var bool
  184. */
  185. protected $_bar;
  186. }
  187. class com_zend_framework_IntrospectorTestExplicitType
  188. {
  189. public $_explicitType = 'explicit';
  190. }
  191. // Call Zend_Amf_Adobe_IntrospectorTest::main() if this source file is executed directly.
  192. if (PHPUnit_MAIN_METHOD == "Zend_Amf_Adobe_IntrospectorTest::main") {
  193. Zend_Amf_Adobe_IntrospectorTest::main();
  194. }