/core/src/test/php/net/xp_framework/unittest/reflection/ParameterTest.class.php

https://github.com/treuter/xp-framework · PHP · 506 lines · 185 code · 52 blank · 269 comment · 0 complexity · f25d17b6ee003b3b7208d553b9fcbdb7 MD5 · raw file

  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('unittest.TestCase', 'util.collections.HashTable');
  7. /**
  8. * Test the XP reflection API
  9. *
  10. * @see xp://lang.reflect.Parameter
  11. * @purpose Testcase
  12. */
  13. class ParameterTest extends TestCase {
  14. /**
  15. * Method without functionality to be used by tests.
  16. *
  17. */
  18. private function initialize() { }
  19. /**
  20. * Method without functionality to be used by tests.
  21. *
  22. * @param string name
  23. */
  24. private function setName($name) { }
  25. /**
  26. * Method without functionality to be used by tests.
  27. *
  28. * @param util.Date date default NULL
  29. */
  30. private function setDate($date= NULL) { }
  31. /**
  32. * Method without functionality to be used by tests.
  33. *
  34. * @param string format
  35. * @param string* values
  36. */
  37. private function printf($format, $values= NULL) { }
  38. /**
  39. * Method without functionality to be used by tests.
  40. *
  41. * @param * value
  42. * @param util.collections.Map context
  43. */
  44. private function serialize($value, Map $context) { }
  45. /**
  46. * Method without functionality to be used by tests.
  47. *
  48. * @param util.collections.HashTable map
  49. */
  50. private function setHashTable(HashTable $map) { }
  51. /**
  52. * Method without functionality to be used by tests.
  53. *
  54. * @param [:var] map
  55. */
  56. private function setHash(array $map) { }
  57. /**
  58. * Method without functionality to be used by tests.
  59. *
  60. * @param string[] map default array
  61. */
  62. private function setArray(array $map= array()) { }
  63. /**
  64. * Method without functionality to be used by tests.
  65. *
  66. * @param int a
  67. * @param int b default 1
  68. */
  69. private function inc($a, $b= 1) { }
  70. /**
  71. * Method without functionality to be used by tests.
  72. *
  73. * @param bool new
  74. */
  75. private function setStatus($new= FALSE) { }
  76. /**
  77. * Method without functionality to be used by tests.
  78. *
  79. * @param rdbms.DBConnection conn
  80. * @param string mode
  81. */
  82. #[@$conn: inject(name= "db")]
  83. private function setConnection($conn, $mode) { }
  84. /**
  85. * Tests Method::numParameters()
  86. *
  87. */
  88. #[@test]
  89. public function numParameters() {
  90. $this->assertEquals(0, $this->getClass()->getMethod('initialize')->numParameters(), 'initialize');
  91. $this->assertEquals(1, $this->getClass()->getMethod('setName')->numParameters(), 'setName');
  92. $this->assertEquals(1, $this->getClass()->getMethod('setDate')->numParameters(), 'setDate');
  93. $this->assertEquals(2, $this->getClass()->getMethod('printf')->numParameters(), 'printf');
  94. $this->assertEquals(2, $this->getClass()->getMethod('serialize')->numParameters(), 'serialize');
  95. }
  96. /**
  97. * Tests Method::getParameter
  98. *
  99. */
  100. #[@test]
  101. public function getExistingParameter() {
  102. $this->assertClass($this->getClass()->getMethod('setName')->getParameter(0), 'lang.reflect.Parameter');
  103. }
  104. /**
  105. * Tests Method::getParameter
  106. *
  107. */
  108. #[@test]
  109. public function getNonExistantParameter() {
  110. $this->assertNull($this->getClass()->getMethod('initialize')->getParameter(0));
  111. }
  112. /**
  113. * Tests Method::getParameters
  114. *
  115. */
  116. #[@test]
  117. public function initializeParameters() {
  118. $this->assertEquals(array(), $this->getClass()->getMethod('initialize')->getParameters());
  119. }
  120. /**
  121. * Tests Method::getParameters
  122. *
  123. */
  124. #[@test]
  125. public function setNameParameters() {
  126. $params= $this->getClass()->getMethod('setName')->getParameters();
  127. $this->assertArray($params);
  128. $this->assertEquals(1, sizeof($params));
  129. $this->assertClass($params[0], 'lang.reflect.Parameter');
  130. }
  131. /**
  132. * Tests Method::getParameters
  133. *
  134. */
  135. #[@test]
  136. public function serializeParameters() {
  137. $params= $this->getClass()->getMethod('serialize')->getParameters();
  138. $this->assertArray($params);
  139. $this->assertEquals(2, sizeof($params));
  140. $this->assertClass($params[0], 'lang.reflect.Parameter');
  141. $this->assertClass($params[1], 'lang.reflect.Parameter');
  142. }
  143. /**
  144. * Helper method to retrieve a method's parameter by its offset
  145. *
  146. * @param string name
  147. * @param string offset
  148. * @return lang.reflect.Parameter
  149. */
  150. protected function methodParameter($name, $offset) {
  151. return $this->getClass()->getMethod($name)->getParameter($offset);
  152. }
  153. /**
  154. * Tests Parameter::getName()
  155. *
  156. */
  157. #[@test]
  158. public function name() {
  159. $this->assertEquals('name', $this->methodParameter('setName', 0)->getName(), 'setName#0');
  160. $this->assertEquals('date', $this->methodParameter('setDate', 0)->getName(), 'setDate#0');
  161. $this->assertEquals('value', $this->methodParameter('serialize', 0)->getName(), 'serialize#0');
  162. $this->assertEquals('context', $this->methodParameter('serialize', 1)->getName(), 'serialize#1');
  163. }
  164. /**
  165. * Tests Parameter::getType()
  166. *
  167. */
  168. #[@test]
  169. public function stringType() {
  170. $this->assertEquals(Primitive::$STRING, $this->methodParameter('setName', 0)->getType());
  171. }
  172. /**
  173. * Tests Parameter::getType()
  174. *
  175. */
  176. #[@test]
  177. public function intType() {
  178. $this->assertEquals(Primitive::$INT, $this->methodParameter('inc', 0)->getType(), 'inc$a');
  179. $this->assertEquals(Primitive::$INT, $this->methodParameter('inc', 1)->getType(), 'inc$b');
  180. }
  181. /**
  182. * Tests Parameter::getType()
  183. *
  184. */
  185. #[@test]
  186. public function booleanType() {
  187. $this->assertEquals(Primitive::$BOOLEAN, $this->methodParameter('setStatus', 0)->getType());
  188. }
  189. /**
  190. * Tests Parameter::getType()
  191. *
  192. */
  193. #[@test]
  194. public function anyType() {
  195. $this->assertEquals(Type::$VAR, $this->methodParameter('serialize', 0)->getType());
  196. }
  197. /**
  198. * Tests Parameter::getType()
  199. *
  200. */
  201. #[@test]
  202. public function classType() {
  203. $this->assertEquals(XPClass::forName('util.Date'), $this->methodParameter('setDate', 0)->getType());
  204. }
  205. /**
  206. * Tests Parameter::getType()
  207. *
  208. */
  209. #[@test]
  210. public function interfaceType() {
  211. $this->assertEquals(XPClass::forName('util.collections.Map'), $this->methodParameter('serialize', 1)->getType());
  212. }
  213. /**
  214. * Tests Parameter::getType()
  215. *
  216. */
  217. #[@test]
  218. public function arrayType() {
  219. $this->assertEquals(ArrayType::forName('string[]'), $this->methodParameter('setArray', 0)->getType());
  220. }
  221. /**
  222. * Tests Parameter::getType()
  223. *
  224. */
  225. #[@test]
  226. public function varArgsArrayType() {
  227. $this->assertEquals(ArrayType::forName('string[]'), $this->methodParameter('printf', 1)->getType());
  228. }
  229. /**
  230. * Tests Parameter::getTypeRestriction()
  231. *
  232. */
  233. #[@test]
  234. public function typeRestriction() {
  235. $this->assertNull($this->methodParameter('setName', 0)->getTypeRestriction());
  236. }
  237. /**
  238. * Tests Parameter::getTypeRestriction()
  239. *
  240. */
  241. #[@test]
  242. public function isOptional() {
  243. $this->assertFalse($this->methodParameter('setName', 0)->isOptional());
  244. $this->assertTrue($this->methodParameter('setDate', 0)->isOptional());
  245. }
  246. /**
  247. * Tests Parameter::getDefaultValue()
  248. *
  249. */
  250. #[@test]
  251. public function nullDefaultValue() {
  252. $this->assertNull($this->methodParameter('setDate', 0)->getDefaultValue());
  253. }
  254. /**
  255. * Tests Parameter::getDefaultValue()
  256. *
  257. */
  258. #[@test]
  259. public function intDefaultValue() {
  260. $this->assertEquals(1, $this->methodParameter('inc', 1)->getDefaultValue());
  261. }
  262. /**
  263. * Tests Parameter::getDefaultValue()
  264. *
  265. */
  266. #[@test]
  267. public function booleanDefaultValue() {
  268. $this->assertEquals(FALSE, $this->methodParameter('setStatus', 0)->getDefaultValue());
  269. }
  270. /**
  271. * Tests Parameter::getDefaultValue()
  272. *
  273. */
  274. #[@test]
  275. public function arrayDefaultValue() {
  276. $this->assertEquals(array(), $this->methodParameter('setArray', 0)->getDefaultValue());
  277. }
  278. /**
  279. * Tests Parameter::toString()
  280. *
  281. */
  282. #[@test]
  283. public function stringOfOptional() {
  284. $this->assertEquals(
  285. 'lang.reflect.Parameter<lang.Primitive<bool> new= false>',
  286. $this->methodParameter('setStatus', 0)->toString()
  287. );
  288. }
  289. /**
  290. * Tests Parameter::toString()
  291. *
  292. */
  293. #[@test]
  294. public function stringOfAnyTyped() {
  295. $this->assertEquals(
  296. 'lang.reflect.Parameter<lang.Type<var> value>',
  297. $this->methodParameter('serialize', 0)->toString()
  298. );
  299. }
  300. /**
  301. * Tests Parameter::toString()
  302. *
  303. */
  304. #[@test]
  305. public function stringOfClassTyped() {
  306. $this->assertEquals(
  307. 'lang.reflect.Parameter<lang.XPClass<util.collections.Map> context>',
  308. $this->methodParameter('serialize', 1)->toString()
  309. );
  310. }
  311. /**
  312. * Tests Parameter::getDefaultValue() throws an exception if
  313. * an Parameter does not have a default value
  314. *
  315. */
  316. #[@test, @expect('lang.IllegalStateException')]
  317. public function defaultValueOfNonOptional() {
  318. $this->methodParameter('setName', 0)->getDefaultValue();
  319. }
  320. /**
  321. * Tests non-type hinted parameter's type restriction is NULL
  322. *
  323. */
  324. #[@test]
  325. public function unRestrictedParamType() {
  326. $this->assertNull($this->methodParameter('setDate', 0)->getTypeRestriction());
  327. }
  328. /**
  329. * Tests type hinted parameter's type is returned via getTypeRestriction()
  330. *
  331. */
  332. #[@test]
  333. public function restrictedParamClassType() {
  334. $this->assertEquals(
  335. XPClass::forName('util.collections.HashTable'),
  336. $this->methodParameter('setHashTable', 0)->getTypeRestriction()
  337. );
  338. }
  339. /**
  340. * Tests type hinted parameter's type is returned via getTypeRestriction()
  341. *
  342. */
  343. #[@test]
  344. public function restrictedParamArrayType() {
  345. $this->assertEquals(
  346. Primitive::$ARRAY,
  347. $this->methodParameter('setHash', 0)->getTypeRestriction(),
  348. 'setHash'
  349. );
  350. $this->assertEquals(
  351. Primitive::$ARRAY,
  352. $this->methodParameter('setArray', 0)->getTypeRestriction(),
  353. 'setArray'
  354. );
  355. }
  356. /**
  357. * Tests hasAnnotation()
  358. *
  359. */
  360. #[@test]
  361. public function annotatedParameterHasInjectAnnotation() {
  362. $this->assertTrue($this->methodParameter('setConnection', 0)->hasAnnotation('inject'));
  363. }
  364. /**
  365. * Tests getAnnotation()
  366. *
  367. */
  368. #[@test]
  369. public function annotatedParametersInjectAnnotation() {
  370. $this->assertEquals(
  371. array('name' => 'db'),
  372. $this->methodParameter('setConnection', 0)->getAnnotation('inject')
  373. );
  374. }
  375. /**
  376. * Tests hasAnnotations()
  377. *
  378. */
  379. #[@test]
  380. public function annotatedParameterHasAnnotations() {
  381. $this->assertTrue($this->methodParameter('setConnection', 0)->hasAnnotations());
  382. }
  383. /**
  384. * Tests getAnnotations()
  385. *
  386. */
  387. #[@test]
  388. public function annotatedParametersAnnotations() {
  389. $this->assertEquals(
  390. array('inject' => array('name' => 'db')),
  391. $this->methodParameter('setConnection', 0)->getAnnotations()
  392. );
  393. }
  394. /**
  395. * Tests hasAnnotations()
  396. *
  397. */
  398. #[@test]
  399. public function normalParameterHasNoAnnotations() {
  400. $this->assertFalse($this->methodParameter('setConnection', 1)->hasAnnotations());
  401. }
  402. /**
  403. * Tests getAnnotations()
  404. *
  405. */
  406. #[@test]
  407. public function normalParametersAnnotations() {
  408. $this->assertEquals(
  409. array(),
  410. $this->methodParameter('setConnection', 1)->getAnnotations()
  411. );
  412. }
  413. /**
  414. * Tests hasAnnotations()
  415. *
  416. */
  417. #[@test]
  418. public function normalParameterHasNoAnnotation() {
  419. $this->assertFalse($this->methodParameter('setConnection', 1)->hasAnnotation('irrelevant'));
  420. }
  421. /**
  422. * Tests getAnnotations()
  423. *
  424. */
  425. #[@test, @expect('lang.ElementNotFoundException')]
  426. public function normalParameterGetAnnotationRaisesException() {
  427. $this->methodParameter('setConnection', 1)->getAnnotation('irrelevant');
  428. }
  429. /**
  430. * Tests the first parameter of the constructor inherited from
  431. * the unittest.TestCase base class is of type string.
  432. *
  433. */
  434. #[@test]
  435. public function inheritedConstructorsParameter() {
  436. $this->assertEquals(
  437. Primitive::$STRING,
  438. $this->getClass()->getConstructor()->getParameter(0)->getType()
  439. );
  440. }
  441. /**
  442. * Tests the first parameter of the constructor inherited from
  443. * the unittest.TestCase base class is of type string.
  444. *
  445. */
  446. #[@test]
  447. public function inheritedConstructorsParameters() {
  448. $this->assertEquals(
  449. Primitive::$STRING,
  450. this($this->getClass()->getConstructor()->getParameters(), 0)->getType()
  451. );
  452. }
  453. }
  454. ?>