PageRenderTime 166ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/doctrine-common/tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 282 lines | 273 code | 6 blank | 3 comment | 0 complexity | 20c9531727b22014838d1414eac6e2ca MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations;
  3. use ReflectionClass, Doctrine\Common\Annotations\AnnotationReader;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. class AnnotationReaderTest extends \Doctrine\Tests\DoctrineTestCase
  6. {
  7. public function testAnnotations()
  8. {
  9. $reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
  10. $reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
  11. $this->assertFalse($reader->getAutoloadAnnotations());
  12. $reader->setAutoloadAnnotations(true);
  13. $this->assertTrue($reader->getAutoloadAnnotations());
  14. $reader->setAutoloadAnnotations(false);
  15. $this->assertFalse($reader->getAutoloadAnnotations());
  16. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClass');
  17. $classAnnots = $reader->getClassAnnotations($class);
  18. $annotName = 'Doctrine\Tests\Common\Annotations\DummyAnnotation';
  19. $this->assertEquals(1, count($classAnnots));
  20. $this->assertTrue($classAnnots[$annotName] instanceof DummyAnnotation);
  21. $this->assertEquals("hello", $classAnnots[$annotName]->dummyValue);
  22. $field1Prop = $class->getProperty('field1');
  23. $propAnnots = $reader->getPropertyAnnotations($field1Prop);
  24. $this->assertEquals(1, count($propAnnots));
  25. $this->assertTrue($propAnnots[$annotName] instanceof DummyAnnotation);
  26. $this->assertEquals("fieldHello", $propAnnots[$annotName]->dummyValue);
  27. $getField1Method = $class->getMethod('getField1');
  28. $methodAnnots = $reader->getMethodAnnotations($getField1Method);
  29. $this->assertEquals(1, count($methodAnnots));
  30. $this->assertTrue($methodAnnots[$annotName] instanceof DummyAnnotation);
  31. $this->assertEquals(array(1, 2, "three"), $methodAnnots[$annotName]->value);
  32. $field2Prop = $class->getProperty('field2');
  33. $propAnnots = $reader->getPropertyAnnotations($field2Prop);
  34. $this->assertEquals(1, count($propAnnots));
  35. $this->assertTrue(isset($propAnnots['Doctrine\Tests\Common\Annotations\DummyJoinTable']));
  36. $joinTableAnnot = $propAnnots['Doctrine\Tests\Common\Annotations\DummyJoinTable'];
  37. $this->assertEquals(1, count($joinTableAnnot->joinColumns));
  38. $this->assertEquals(1, count($joinTableAnnot->inverseJoinColumns));
  39. $this->assertTrue($joinTableAnnot->joinColumns[0] instanceof DummyJoinColumn);
  40. $this->assertTrue($joinTableAnnot->inverseJoinColumns[0] instanceof DummyJoinColumn);
  41. $this->assertEquals('col1', $joinTableAnnot->joinColumns[0]->name);
  42. $this->assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName);
  43. $this->assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name);
  44. $this->assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName);
  45. $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  46. $this->assertEquals('', $dummyAnnot->dummyValue);
  47. $this->assertEquals(array(1, 2, 'three'), $dummyAnnot->value);
  48. $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  49. $this->assertEquals('fieldHello', $dummyAnnot->dummyValue);
  50. $classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
  51. $this->assertEquals('hello', $classAnnot->dummyValue);
  52. }
  53. public function testClassSyntaxErrorContext()
  54. {
  55. $this->setExpectedException(
  56. "Doctrine\Common\Annotations\AnnotationException",
  57. "[Syntax Error] Expected Doctrine\Common\Annotations\Lexer::T_IDENTIFIER, got ')' at position 18 in class ".
  58. "Doctrine\Tests\Common\Annotations\DummyClassSyntaxError."
  59. );
  60. $class = new ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassSyntaxError');
  61. $reader = $this->createAnnotationReader();
  62. $reader->getClassAnnotations($class);
  63. }
  64. public function testMethodSyntaxErrorContext()
  65. {
  66. $this->setExpectedException(
  67. "Doctrine\Common\Annotations\AnnotationException",
  68. "[Syntax Error] Expected Doctrine\Common\Annotations\Lexer::T_IDENTIFIER, got ')' at position 18 in ".
  69. "method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo()."
  70. );
  71. $class = new ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError');
  72. $method = $class->getMethod('foo');
  73. $reader = $this->createAnnotationReader();
  74. $reader->getMethodAnnotations($method);
  75. }
  76. public function testPropertySyntaxErrorContext()
  77. {
  78. $this->setExpectedException(
  79. "Doctrine\Common\Annotations\AnnotationException",
  80. "[Syntax Error] Expected Doctrine\Common\Annotations\Lexer::T_IDENTIFIER, got ')' at position 18 in ".
  81. "property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::\$foo."
  82. );
  83. $class = new ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError');
  84. $property = $class->getProperty('foo');
  85. $reader = $this->createAnnotationReader();
  86. $reader->getPropertyAnnotations($property);
  87. }
  88. /**
  89. * @group regression
  90. */
  91. public function testMultipleAnnotationsOnSameLine()
  92. {
  93. $reader = $this->createAnnotationReader();
  94. $class = new ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClass2');
  95. $annotations = $reader->getPropertyAnnotations($class->getProperty('id'));
  96. $this->assertEquals(3, count($annotations));
  97. }
  98. public function testCustomAnnotationCreationFunction()
  99. {
  100. $reader = $this->createAnnotationReader();
  101. $reader->setAnnotationCreationFunction(function($name, $values) {
  102. if ($name == 'Doctrine\Tests\Common\Annotations\DummyAnnotation') {
  103. $a = new CustomDummyAnnotationClass;
  104. $a->setDummyValue($values['dummyValue']);
  105. return $a;
  106. }
  107. });
  108. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClass');
  109. $classAnnots = $reader->getClassAnnotations($class);
  110. $this->assertTrue(isset($classAnnots['Doctrine\Tests\Common\Annotations\CustomDummyAnnotationClass']));
  111. $annot = $classAnnots['Doctrine\Tests\Common\Annotations\CustomDummyAnnotationClass'];
  112. $this->assertEquals('hello', $annot->getDummyValue());
  113. }
  114. public function testNonAnnotationProblem()
  115. {
  116. $reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
  117. $reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
  118. $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClassNonAnnotationProblem');
  119. $annotations = $reader->getPropertyAnnotations($class->getProperty('foo'));
  120. $this->assertArrayHasKey('Doctrine\Tests\Common\Annotations\DummyAnnotation', $annotations);
  121. $this->assertType('Doctrine\Tests\Common\Annotations\DummyAnnotation', $annotations['Doctrine\Tests\Common\Annotations\DummyAnnotation']);
  122. }
  123. /**
  124. * @return AnnotationReader
  125. */
  126. public function createAnnotationReader()
  127. {
  128. $reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
  129. $reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
  130. return $reader;
  131. }
  132. /**
  133. * @group DCOM-25
  134. */
  135. public function testSetAutoloadAnnotations()
  136. {
  137. $reader = $this->createAnnotationReader();
  138. $reader->setAutoloadAnnotations(true);
  139. $this->assertTrue($reader->getAutoloadAnnotations());
  140. }
  141. }
  142. class CustomDummyAnnotationClass {
  143. private $dummyValue;
  144. public function setDummyValue($value) {
  145. $this->dummyValue = $value;
  146. }
  147. public function getDummyValue() {
  148. return $this->dummyValue;
  149. }
  150. }
  151. /**
  152. * A description of this class.
  153. *
  154. * @author robo
  155. * @since 2.0
  156. * @DummyAnnotation(dummyValue="hello")
  157. */
  158. class DummyClass {
  159. /**
  160. * A nice property.
  161. *
  162. * @var mixed
  163. * @DummyAnnotation(dummyValue="fieldHello")
  164. */
  165. private $field1;
  166. /**
  167. * @DummyJoinTable(name="join_table",
  168. * joinColumns={
  169. * @DummyJoinColumn(name="col1", referencedColumnName="col2")
  170. * },
  171. * inverseJoinColumns={
  172. * @DummyJoinColumn(name="col3", referencedColumnName="col4")
  173. * })
  174. */
  175. private $field2;
  176. /**
  177. * Gets the value of field1.
  178. *
  179. * @return mixed
  180. * @DummyAnnotation({1,2,"three"})
  181. */
  182. public function getField1() {
  183. }
  184. }
  185. class DummyClass2 {
  186. /**
  187. * @DummyId @DummyColumn(type="integer") @DummyGeneratedValue
  188. * @var integer
  189. */
  190. private $id;
  191. }
  192. class DummyId extends \Doctrine\Common\Annotations\Annotation {}
  193. class DummyColumn extends \Doctrine\Common\Annotations\Annotation {
  194. public $type;
  195. }
  196. class DummyGeneratedValue extends \Doctrine\Common\Annotations\Annotation {}
  197. class DummyAnnotation extends \Doctrine\Common\Annotations\Annotation {
  198. public $dummyValue;
  199. }
  200. class DummyJoinColumn extends \Doctrine\Common\Annotations\Annotation {
  201. public $name;
  202. public $referencedColumnName;
  203. }
  204. class DummyJoinTable extends \Doctrine\Common\Annotations\Annotation {
  205. public $name;
  206. public $joinColumns;
  207. public $inverseJoinColumns;
  208. }
  209. /**
  210. * @DummyAnnotation(@)
  211. */
  212. class DummyClassSyntaxError
  213. {
  214. }
  215. class DummyClassMethodSyntaxError
  216. {
  217. /**
  218. * @DummyAnnotation(@)
  219. */
  220. public function foo()
  221. {
  222. }
  223. }
  224. class DummyClassPropertySyntaxError
  225. {
  226. /**
  227. * @DummyAnnotation(@)
  228. */
  229. public $foo;
  230. }
  231. class DummyClassNonAnnotationProblem
  232. {
  233. /**
  234. * @DummyAnnotation
  235. *
  236. * @var \Test
  237. */
  238. public $foo;
  239. }