Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
before_script:
- travis_retry composer require --dev --prefer-dist --prefer-stable phpstan/phpstan:^0.12.20
script: vendor/bin/phpstan analyse -l 3 -c phpstan.neon lib tests
php: 7.4

- stage: Benchmark
install:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"doctrine/cache": "1.*",
"phpstan/phpstan": "^0.12.20",
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7.5 || ^9.1.5"
},
"config": {
"sort-packages": true
Expand Down
120 changes: 59 additions & 61 deletions tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

abstract class AbstractReaderTest extends TestCase
{
/**
* @var bool
*/
private $expectException = true;

final protected function ignoreIssues(): void
{
$this->expectException = false;
}

public function getReflectionClass()
{
return new ReflectionClass(DummyClass::class);
Expand Down Expand Up @@ -84,7 +94,7 @@ public function testAnnotationsWithVarType()
self::assertCount(1, $fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo')));
self::assertCount(1, $barAnnot = $reader->getMethodAnnotations($class->getMethod('bar')));

self::assertInternalType('string', $fooAnnot[0]->string);
self::assertIsString($fooAnnot[0]->string);
self::assertInstanceOf(Fixtures\AnnotationTargetAll::class, $barAnnot[0]->annotation);
}

Expand All @@ -108,13 +118,13 @@ public function testClassWithWithDanglingComma()
self::assertCount(1, $annots);
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetPropertyMethod is not allowed to be declared on class Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass. You may only use this annotation on these code elements: METHOD, PROPERTY
*/
public function testClassWithInvalidAnnotationTargetAtClassDocBlock()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Semantical Error] Annotation @AnnotationTargetPropertyMethod is not allowed to be declared on class Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass. You may only use this annotation on these code elements: METHOD, PROPERTY');
}
$reader->getClassAnnotations(new \ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtClass::class));
}

Expand All @@ -125,117 +135,104 @@ public function testClassWithWithInclude()
self::assertCount(1, $annots);
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$foo. You may only use this annotation on these code elements: CLASS
*/
public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$foo. You may only use this annotation on these code elements: CLASS');
}
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'foo'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetAnnotation is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$bar. You may only use this annotation on these code elements: ANNOTATION
*/
public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Semantical Error] Annotation @AnnotationTargetAnnotation is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$bar. You may only use this annotation on these code elements: ANNOTATION');
}
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'bar'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod::functionName(). You may only use this annotation on these code elements: CLASS
*/
public function testClassWithInvalidAnnotationTargetAtMethodDocBlock()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod::functionName(). You may only use this annotation on these code elements: CLASS');
}
$reader->getMethodAnnotations(new \ReflectionMethod(Fixtures\ClassWithInvalidAnnotationTargetAtMethod::class, 'functionName'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
*/
public function testClassWithAnnotationWithTargetSyntaxErrorAtClassDocBlock()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.");
$reader->getClassAnnotations(new \ReflectionClass(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
*/
public function testClassWithAnnotationWithTargetSyntaxErrorAtPropertyDocBlock()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.");
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class,'foo'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
*/
public function testClassWithAnnotationWithTargetSyntaxErrorAtMethodDocBlock()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.");
$reader->getMethodAnnotations(new \ReflectionMethod(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class,'bar'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Type Error] Attribute "string" of @AnnotationWithVarType declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::$invalidProperty expects a(n) string, but got integer.
*/
public function testClassWithPropertyInvalidVarTypeError()
{
$reader = $this->getReader();
$class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class);

$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Type Error] Attribute "string" of @AnnotationWithVarType declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::$invalidProperty expects a(n) string, but got integer.');
$reader->getPropertyAnnotations($class->getProperty('invalidProperty'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage [Type Error] Attribute "annotation" of @AnnotationWithVarType declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::invalidMethod() expects a(n) \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll, but got an instance of Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.
*/
public function testClassWithMethodInvalidVarTypeError()
{
$reader = $this->getReader();
$class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class);

$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('[Type Error] Attribute "annotation" of @AnnotationWithVarType declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::invalidMethod() expects a(n) \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll, but got an instance of Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.');
$reader->getMethodAnnotations($class->getMethod('invalidMethod'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in class Doctrine\Tests\Common\Annotations\DummyClassSyntaxError.
*/
public function testClassSyntaxErrorContext()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 18 in class Doctrine\Tests\Common\Annotations\DummyClassSyntaxError.");
$reader->getClassAnnotations(new \ReflectionClass(DummyClassSyntaxError::class));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo().
*/
public function testMethodSyntaxErrorContext()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 18 in method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo().");
$reader->getMethodAnnotations(new \ReflectionMethod(DummyClassMethodSyntaxError::class, 'foo'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::$foo.
*/
public function testPropertySyntaxErrorContext()
{
$reader = $this->getReader();
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage(<<<'EOT'
Expected namespace separator or identifier, got ')' at position 18 in property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::$foo.
EOT
);
$reader->getPropertyAnnotations(new \ReflectionProperty(DummyClassPropertySyntaxError::class, 'foo'));
}

Expand Down Expand Up @@ -290,23 +287,24 @@ public function testImportWithInheritance()
self::assertInstanceOf(Bar\Name::class, reset($parentAnnotations));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage The annotation "@NameFoo" in property Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass::$field was never imported.
*/
public function testImportDetectsNotImportedAnnotation()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The annotation "@NameFoo" in property Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass::$field was never imported.');

}
$reader->getPropertyAnnotations(new \ReflectionProperty(TestAnnotationNotImportedClass::class, 'field'));
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage The annotation "@Foo\Bar\Name" in property Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass::$field was never imported.
*/
public function testImportDetectsNonExistentAnnotation()
{
$reader = $this->getReader();
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The annotation "@Foo\Bar\Name" in property Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass::$field was never imported.');
}
$reader->getPropertyAnnotations(new \ReflectionProperty(TestNonExistentAnnotationClass::class, 'field'));
}

Expand Down Expand Up @@ -374,14 +372,14 @@ public function testResetsPhpParserAfterUse()
self::assertEmpty($annotations);
}

/**
* @expectedException \Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage The class "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation". If it is indeed no annotation, then you need to add @IgnoreAnnotation("NoAnnotation") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass.
*/
public function testErrorWhenInvalidAnnotationIsUsed()
{
$reader = $this->getReader();
$ref = new \ReflectionClass(Fixtures\InvalidAnnotationUsageClass::class);
if ($this->expectException) {
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The class "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation". If it is indeed no annotation, then you need to add @IgnoreAnnotation("NoAnnotation") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass.');
}
$reader->getClassAnnotations($ref);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function testRegisterLoaderIfNotExistsOnlyRegisteresSameLoaderOnce() : vo
{
$className = 'autoloadedClassThatDoesNotExist';
AnnotationRegistry::reset();
$autoLoader = self::createPartialMock(\stdClass::class, ['__invoke']);
$autoLoader = self::createPartialMock(Autoloader::class, ['__invoke']);
$autoLoader->expects($this->once())->method('__invoke');
AnnotationRegistry::registerUniqueLoader($autoLoader);
AnnotationRegistry::registerUniqueLoader($autoLoader);
Expand Down Expand Up @@ -209,3 +209,10 @@ public function testClassExistsFallbackNotUsedWhenRegisterFileUsed() : void
self::assertFalse(AnnotationRegistry::loadAnnotationClass(ShouldNeverBeLoaded::class));
}
}

class Autoloader
{
public function __invoke(): void
{
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function testWithinDoubleQuotesVeryVeryLongStringWillNotOverflowPregSplit

$lexer->setInput('"' . str_repeat('.', 20240) . '"');

self::assertInternalType('array', $lexer->glimpse());
self::assertIsArray($lexer->glimpse());
}

/**
Expand Down
Loading