DocParserTest.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations;
  3. use Doctrine\Common\Annotations\Annotation\IgnorePhpDoc;
  4. use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
  5. use Doctrine\Common\Annotations\DocParser;
  6. use Doctrine\Common\Annotations\AnnotationRegistry;
  7. use Doctrine\Common\Annotations\Annotation\Target;
  8. use Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants;
  9. use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants;
  10. use Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants;
  11. class DocParserTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testNestedArraysWithNestedAnnotation()
  14. {
  15. $parser = $this->createTestParser();
  16. // Nested arrays with nested annotations
  17. $result = $parser->parse('@Name(foo={1,2, {"key"=@Name}})');
  18. $annot = $result[0];
  19. $this->assertTrue($annot instanceof Name);
  20. $this->assertNull($annot->value);
  21. $this->assertEquals(3, count($annot->foo));
  22. $this->assertEquals(1, $annot->foo[0]);
  23. $this->assertEquals(2, $annot->foo[1]);
  24. $this->assertTrue(is_array($annot->foo[2]));
  25. $nestedArray = $annot->foo[2];
  26. $this->assertTrue(isset($nestedArray['key']));
  27. $this->assertTrue($nestedArray['key'] instanceof Name);
  28. }
  29. public function testBasicAnnotations()
  30. {
  31. $parser = $this->createTestParser();
  32. // Marker annotation
  33. $result = $parser->parse("@Name");
  34. $annot = $result[0];
  35. $this->assertTrue($annot instanceof Name);
  36. $this->assertNull($annot->value);
  37. $this->assertNull($annot->foo);
  38. // Associative arrays
  39. $result = $parser->parse('@Name(foo={"key1" = "value1"})');
  40. $annot = $result[0];
  41. $this->assertNull($annot->value);
  42. $this->assertTrue(is_array($annot->foo));
  43. $this->assertTrue(isset($annot->foo['key1']));
  44. // Numerical arrays
  45. $result = $parser->parse('@Name({2="foo", 4="bar"})');
  46. $annot = $result[0];
  47. $this->assertTrue(is_array($annot->value));
  48. $this->assertEquals('foo', $annot->value[2]);
  49. $this->assertEquals('bar', $annot->value[4]);
  50. $this->assertFalse(isset($annot->value[0]));
  51. $this->assertFalse(isset($annot->value[1]));
  52. $this->assertFalse(isset($annot->value[3]));
  53. // Multiple values
  54. $result = $parser->parse('@Name(@Name, @Name)');
  55. $annot = $result[0];
  56. $this->assertTrue($annot instanceof Name);
  57. $this->assertTrue(is_array($annot->value));
  58. $this->assertTrue($annot->value[0] instanceof Name);
  59. $this->assertTrue($annot->value[1] instanceof Name);
  60. // Multiple types as values
  61. $result = $parser->parse('@Name(foo="Bar", @Name, {"key1"="value1", "key2"="value2"})');
  62. $annot = $result[0];
  63. $this->assertTrue($annot instanceof Name);
  64. $this->assertTrue(is_array($annot->value));
  65. $this->assertTrue($annot->value[0] instanceof Name);
  66. $this->assertTrue(is_array($annot->value[1]));
  67. $this->assertEquals('value1', $annot->value[1]['key1']);
  68. $this->assertEquals('value2', $annot->value[1]['key2']);
  69. // Complete docblock
  70. $docblock = <<<DOCBLOCK
  71. /**
  72. * Some nifty class.
  73. *
  74. * @author Mr.X
  75. * @Name(foo="bar")
  76. */
  77. DOCBLOCK;
  78. $result = $parser->parse($docblock);
  79. $this->assertEquals(1, count($result));
  80. $annot = $result[0];
  81. $this->assertTrue($annot instanceof Name);
  82. $this->assertEquals("bar", $annot->foo);
  83. $this->assertNull($annot->value);
  84. }
  85. public function testNamespacedAnnotations()
  86. {
  87. $parser = new DocParser;
  88. $parser->setIgnoreNotImportedAnnotations(true);
  89. $docblock = <<<DOCBLOCK
  90. /**
  91. * Some nifty class.
  92. *
  93. * @package foo
  94. * @subpackage bar
  95. * @author Mr.X <mr@x.com>
  96. * @Doctrine\Tests\Common\Annotations\Name(foo="bar")
  97. * @ignore
  98. */
  99. DOCBLOCK;
  100. $result = $parser->parse($docblock);
  101. $this->assertEquals(1, count($result));
  102. $annot = $result[0];
  103. $this->assertTrue($annot instanceof Name);
  104. $this->assertEquals("bar", $annot->foo);
  105. }
  106. /**
  107. * @group debug
  108. */
  109. public function testTypicalMethodDocBlock()
  110. {
  111. $parser = $this->createTestParser();
  112. $docblock = <<<DOCBLOCK
  113. /**
  114. * Some nifty method.
  115. *
  116. * @since 2.0
  117. * @Doctrine\Tests\Common\Annotations\Name(foo="bar")
  118. * @param string \$foo This is foo.
  119. * @param mixed \$bar This is bar.
  120. * @return string Foo and bar.
  121. * @This is irrelevant
  122. * @Marker
  123. */
  124. DOCBLOCK;
  125. $result = $parser->parse($docblock);
  126. $this->assertEquals(2, count($result));
  127. $this->assertTrue(isset($result[0]));
  128. $this->assertTrue(isset($result[1]));
  129. $annot = $result[0];
  130. $this->assertTrue($annot instanceof Name);
  131. $this->assertEquals("bar", $annot->foo);
  132. $marker = $result[1];
  133. $this->assertTrue($marker instanceof Marker);
  134. }
  135. public function testAnnotationWithoutConstructor()
  136. {
  137. $parser = $this->createTestParser();
  138. $docblock = <<<DOCBLOCK
  139. /**
  140. * @SomeAnnotationClassNameWithoutConstructor("Some data")
  141. */
  142. DOCBLOCK;
  143. $result = $parser->parse($docblock);
  144. $this->assertEquals(count($result), 1);
  145. $annot = $result[0];
  146. $this->assertNotNull($annot);
  147. $this->assertTrue($annot instanceof SomeAnnotationClassNameWithoutConstructor);
  148. $this->assertNull($annot->name);
  149. $this->assertNotNull($annot->data);
  150. $this->assertEquals($annot->data, "Some data");
  151. $docblock = <<<DOCBLOCK
  152. /**
  153. * @SomeAnnotationClassNameWithoutConstructor(name="Some Name", data = "Some data")
  154. */
  155. DOCBLOCK;
  156. $result = $parser->parse($docblock);
  157. $this->assertEquals(count($result), 1);
  158. $annot = $result[0];
  159. $this->assertNotNull($annot);
  160. $this->assertTrue($annot instanceof SomeAnnotationClassNameWithoutConstructor);
  161. $this->assertEquals($annot->name, "Some Name");
  162. $this->assertEquals($annot->data, "Some data");
  163. $docblock = <<<DOCBLOCK
  164. /**
  165. * @SomeAnnotationClassNameWithoutConstructor(data = "Some data")
  166. */
  167. DOCBLOCK;
  168. $result = $parser->parse($docblock);
  169. $this->assertEquals(count($result), 1);
  170. $annot = $result[0];
  171. $this->assertEquals($annot->data, "Some data");
  172. $this->assertNull($annot->name);
  173. $docblock = <<<DOCBLOCK
  174. /**
  175. * @SomeAnnotationClassNameWithoutConstructor(name = "Some name")
  176. */
  177. DOCBLOCK;
  178. $result = $parser->parse($docblock);
  179. $this->assertEquals(count($result), 1);
  180. $annot = $result[0];
  181. $this->assertEquals($annot->name, "Some name");
  182. $this->assertNull($annot->data);
  183. $docblock = <<<DOCBLOCK
  184. /**
  185. * @SomeAnnotationClassNameWithoutConstructor("Some data")
  186. */
  187. DOCBLOCK;
  188. $result = $parser->parse($docblock);
  189. $this->assertEquals(count($result), 1);
  190. $annot = $result[0];
  191. $this->assertEquals($annot->data, "Some data");
  192. $this->assertNull($annot->name);
  193. $docblock = <<<DOCBLOCK
  194. /**
  195. * @SomeAnnotationClassNameWithoutConstructor("Some data",name = "Some name")
  196. */
  197. DOCBLOCK;
  198. $result = $parser->parse($docblock);
  199. $this->assertEquals(count($result), 1);
  200. $annot = $result[0];
  201. $this->assertEquals($annot->name, "Some name");
  202. $this->assertEquals($annot->data, "Some data");
  203. $docblock = <<<DOCBLOCK
  204. /**
  205. * @SomeAnnotationWithConstructorWithoutParams(name = "Some name")
  206. */
  207. DOCBLOCK;
  208. $result = $parser->parse($docblock);
  209. $this->assertEquals(count($result), 1);
  210. $annot = $result[0];
  211. $this->assertEquals($annot->name, "Some name");
  212. $this->assertEquals($annot->data, "Some data");
  213. $docblock = <<<DOCBLOCK
  214. /**
  215. * @SomeAnnotationClassNameWithoutConstructorAndProperties()
  216. */
  217. DOCBLOCK;
  218. $result = $parser->parse($docblock);
  219. $this->assertEquals(count($result), 1);
  220. $this->assertTrue($result[0] instanceof SomeAnnotationClassNameWithoutConstructorAndProperties);
  221. }
  222. public function testAnnotationTarget()
  223. {
  224. $parser = new DocParser;
  225. $parser->setImports(array(
  226. '__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Fixtures',
  227. ));
  228. $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithValidAnnotationTarget');
  229. $context = 'class ' . $class->getName();
  230. $docComment = $class->getDocComment();
  231. $parser->setTarget(Target::TARGET_CLASS);
  232. $this->assertNotNull($parser->parse($docComment,$context));
  233. $property = $class->getProperty('foo');
  234. $docComment = $property->getDocComment();
  235. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  236. $parser->setTarget(Target::TARGET_PROPERTY);
  237. $this->assertNotNull($parser->parse($docComment,$context));
  238. $method = $class->getMethod('someFunction');
  239. $docComment = $property->getDocComment();
  240. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  241. $parser->setTarget(Target::TARGET_METHOD);
  242. $this->assertNotNull($parser->parse($docComment,$context));
  243. try {
  244. $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass');
  245. $context = 'class ' . $class->getName();
  246. $docComment = $class->getDocComment();
  247. $parser->setTarget(Target::TARGET_CLASS);
  248. $parser->parse($class->getDocComment(),$context);
  249. $this->fail();
  250. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  251. $this->assertNotNull($exc->getMessage());
  252. }
  253. try {
  254. $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod');
  255. $method = $class->getMethod('functionName');
  256. $docComment = $method->getDocComment();
  257. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  258. $parser->setTarget(Target::TARGET_METHOD);
  259. $parser->parse($docComment,$context);
  260. $this->fail();
  261. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  262. $this->assertNotNull($exc->getMessage());
  263. }
  264. try {
  265. $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty');
  266. $property = $class->getProperty('foo');
  267. $docComment = $property->getDocComment();
  268. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  269. $parser->setTarget(Target::TARGET_PROPERTY);
  270. $parser->parse($docComment,$context);
  271. $this->fail();
  272. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  273. $this->assertNotNull($exc->getMessage());
  274. }
  275. }
  276. public function getAnnotationVarTypeProviderValid()
  277. {
  278. //({attribute name}, {attribute value})
  279. return array(
  280. // mixed type
  281. array('mixed', '"String Value"'),
  282. array('mixed', 'true'),
  283. array('mixed', 'false'),
  284. array('mixed', '1'),
  285. array('mixed', '1.2'),
  286. array('mixed', '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll'),
  287. // boolean type
  288. array('boolean', 'true'),
  289. array('boolean', 'false'),
  290. // alias for internal type boolean
  291. array('bool', 'true'),
  292. array('bool', 'false'),
  293. // integer type
  294. array('integer', '0'),
  295. array('integer', '1'),
  296. array('integer', '123456789'),
  297. array('integer', '9223372036854775807'),
  298. // alias for internal type double
  299. array('float', '0.1'),
  300. array('float', '1.2'),
  301. array('float', '123.456'),
  302. // string type
  303. array('string', '"String Value"'),
  304. array('string', '"true"'),
  305. array('string', '"123"'),
  306. // array type
  307. array('array', '{@AnnotationExtendsAnnotationTargetAll}'),
  308. array('array', '{@AnnotationExtendsAnnotationTargetAll,@AnnotationExtendsAnnotationTargetAll}'),
  309. array('arrayOfIntegers', '1'),
  310. array('arrayOfIntegers', '{1}'),
  311. array('arrayOfIntegers', '{1,2,3,4}'),
  312. array('arrayOfAnnotations', '@AnnotationExtendsAnnotationTargetAll'),
  313. array('arrayOfAnnotations', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll}'),
  314. array('arrayOfAnnotations', '{@AnnotationExtendsAnnotationTargetAll, @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll}'),
  315. // annotation instance
  316. array('annotation', '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll'),
  317. array('annotation', '@AnnotationExtendsAnnotationTargetAll'),
  318. );
  319. }
  320. public function getAnnotationVarTypeProviderInvalid()
  321. {
  322. //({attribute name}, {type declared type}, {attribute value} , {given type or class})
  323. return array(
  324. // boolean type
  325. array('boolean','boolean','1','integer'),
  326. array('boolean','boolean','1.2','double'),
  327. array('boolean','boolean','"str"','string'),
  328. array('boolean','boolean','{1,2,3}','array'),
  329. array('boolean','boolean','@Name', 'an instance of Doctrine\Tests\Common\Annotations\Name'),
  330. // alias for internal type boolean
  331. array('bool','bool', '1','integer'),
  332. array('bool','bool', '1.2','double'),
  333. array('bool','bool', '"str"','string'),
  334. array('bool','bool', '{"str"}','array'),
  335. // integer type
  336. array('integer','integer', 'true','boolean'),
  337. array('integer','integer', 'false','boolean'),
  338. array('integer','integer', '1.2','double'),
  339. array('integer','integer', '"str"','string'),
  340. array('integer','integer', '{"str"}','array'),
  341. array('integer','integer', '{1,2,3,4}','array'),
  342. // alias for internal type double
  343. array('float','float', 'true','boolean'),
  344. array('float','float', 'false','boolean'),
  345. array('float','float', '123','integer'),
  346. array('float','float', '"str"','string'),
  347. array('float','float', '{"str"}','array'),
  348. array('float','float', '{12.34}','array'),
  349. array('float','float', '{1,2,3}','array'),
  350. // string type
  351. array('string','string', 'true','boolean'),
  352. array('string','string', 'false','boolean'),
  353. array('string','string', '12','integer'),
  354. array('string','string', '1.2','double'),
  355. array('string','string', '{"str"}','array'),
  356. array('string','string', '{1,2,3,4}','array'),
  357. // annotation instance
  358. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', 'true','boolean'),
  359. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', 'false','boolean'),
  360. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '12','integer'),
  361. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '1.2','double'),
  362. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{"str"}','array'),
  363. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{1,2,3,4}','array'),
  364. array('annotation','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '@Name','an instance of Doctrine\Tests\Common\Annotations\Name'),
  365. );
  366. }
  367. public function getAnnotationVarTypeArrayProviderInvalid()
  368. {
  369. //({attribute name}, {type declared type}, {attribute value} , {given type or class})
  370. return array(
  371. array('arrayOfIntegers','integer', 'true','boolean'),
  372. array('arrayOfIntegers','integer', 'false','boolean'),
  373. array('arrayOfIntegers','integer', '{true,true}','boolean'),
  374. array('arrayOfIntegers','integer', '{1,true}','boolean'),
  375. array('arrayOfIntegers','integer', '{1,2,1.2}','double'),
  376. array('arrayOfIntegers','integer', '{1,2,"str"}','string'),
  377. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', 'true','boolean'),
  378. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', 'false','boolean'),
  379. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,true}','boolean'),
  380. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,true}','boolean'),
  381. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,1.2}','double'),
  382. array('arrayOfAnnotations','Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,@AnnotationExtendsAnnotationTargetAll,"str"}','string'),
  383. );
  384. }
  385. /**
  386. * @dataProvider getAnnotationVarTypeProviderValid
  387. */
  388. public function testAnnotationWithVarType($attribute, $value)
  389. {
  390. $parser = $this->createTestParser();
  391. $context = 'property SomeClassName::$invalidProperty.';
  392. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)',$attribute, $value);
  393. $parser->setTarget(Target::TARGET_PROPERTY);
  394. $result = $parser->parse($docblock, $context);
  395. $this->assertTrue(sizeof($result) === 1);
  396. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType', $result[0]);
  397. $this->assertNotNull($result[0]->$attribute);
  398. }
  399. /**
  400. * @dataProvider getAnnotationVarTypeProviderInvalid
  401. */
  402. public function testAnnotationWithVarTypeError($attribute,$type,$value,$given)
  403. {
  404. $parser = $this->createTestParser();
  405. $context = 'property SomeClassName::invalidProperty.';
  406. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)',$attribute, $value);
  407. $parser->setTarget(Target::TARGET_PROPERTY);
  408. try {
  409. $parser->parse($docblock, $context);
  410. $this->fail();
  411. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  412. $this->assertContains("[Type Error] Attribute \"$attribute\" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType declared on property SomeClassName::invalidProperty. expects a(n) $type, but got $given.", $exc->getMessage());
  413. }
  414. }
  415. /**
  416. * @dataProvider getAnnotationVarTypeArrayProviderInvalid
  417. */
  418. public function testAnnotationWithVarTypeArrayError($attribute,$type,$value,$given)
  419. {
  420. $parser = $this->createTestParser();
  421. $context = 'property SomeClassName::invalidProperty.';
  422. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)',$attribute, $value);
  423. $parser->setTarget(Target::TARGET_PROPERTY);
  424. try {
  425. $parser->parse($docblock, $context);
  426. $this->fail();
  427. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  428. $this->assertContains("[Type Error] Attribute \"$attribute\" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType declared on property SomeClassName::invalidProperty. expects either a(n) $type, or an array of {$type}s, but got $given.", $exc->getMessage());
  429. }
  430. }
  431. /**
  432. * @dataProvider getAnnotationVarTypeProviderValid
  433. */
  434. public function testAnnotationWithAttributes($attribute, $value)
  435. {
  436. $parser = $this->createTestParser();
  437. $context = 'property SomeClassName::$invalidProperty.';
  438. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)',$attribute, $value);
  439. $parser->setTarget(Target::TARGET_PROPERTY);
  440. $result = $parser->parse($docblock, $context);
  441. $this->assertTrue(sizeof($result) === 1);
  442. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes', $result[0]);
  443. $getter = "get".ucfirst($attribute);
  444. $this->assertNotNull($result[0]->$getter());
  445. }
  446. /**
  447. * @dataProvider getAnnotationVarTypeProviderInvalid
  448. */
  449. public function testAnnotationWithAttributesError($attribute,$type,$value,$given)
  450. {
  451. $parser = $this->createTestParser();
  452. $context = 'property SomeClassName::invalidProperty.';
  453. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)',$attribute, $value);
  454. $parser->setTarget(Target::TARGET_PROPERTY);
  455. try {
  456. $parser->parse($docblock, $context);
  457. $this->fail();
  458. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  459. $this->assertContains("[Type Error] Attribute \"$attribute\" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes declared on property SomeClassName::invalidProperty. expects a(n) $type, but got $given.", $exc->getMessage());
  460. }
  461. }
  462. /**
  463. * @dataProvider getAnnotationVarTypeArrayProviderInvalid
  464. */
  465. public function testAnnotationWithAttributesWithVarTypeArrayError($attribute,$type,$value,$given)
  466. {
  467. $parser = $this->createTestParser();
  468. $context = 'property SomeClassName::invalidProperty.';
  469. $docblock = sprintf('@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)',$attribute, $value);
  470. $parser->setTarget(Target::TARGET_PROPERTY);
  471. try {
  472. $parser->parse($docblock, $context);
  473. $this->fail();
  474. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  475. $this->assertContains("[Type Error] Attribute \"$attribute\" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes declared on property SomeClassName::invalidProperty. expects either a(n) $type, or an array of {$type}s, but got $given.", $exc->getMessage());
  476. }
  477. }
  478. public function testAnnotationWithRequiredAttributes()
  479. {
  480. $parser = $this->createTestParser();
  481. $context = 'property SomeClassName::invalidProperty.';
  482. $parser->setTarget(Target::TARGET_PROPERTY);
  483. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes("Some Value", annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)';
  484. $result = $parser->parse($docblock);
  485. $this->assertTrue(sizeof($result) === 1);
  486. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes', $result[0]);
  487. $this->assertEquals("Some Value",$result[0]->getValue());
  488. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation', $result[0]->getAnnot());
  489. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes("Some Value")';
  490. try {
  491. $result = $parser->parse($docblock,$context);
  492. $this->fail();
  493. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  494. $this->assertContains('Attribute "annot" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes declared on property SomeClassName::invalidProperty. expects a(n) Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation. This value should not be null.', $exc->getMessage());
  495. }
  496. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)';
  497. try {
  498. $result = $parser->parse($docblock,$context);
  499. $this->fail();
  500. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  501. $this->assertContains('Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes declared on property SomeClassName::invalidProperty. expects a(n) string. This value should not be null.', $exc->getMessage());
  502. }
  503. }
  504. public function testAnnotationWithRequiredAttributesWithoutContructor()
  505. {
  506. $parser = $this->createTestParser();
  507. $context = 'property SomeClassName::invalidProperty.';
  508. $parser->setTarget(Target::TARGET_PROPERTY);
  509. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor("Some Value", annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)';
  510. $result = $parser->parse($docblock);
  511. $this->assertTrue(sizeof($result) === 1);
  512. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor', $result[0]);
  513. $this->assertEquals("Some Value", $result[0]->value);
  514. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation', $result[0]->annot);
  515. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor("Some Value")';
  516. try {
  517. $result = $parser->parse($docblock,$context);
  518. $this->fail();
  519. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  520. $this->assertContains('Attribute "annot" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor declared on property SomeClassName::invalidProperty. expects a(n) Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation. This value should not be null.', $exc->getMessage());
  521. }
  522. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)';
  523. try {
  524. $result = $parser->parse($docblock,$context);
  525. $this->fail();
  526. } catch (\Doctrine\Common\Annotations\AnnotationException $exc) {
  527. $this->assertContains('Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutContructor declared on property SomeClassName::invalidProperty. expects a(n) string. This value should not be null.', $exc->getMessage());
  528. }
  529. }
  530. /**
  531. * @expectedException Doctrine\Common\Annotations\AnnotationException
  532. * @expectedExceptionMessage Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accept only [ONE, TWO, THREE], but got FOUR.
  533. */
  534. public function testAnnotationEnumeratorException()
  535. {
  536. $parser = $this->createTestParser();
  537. $context = 'property SomeClassName::invalidProperty.';
  538. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum("FOUR")';
  539. $parser->setIgnoreNotImportedAnnotations(false);
  540. $parser->setTarget(Target::TARGET_PROPERTY);
  541. $parser->parse($docblock, $context);
  542. }
  543. /**
  544. * @expectedException Doctrine\Common\Annotations\AnnotationException
  545. * @expectedExceptionMessage Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral declared on property SomeClassName::invalidProperty. accept only [AnnotationEnumLiteral::ONE, AnnotationEnumLiteral::TWO, AnnotationEnumLiteral::THREE], but got 4.
  546. */
  547. public function testAnnotationEnumeratorLiteralException()
  548. {
  549. $parser = $this->createTestParser();
  550. $context = 'property SomeClassName::invalidProperty.';
  551. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral(4)';
  552. $parser->setIgnoreNotImportedAnnotations(false);
  553. $parser->setTarget(Target::TARGET_PROPERTY);
  554. $parser->parse($docblock, $context);
  555. }
  556. /**
  557. * @expectedException \InvalidArgumentException
  558. * @expectedExceptionMessage @Enum supports only scalar values "array" given.
  559. */
  560. public function testAnnotationEnumInvalidTypeDeclarationException()
  561. {
  562. $parser = $this->createTestParser();
  563. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumInvalid("foo")';
  564. $parser->setIgnoreNotImportedAnnotations(false);
  565. $parser->parse($docblock);
  566. }
  567. /**
  568. * @expectedException \InvalidArgumentException
  569. * @expectedExceptionMessage Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".
  570. */
  571. public function testAnnotationEnumInvalidLiteralDeclarationException()
  572. {
  573. $parser = $this->createTestParser();
  574. $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteralInvalid("foo")';
  575. $parser->setIgnoreNotImportedAnnotations(false);
  576. $parser->parse($docblock);
  577. }
  578. public function getConstantsProvider()
  579. {
  580. $provider[] = array(
  581. '@AnnotationWithConstants(PHP_EOL)',
  582. PHP_EOL
  583. );
  584. $provider[] = array(
  585. '@AnnotationWithConstants(AnnotationWithConstants::INTEGER)',
  586. AnnotationWithConstants::INTEGER
  587. );
  588. $provider[] = array(
  589. '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants(AnnotationWithConstants::STRING)',
  590. AnnotationWithConstants::STRING
  591. );
  592. $provider[] = array(
  593. '@AnnotationWithConstants(Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants::FLOAT)',
  594. AnnotationWithConstants::FLOAT
  595. );
  596. $provider[] = array(
  597. '@AnnotationWithConstants(ClassWithConstants::SOME_VALUE)',
  598. ClassWithConstants::SOME_VALUE
  599. );
  600. $provider[] = array(
  601. '@AnnotationWithConstants(Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants::SOME_VALUE)',
  602. ClassWithConstants::SOME_VALUE
  603. );
  604. $provider[] = array(
  605. '@AnnotationWithConstants(IntefaceWithConstants::SOME_VALUE)',
  606. IntefaceWithConstants::SOME_VALUE
  607. );
  608. $provider[] = array(
  609. '@AnnotationWithConstants(\Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants::SOME_VALUE)',
  610. IntefaceWithConstants::SOME_VALUE
  611. );
  612. $provider[] = array(
  613. '@AnnotationWithConstants({AnnotationWithConstants::STRING, AnnotationWithConstants::INTEGER, AnnotationWithConstants::FLOAT})',
  614. array(AnnotationWithConstants::STRING, AnnotationWithConstants::INTEGER, AnnotationWithConstants::FLOAT)
  615. );
  616. $provider[] = array(
  617. '@AnnotationWithConstants({
  618. AnnotationWithConstants::STRING = AnnotationWithConstants::INTEGER
  619. })',
  620. array(AnnotationWithConstants::STRING => AnnotationWithConstants::INTEGER)
  621. );
  622. $provider[] = array(
  623. '@AnnotationWithConstants({
  624. Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants::SOME_KEY = AnnotationWithConstants::INTEGER
  625. })',
  626. array(IntefaceWithConstants::SOME_KEY => AnnotationWithConstants::INTEGER)
  627. );
  628. $provider[] = array(
  629. '@AnnotationWithConstants({
  630. \Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants::SOME_KEY = AnnotationWithConstants::INTEGER
  631. })',
  632. array(IntefaceWithConstants::SOME_KEY => AnnotationWithConstants::INTEGER)
  633. );
  634. $provider[] = array(
  635. '@AnnotationWithConstants({
  636. AnnotationWithConstants::STRING = AnnotationWithConstants::INTEGER,
  637. ClassWithConstants::SOME_KEY = ClassWithConstants::SOME_VALUE,
  638. Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants::SOME_KEY = IntefaceWithConstants::SOME_VALUE
  639. })',
  640. array(
  641. AnnotationWithConstants::STRING => AnnotationWithConstants::INTEGER,
  642. ClassWithConstants::SOME_KEY => ClassWithConstants::SOME_VALUE,
  643. ClassWithConstants::SOME_KEY => IntefaceWithConstants::SOME_VALUE
  644. )
  645. );
  646. return $provider;
  647. }
  648. /**
  649. * @dataProvider getConstantsProvider
  650. */
  651. public function testSupportClassConstants($docblock, $expected)
  652. {
  653. $parser = $this->createTestParser();
  654. $parser->setImports(array(
  655. 'classwithconstants' => 'Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants',
  656. 'intefacewithconstants' => 'Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants',
  657. 'annotationwithconstants' => 'Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants'
  658. ));
  659. $result = $parser->parse($docblock);
  660. $this->assertInstanceOf('\Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants', $annotation = $result[0]);
  661. $this->assertEquals($expected, $annotation->value);
  662. }
  663. /**
  664. * @expectedException Doctrine\Common\Annotations\AnnotationException
  665. * @expectedExceptionMessage The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}.
  666. */
  667. public function testWithoutConstructorWhenIsNotDefaultValue()
  668. {
  669. $parser = $this->createTestParser();
  670. $docblock = <<<DOCBLOCK
  671. /**
  672. * @SomeAnnotationClassNameWithoutConstructorAndProperties("Foo")
  673. */
  674. DOCBLOCK;
  675. $parser->setTarget(Target::TARGET_CLASS);
  676. $parser->parse($docblock);
  677. }
  678. /**
  679. * @expectedException Doctrine\Common\Annotations\AnnotationException
  680. * @expectedExceptionMessage The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}.
  681. */
  682. public function testWithoutConstructorWhenHasNoProperties()
  683. {
  684. $parser = $this->createTestParser();
  685. $docblock = <<<DOCBLOCK
  686. /**
  687. * @SomeAnnotationClassNameWithoutConstructorAndProperties(value = "Foo")
  688. */
  689. DOCBLOCK;
  690. $parser->setTarget(Target::TARGET_CLASS);
  691. $parser->parse($docblock);
  692. }
  693. /**
  694. * @expectedException Doctrine\Common\Annotations\AnnotationException
  695. * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
  696. */
  697. public function testAnnotationTargetSyntaxError()
  698. {
  699. $parser = $this->createTestParser();
  700. $context = 'class ' . 'SomeClassName';
  701. $docblock = <<<DOCBLOCK
  702. /**
  703. * @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError()
  704. */
  705. DOCBLOCK;
  706. $parser->setTarget(Target::TARGET_CLASS);
  707. $parser->parse($docblock,$context);
  708. }
  709. /**
  710. * @expectedException \InvalidArgumentException
  711. * @expectedExceptionMessage Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, ANNOTATION]
  712. */
  713. public function testAnnotationWithInvalidTargetDeclarationError()
  714. {
  715. $parser = $this->createTestParser();
  716. $context = 'class ' . 'SomeClassName';
  717. $docblock = <<<DOCBLOCK
  718. /**
  719. * @AnnotationWithInvalidTargetDeclaration()
  720. */
  721. DOCBLOCK;
  722. $parser->setTarget(Target::TARGET_CLASS);
  723. $parser->parse($docblock,$context);
  724. }
  725. /**
  726. * @expectedException \InvalidArgumentException
  727. * @expectedExceptionMessage @Target expects either a string value, or an array of strings, "NULL" given.
  728. */
  729. public function testAnnotationWithTargetEmptyError()
  730. {
  731. $parser = $this->createTestParser();
  732. $context = 'class ' . 'SomeClassName';
  733. $docblock = <<<DOCBLOCK
  734. /**
  735. * @AnnotationWithTargetEmpty()
  736. */
  737. DOCBLOCK;
  738. $parser->setTarget(Target::TARGET_CLASS);
  739. $parser->parse($docblock,$context);
  740. }
  741. /**
  742. * @group DDC-575
  743. */
  744. public function testRegressionDDC575()
  745. {
  746. $parser = $this->createTestParser();
  747. $docblock = <<<DOCBLOCK
  748. /**
  749. * @Name
  750. *
  751. * Will trigger error.
  752. */
  753. DOCBLOCK;
  754. $result = $parser->parse($docblock);
  755. $this->assertInstanceOf("Doctrine\Tests\Common\Annotations\Name", $result[0]);
  756. $docblock = <<<DOCBLOCK
  757. /**
  758. * @Name
  759. * @Marker
  760. *
  761. * Will trigger error.
  762. */
  763. DOCBLOCK;
  764. $result = $parser->parse($docblock);
  765. $this->assertInstanceOf("Doctrine\Tests\Common\Annotations\Name", $result[0]);
  766. }
  767. /**
  768. * @group DDC-77
  769. */
  770. public function testAnnotationWithoutClassIsIgnoredWithoutWarning()
  771. {
  772. $parser = new DocParser();
  773. $parser->setIgnoreNotImportedAnnotations(true);
  774. $result = $parser->parse("@param");
  775. $this->assertEquals(0, count($result));
  776. }
  777. /**
  778. * @expectedException Doctrine\Common\Annotations\AnnotationException
  779. * @expectedExceptionMessage Expected PlainValue, got ''' at position 10.
  780. */
  781. public function testAnnotationDontAcceptSingleQuotes()
  782. {
  783. $parser = $this->createTestParser();
  784. $parser->parse("@Name(foo='bar')");
  785. }
  786. /**
  787. * @group DCOM-41
  788. */
  789. public function testAnnotationDoesntThrowExceptionWhenAtSignIsNotFollowedByIdentifier()
  790. {
  791. $parser = new DocParser();
  792. $result = $parser->parse("'@'");
  793. $this->assertEquals(0, count($result));
  794. }
  795. /**
  796. * @group DCOM-41
  797. * @expectedException Doctrine\Common\Annotations\AnnotationException
  798. */
  799. public function testAnnotationThrowsExceptionWhenAtSignIsNotFollowedByIdentifierInNestedAnnotation()
  800. {
  801. $parser = new DocParser();
  802. $result = $parser->parse("@Doctrine\Tests\Common\Annotations\Name(@')");
  803. }
  804. /**
  805. * @group DCOM-56
  806. */
  807. public function testAutoloadAnnotation()
  808. {
  809. $this->assertFalse(class_exists('Doctrine\Tests\Common\Annotations\Fixture\Annotation\Autoload', false), 'Pre-condition: Doctrine\Tests\Common\Annotations\Fixture\Annotation\Autoload not allowed to be loaded.');
  810. $parser = new DocParser();
  811. AnnotationRegistry::registerAutoloadNamespace('Doctrine\Tests\Common\Annotations\Fixtures\Annotation', __DIR__ . '/../../../../');
  812. $parser->setImports(array(
  813. 'autoload' => 'Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Autoload',
  814. ));
  815. $annotations = $parser->parse('@Autoload');
  816. $this->assertEquals(1, count($annotations));
  817. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Autoload', $annotations[0]);
  818. }
  819. public function createTestParser()
  820. {
  821. $parser = new DocParser();
  822. $parser->setIgnoreNotImportedAnnotations(true);
  823. $parser->setImports(array(
  824. 'name' => 'Doctrine\Tests\Common\Annotations\Name',
  825. '__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations',
  826. ));
  827. return $parser;
  828. }
  829. /**
  830. * @group DDC-78
  831. * @expectedException Doctrine\Common\Annotations\AnnotationException
  832. * @expectedExceptionMessage Expected PlainValue, got ''' at position 10 in class \Doctrine\Tests\Common\Annotations\Name
  833. */
  834. public function testSyntaxErrorWithContextDescription()
  835. {
  836. $parser = $this->createTestParser();
  837. $parser->parse("@Name(foo='bar')", "class \Doctrine\Tests\Common\Annotations\Name");
  838. }
  839. /**
  840. * @group DDC-183
  841. */
  842. public function testSyntaxErrorWithUnknownCharacters()
  843. {
  844. $docblock = <<<DOCBLOCK
  845. /**
  846. * @test at.
  847. */
  848. class A {
  849. }
  850. DOCBLOCK;
  851. //$lexer = new \Doctrine\Common\Annotations\Lexer();
  852. //$lexer->setInput(trim($docblock, '/ *'));
  853. //var_dump($lexer);
  854. try {
  855. $parser = $this->createTestParser();
  856. $result = $parser->parse($docblock);
  857. } catch (Exception $e) {
  858. $this->fail($e->getMessage());
  859. }
  860. }
  861. /**
  862. * @group DCOM-14
  863. */
  864. public function testIgnorePHPDocThrowTag()
  865. {
  866. $docblock = <<<DOCBLOCK
  867. /**
  868. * @throws \RuntimeException
  869. */
  870. class A {
  871. }
  872. DOCBLOCK;
  873. try {
  874. $parser = $this->createTestParser();
  875. $result = $parser->parse($docblock);
  876. } catch (Exception $e) {
  877. $this->fail($e->getMessage());
  878. }
  879. }
  880. /**
  881. * @group DCOM-38
  882. */
  883. public function testCastInt()
  884. {
  885. $parser = $this->createTestParser();
  886. $result = $parser->parse("@Name(foo=1234)");
  887. $annot = $result[0];
  888. $this->assertInternalType('int', $annot->foo);
  889. }
  890. /**
  891. * @group DCOM-38
  892. */
  893. public function testCastNegativeInt()
  894. {
  895. $parser = $this->createTestParser();
  896. $result = $parser->parse("@Name(foo=-1234)");
  897. $annot = $result[0];
  898. $this->assertInternalType('int', $annot->foo);
  899. }
  900. /**
  901. * @group DCOM-38
  902. */
  903. public function testCastFloat()
  904. {
  905. $parser = $this->createTestParser();
  906. $result = $parser->parse("@Name(foo=1234.345)");
  907. $annot = $result[0];
  908. $this->assertInternalType('float', $annot->foo);
  909. }
  910. /**
  911. * @group DCOM-38
  912. */
  913. public function testCastNegativeFloat()
  914. {
  915. $parser = $this->createTestParser();
  916. $result = $parser->parse("@Name(foo=-1234.345)");
  917. $annot = $result[0];
  918. $this->assertInternalType('float', $annot->foo);
  919. $result = $parser->parse("@Marker(-1234.345)");
  920. $annot = $result[0];
  921. $this->assertInternalType('float', $annot->value);
  922. }
  923. public function testReservedKeywordsInAnnotations()
  924. {
  925. $parser = $this->createTestParser();
  926. $result = $parser->parse('@Doctrine\Tests\Common\Annotations\True');
  927. $this->assertTrue($result[0] instanceof True);
  928. $result = $parser->parse('@Doctrine\Tests\Common\Annotations\False');
  929. $this->assertTrue($result[0] instanceof False);
  930. $result = $parser->parse('@Doctrine\Tests\Common\Annotations\Null');
  931. $this->assertTrue($result[0] instanceof Null);
  932. $result = $parser->parse('@True');
  933. $this->assertTrue($result[0] instanceof True);
  934. $result = $parser->parse('@False');
  935. $this->assertTrue($result[0] instanceof False);
  936. $result = $parser->parse('@Null');
  937. $this->assertTrue($result[0] instanceof Null);
  938. }
  939. /**
  940. * @expectedException Doctrine\Common\Annotations\AnnotationException
  941. * @expectedExceptionMessage [Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared on some class does not have a property named "invalidaProperty". Available properties: data, name
  942. */
  943. public function testSetValuesExeption()
  944. {
  945. $docblock = <<<DOCBLOCK
  946. /**
  947. * @SomeAnnotationClassNameWithoutConstructor(invalidaProperty = "Some val")
  948. */
  949. DOCBLOCK;
  950. $this->createTestParser()->parse($docblock, 'some class');
  951. }
  952. /**
  953. * @expectedException Doctrine\Common\Annotations\AnnotationException
  954. * @expectedExceptionMessage [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_IDENTIFIER or Doctrine\Common\Annotations\DocLexer::T_TRUE or Doctrine\Common\Annotations\DocLexer::T_FALSE or Doctrine\Common\Annotations\DocLexer::T_NULL, got '3.42' at position 5.
  955. */
  956. public function testInvalidIdentifierInAnnotation()
  957. {
  958. $parser = $this->createTestParser();
  959. $parser->parse('@Foo\3.42');
  960. }
  961. public function testTrailingCommaIsAllowed()
  962. {
  963. $parser = $this->createTestParser();
  964. $annots = $parser->parse('@Name({
  965. "Foo",
  966. "Bar",
  967. })');
  968. $this->assertEquals(1, count($annots));
  969. $this->assertEquals(array('Foo', 'Bar'), $annots[0]->value);
  970. }
  971. public function testDefaultAnnotationValueIsNotOverwritten()
  972. {
  973. $parser = $this->createTestParser();
  974. $annots = $parser->parse('@Doctrine\Tests\Common\Annotations\Fixtures\Annotation\AnnotWithDefaultValue');
  975. $this->assertEquals(1, count($annots));
  976. $this->assertEquals('bar', $annots[0]->foo);
  977. }
  978. public function testArrayWithColon()
  979. {
  980. $parser = $this->createTestParser();
  981. $annots = $parser->parse('@Name({"foo": "bar"})');
  982. $this->assertEquals(1, count($annots));
  983. $this->assertEquals(array('foo' => 'bar'), $annots[0]->value);
  984. }
  985. /**
  986. * @expectedException Doctrine\Common\Annotations\AnnotationException
  987. * @expectedExceptionMessage [Semantical Error] Couldn't find constant foo.
  988. */
  989. public function testInvalidContantName()
  990. {
  991. $parser = $this->createTestParser();
  992. $parser->parse('@Name(foo: "bar")');
  993. }
  994. /**
  995. * Tests parsing empty arrays.
  996. */
  997. public function testEmptyArray()
  998. {
  999. $parser = $this->createTestParser();
  1000. $annots = $parser->parse('@Name({"foo": {}})');
  1001. $this->assertEquals(1, count($annots));
  1002. $this->assertEquals(array('foo' => array()), $annots[0]->value);
  1003. }
  1004. }
  1005. /** @Annotation */
  1006. class SomeAnnotationClassNameWithoutConstructor
  1007. {
  1008. public $data;
  1009. public $name;
  1010. }
  1011. /** @Annotation */
  1012. class SomeAnnotationWithConstructorWithoutParams
  1013. {
  1014. function __construct()
  1015. {
  1016. $this->data = "Some data";
  1017. }
  1018. public $data;
  1019. public $name;
  1020. }
  1021. /** @Annotation */
  1022. class SomeAnnotationClassNameWithoutConstructorAndProperties{}
  1023. /**
  1024. * @Annotation
  1025. * @Target("Foo")
  1026. */
  1027. class AnnotationWithInvalidTargetDeclaration{}
  1028. /**
  1029. * @Annotation
  1030. * @Target
  1031. */
  1032. class AnnotationWithTargetEmpty{}
  1033. /** @Annotation */
  1034. class AnnotationExtendsAnnotationTargetAll extends \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll
  1035. {
  1036. }
  1037. /** @Annotation */
  1038. class Name extends \Doctrine\Common\Annotations\Annotation {
  1039. public $foo;
  1040. }
  1041. /** @Annotation */
  1042. class Marker {
  1043. public $value;
  1044. }
  1045. /** @Annotation */
  1046. class True {}
  1047. /** @Annotation */
  1048. class False {}
  1049. /** @Annotation */
  1050. class Null {}
  1051. namespace Doctrine\Tests\Common\Annotations\FooBar;
  1052. /** @Annotation */
  1053. class Name extends \Doctrine\Common\Annotations\Annotation {
  1054. }