/vendor/doctrine-common/tests/Doctrine/Tests/Common/Annotations/Regression/BCAnnotationReaderTest.php

https://github.com/proclamo/txinbometro · PHP · 258 lines · 148 code · 41 blank · 69 comment · 2 complexity · 74be7a8c3785a7cc3463325cfe3f6458 MD5 · raw file

  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations\Regression;
  3. use ReflectionClass;
  4. use Doctrine\Common\Annotations\AnnotationReader;
  5. use Doctrine\Common\Annotations\IndexedReader;
  6. use Doctrine\Tests\Common\Annotations\DummyJoinTable;
  7. use Doctrine\Tests\Common\Annotations\DummyId;
  8. /**
  9. * Important: This class needs a different namespace than Doctrine\Tests\Common\Annotations\
  10. * to be able to really test the set default annotation namespace functionality.
  11. */
  12. class BCAnnotationReaderTest extends \Doctrine\Tests\DoctrineTestCase
  13. {
  14. public function testAnnotations()
  15. {
  16. $reader = $this->createAnnotationReader();
  17. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\DummyClass');
  18. $classAnnots = $reader->getClassAnnotations($class);
  19. $annotName = 'Doctrine\Tests\Common\Annotations\DummyAnnotation';
  20. $this->assertEquals(1, count($classAnnots));
  21. $this->assertTrue($classAnnots[$annotName] instanceof \Doctrine\Tests\Common\Annotations\DummyAnnotation); // no use!
  22. $this->assertEquals("hello", $classAnnots[$annotName]->dummyValue);
  23. $field1Prop = $class->getProperty('field1');
  24. $propAnnots = $reader->getPropertyAnnotations($field1Prop);
  25. $this->assertEquals(1, count($propAnnots));
  26. $this->assertTrue($propAnnots[$annotName] instanceof \Doctrine\Tests\Common\Annotations\DummyAnnotation); // no use!
  27. $this->assertEquals("fieldHello", $propAnnots[$annotName]->dummyValue);
  28. $getField1Method = $class->getMethod('getField1');
  29. $methodAnnots = $reader->getMethodAnnotations($getField1Method);
  30. $this->assertEquals(1, count($methodAnnots));
  31. $this->assertTrue($methodAnnots[$annotName] instanceof \Doctrine\Tests\Common\Annotations\DummyAnnotation); // no use!
  32. $this->assertEquals(array(1, 2, "three"), $methodAnnots[$annotName]->value);
  33. $field2Prop = $class->getProperty('field2');
  34. $propAnnots = $reader->getPropertyAnnotations($field2Prop);
  35. $this->assertEquals(1, count($propAnnots));
  36. $this->assertTrue(isset($propAnnots['Doctrine\Tests\Common\Annotations\DummyJoinTable']));
  37. $joinTableAnnot = $propAnnots['Doctrine\Tests\Common\Annotations\DummyJoinTable'];
  38. $this->assertEquals(1, count($joinTableAnnot->joinColumns));
  39. $this->assertEquals(1, count($joinTableAnnot->inverseJoinColumns));
  40. $this->assertTrue($joinTableAnnot->joinColumns[0] instanceof \Doctrine\Tests\Common\Annotations\DummyJoinColumn); // no use!
  41. $this->assertTrue($joinTableAnnot->inverseJoinColumns[0] instanceof \Doctrine\Tests\Common\Annotations\DummyJoinColumn); // no use!
  42. $this->assertEquals('col1', $joinTableAnnot->joinColumns[0]->name);
  43. $this->assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName);
  44. $this->assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name);
  45. $this->assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName);
  46. $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  47. $this->assertEquals('', $dummyAnnot->dummyValue);
  48. $this->assertEquals(array(1, 2, 'three'), $dummyAnnot->value);
  49. $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  50. $this->assertEquals('fieldHello', $dummyAnnot->dummyValue);
  51. $classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  52. $this->assertEquals('hello', $classAnnot->dummyValue);
  53. }
  54. /**
  55. * @group regression
  56. */
  57. public function testMultipleAnnotationsOnSameLine()
  58. {
  59. $reader = $this->createAnnotationReader();
  60. $class = new ReflectionClass('\Doctrine\Tests\Common\Annotations\Regression\DummyClass2');
  61. $annotations = $reader->getPropertyAnnotations($class->getProperty('id'));
  62. $this->assertEquals(3, count($annotations));
  63. }
  64. public function testCustomAnnotationCreationFunction()
  65. {
  66. $reader = $this->createAnnotationReader();
  67. $reader->setAnnotationCreationFunction(function($name, $values) {
  68. if ($name == 'Doctrine\Tests\Common\Annotations\DummyAnnotation') {
  69. $a = new CustomDummyAnnotationClass;
  70. $a->setDummyValue($values['dummyValue']);
  71. return $a;
  72. }
  73. });
  74. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\DummyClass');
  75. $classAnnots = $reader->getClassAnnotations($class);
  76. $this->assertTrue(isset($classAnnots['Doctrine\Tests\Common\Annotations\Regression\CustomDummyAnnotationClass']));
  77. $annot = $classAnnots['Doctrine\Tests\Common\Annotations\Regression\CustomDummyAnnotationClass'];
  78. $this->assertEquals('hello', $annot->getDummyValue());
  79. }
  80. public function testNonAnnotationProblem()
  81. {
  82. $reader = $this->createAnnotationReader();
  83. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\DummyClassNonAnnotationProblem');
  84. $annotations = $reader->getPropertyAnnotations($class->getProperty('foo'));
  85. $this->assertArrayHasKey('Doctrine\Tests\Common\Annotations\DummyAnnotation', $annotations);
  86. $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\DummyAnnotation', $annotations['Doctrine\Tests\Common\Annotations\DummyAnnotation']);
  87. }
  88. /**
  89. * @return AnnotationReader
  90. */
  91. public function createAnnotationReader()
  92. {
  93. $reader = new IndexedReader(new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache));
  94. $reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
  95. $reader->setEnableParsePhpImports(false);
  96. return $reader;
  97. }
  98. public function testEmailAsAnnotation()
  99. {
  100. $reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
  101. $reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
  102. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\DummyClassWithEmail');
  103. $classAnnots = $reader->getClassAnnotations($class);
  104. $this->assertEquals(1, count($classAnnots));
  105. }
  106. public function testNamespaceAliasedAnnotations()
  107. {
  108. $reader = new IndexedReader(new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache));
  109. $reader->setAnnotationNamespaceAlias('Doctrine\Tests\Common\Annotations\\', 'alias');
  110. $reflClass = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\AliasNamespace');
  111. $result = $reader->getPropertyAnnotations($reflClass->getProperty('bar'));
  112. $this->assertEquals(1, count($result));
  113. $annot = $result['Doctrine\Tests\Common\Annotations\Name'];
  114. $this->assertTrue($annot instanceof \Doctrine\Tests\Common\Annotations\Name); // no use!
  115. $this->assertEquals('bar', $annot->foo);
  116. }
  117. /**
  118. * @group DCOM-4
  119. */
  120. public function testNamespaceAliasAnnotationWithSeparator()
  121. {
  122. $reader = new IndexedReader(new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache));
  123. $reader->setAnnotationNamespaceAlias('Doctrine\Tests\Common\\', 'alias');
  124. $reflClass = new ReflectionClass('Doctrine\Tests\Common\Annotations\Regression\AliasNamespace');
  125. $result = $reader->getPropertyAnnotations($reflClass->getProperty('foo'));
  126. $this->assertEquals(1, count($result));
  127. $annot = $result['Doctrine\Tests\Common\Annotations\Name'];
  128. $this->assertTrue($annot instanceof \Doctrine\Tests\Common\Annotations\Name); // no use!
  129. $this->assertEquals('bar', $annot->foo);
  130. }
  131. }
  132. class CustomDummyAnnotationClass {
  133. private $dummyValue;
  134. public function setDummyValue($value) {
  135. $this->dummyValue = $value;
  136. }
  137. public function getDummyValue() {
  138. return $this->dummyValue;
  139. }
  140. }
  141. class AliasNamespace
  142. {
  143. /**
  144. * @alias:Name(foo="bar")
  145. */
  146. public $bar;
  147. /**
  148. * @alias:Annotations\Name(foo="bar")
  149. */
  150. public $foo;
  151. }
  152. /**
  153. * A description of this class.
  154. *
  155. * Let's see if the parser recognizes that this @ is not really referring to an
  156. * annotation. Also make sure that @var \ is not concated to "@var\is".
  157. *
  158. * Copy of the class one namespace up to avoid matches against __NAMESPACE__
  159. *
  160. * @author robo
  161. * @since 2.0
  162. * @DummyAnnotation(dummyValue="hello")
  163. */
  164. class DummyClass {
  165. /**
  166. * A nice property.
  167. *
  168. * @var mixed
  169. * @DummyAnnotation(dummyValue="fieldHello")
  170. */
  171. private $field1;
  172. /**
  173. * @DummyJoinTable(name="join_table",
  174. * joinColumns={@DummyJoinColumn(name="col1", referencedColumnName="col2")},
  175. * inverseJoinColumns={
  176. * @DummyJoinColumn(name="col3", referencedColumnName="col4")
  177. * })
  178. */
  179. private $field2;
  180. /**
  181. * Gets the value of field1.
  182. *
  183. * @return mixed
  184. * @DummyAnnotation({1,2,"three"})
  185. */
  186. public function getField1() {
  187. }
  188. }
  189. /**
  190. * @ignoreAnnotation("var")
  191. */
  192. class DummyClass2 {
  193. /**
  194. * @DummyId @DummyColumn(type="integer") @DummyGeneratedValue
  195. * @var integer
  196. */
  197. private $id;
  198. }
  199. /**
  200. * @ignoreAnnotation({"since", "var"})
  201. */
  202. class DummyClassNonAnnotationProblem
  203. {
  204. /**
  205. * @DummyAnnotation
  206. *
  207. * @var \Test
  208. * @since 0.1
  209. */
  210. public $foo;
  211. }
  212. /**
  213. * @DummyAnnotation Foo bar <foobar@1domain.com>
  214. */
  215. class DummyClassWithEmail
  216. {
  217. }