From 019fd20468476158908cdc07b96f739c637ed995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jul 2020 19:31:39 +0200 Subject: [PATCH 1/3] Make tests compatible with PHPUnit 9 --- .../Common/Annotations/AbstractReaderTest.php | 120 +++++++++-------- .../Annotations/AnnotationRegistryTest.php | 9 +- .../Tests/Common/Annotations/DocLexerTest.php | 2 +- .../Common/Annotations/DocParserTest.php | 124 +++++++----------- .../Annotations/FileCacheReaderTest.php | 8 +- .../SimpleAnnotationReaderTest.php | 7 + .../Common/Annotations/Ticket/DCOM55Test.php | 7 +- 7 files changed, 134 insertions(+), 143 deletions(-) diff --git a/tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php b/tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php index abc61974d..024a8bfa4 100644 --- a/tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php @@ -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); @@ -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); } @@ -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)); } @@ -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')); } @@ -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')); } @@ -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); } diff --git a/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php b/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php index bbb64a53f..90a3ee9b9 100644 --- a/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php @@ -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); @@ -209,3 +209,10 @@ public function testClassExistsFallbackNotUsedWhenRegisterFileUsed() : void self::assertFalse(AnnotationRegistry::loadAnnotationClass(ShouldNeverBeLoaded::class)); } } + +class Autoloader +{ + public function __invoke(): void + { + } +} diff --git a/tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php b/tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php index c95d32445..2ebbc6f92 100644 --- a/tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php @@ -163,7 +163,7 @@ public function testWithinDoubleQuotesVeryVeryLongStringWillNotOverflowPregSplit $lexer->setInput('"' . str_repeat('.', 20240) . '"'); - self::assertInternalType('array', $lexer->glimpse()); + self::assertIsArray($lexer->glimpse()); } /** diff --git a/tests/Doctrine/Tests/Common/Annotations/DocParserTest.php b/tests/Doctrine/Tests/Common/Annotations/DocParserTest.php index 777f64ab8..1cb4a914d 100644 --- a/tests/Doctrine/Tests/Common/Annotations/DocParserTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/DocParserTest.php @@ -11,6 +11,7 @@ use Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants; use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants; use Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; class DocParserTest extends TestCase @@ -28,7 +29,7 @@ public function testNestedArraysWithNestedAnnotation() self::assertCount(3, $annot->foo); self::assertEquals(1, $annot->foo[0]); self::assertEquals(2, $annot->foo[1]); - self::assertInternalType('array', $annot->foo[2]); + self::assertIsArray($annot->foo[2]); $nestedArray = $annot->foo[2]; self::assertTrue(isset($nestedArray['key'])); @@ -50,13 +51,13 @@ public function testBasicAnnotations() $result = $parser->parse('@Name(foo={"key1" = "value1"})'); $annot = $result[0]; self::assertNull($annot->value); - self::assertInternalType('array', $annot->foo); + self::assertIsArray($annot->foo); self::assertTrue(isset($annot->foo['key1'])); // Numerical arrays $result = $parser->parse('@Name({2="foo", 4="bar"})'); $annot = $result[0]; - self::assertInternalType('array', $annot->value); + self::assertIsArray($annot->value); self::assertEquals('foo', $annot->value[2]); self::assertEquals('bar', $annot->value[4]); self::assertFalse(isset($annot->value[0])); @@ -68,7 +69,7 @@ public function testBasicAnnotations() $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); - self::assertInternalType('array', $annot->value); + self::assertIsArray($annot->value); self::assertInstanceOf(Name::class, $annot->value[0]); self::assertInstanceOf(Name::class, $annot->value[1]); @@ -77,9 +78,9 @@ public function testBasicAnnotations() $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); - self::assertInternalType('array', $annot->value); + self::assertIsArray($annot->value); self::assertInstanceOf(Name::class, $annot->value[0]); - self::assertInternalType('array', $annot->value[1]); + self::assertIsArray($annot->value[1]); self::assertEquals('value1', $annot->value[1]['key1']); self::assertEquals('value2', $annot->value[1]['key2']); @@ -110,7 +111,7 @@ public function testDefaultValueAnnotations() $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); - self::assertInternalType('array', $annot->value); + self::assertIsArray($annot->value); self::assertEquals('value1', $annot->value['key1']); // Array as first value and additional values @@ -118,7 +119,7 @@ public function testDefaultValueAnnotations() $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); - self::assertInternalType('array', $annot->value); + self::assertIsArray($annot->value); self::assertEquals('value1', $annot->value['key1']); self::assertEquals('bar', $annot->foo); } @@ -609,7 +610,7 @@ public function testAnnotationWithAttributesError($attribute,$type,$value,$given $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::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()); + self::assertStringContainsString("[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()); } } @@ -628,7 +629,7 @@ public function testAnnotationWithAttributesWithVarTypeArrayError($attribute,$ty $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::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()); + self::assertStringContainsString("[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()); } } @@ -657,7 +658,7 @@ public function testAnnotationWithRequiredAttributes() $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::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()); + self::assertStringContainsString('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()); } $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; @@ -665,7 +666,7 @@ public function testAnnotationWithRequiredAttributes() $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::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()); + self::assertStringContainsString('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()); } } @@ -691,7 +692,7 @@ public function testAnnotationWithRequiredAttributesWithoutConstructor() $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::assertContains('Attribute "annot" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor declared on property SomeClassName::invalidProperty. expects a(n) \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation. This value should not be null.', $exc->getMessage()); + self::assertStringContainsString('Attribute "annot" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor declared on property SomeClassName::invalidProperty. expects a(n) \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation. This value should not be null.', $exc->getMessage()); } $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; @@ -699,15 +700,11 @@ public function testAnnotationWithRequiredAttributesWithoutConstructor() $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { - self::assertContains('Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor declared on property SomeClassName::invalidProperty. expects a(n) string. This value should not be null.', $exc->getMessage()); + self::assertStringContainsString('Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor declared on property SomeClassName::invalidProperty. expects a(n) string. This value should not be null.', $exc->getMessage()); } } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accept only [ONE, TWO, THREE], but got FOUR. - */ public function testAnnotationEnumeratorException() { $parser = $this->createTestParser(); @@ -716,13 +713,11 @@ public function testAnnotationEnumeratorException() $parser->setIgnoreNotImportedAnnotations(false); $parser->setTarget(Target::TARGET_PROPERTY); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accept only [ONE, TWO, THREE], but got FOUR.'); $parser->parse($docblock, $context); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @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. - */ public function testAnnotationEnumeratorLiteralException() { $parser = $this->createTestParser(); @@ -731,32 +726,30 @@ public function testAnnotationEnumeratorLiteralException() $parser->setIgnoreNotImportedAnnotations(false); $parser->setTarget(Target::TARGET_PROPERTY); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('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.'); $parser->parse($docblock, $context); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage @Enum supports only scalar values "array" given. - */ public function testAnnotationEnumInvalidTypeDeclarationException() { $parser = $this->createTestParser(); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumInvalid("foo")'; $parser->setIgnoreNotImportedAnnotations(false); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('@Enum supports only scalar values "array" given.'); $parser->parse($docblock); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE". - */ public function testAnnotationEnumInvalidLiteralDeclarationException() { $parser = $this->createTestParser(); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteralInvalid("foo")'; $parser->setIgnoreNotImportedAnnotations(false); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".'); $parser->parse($docblock); } @@ -872,10 +865,6 @@ public function testSupportClassConstants($docblock, $expected) self::assertEquals($expected, $annotation->value); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}. - */ public function testWithoutConstructorWhenIsNotDefaultValue() { $parser = $this->createTestParser(); @@ -887,13 +876,11 @@ public function testWithoutConstructorWhenIsNotDefaultValue() $parser->setTarget(Target::TARGET_CLASS); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}.'); $parser->parse($docblock); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}. - */ public function testWithoutConstructorWhenHasNoProperties() { $parser = $this->createTestParser(); @@ -904,13 +891,11 @@ public function testWithoutConstructorWhenHasNoProperties() DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on does not accept any values, but got {"value":"Foo"}.'); $parser->parse($docblock); } - /** - * @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 testAnnotationTargetSyntaxError() { $parser = $this->createTestParser(); @@ -922,13 +907,11 @@ public function testAnnotationTargetSyntaxError() DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage("Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError."); $parser->parse($docblock, $context); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, ANNOTATION] - */ public function testAnnotationWithInvalidTargetDeclarationError() { $parser = $this->createTestParser(); @@ -940,13 +923,11 @@ public function testAnnotationWithInvalidTargetDeclarationError() DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, ANNOTATION]'); $parser->parse($docblock, $context); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage @Target expects either a string value, or an array of strings, "NULL" given. - */ public function testAnnotationWithTargetEmptyError() { $parser = $this->createTestParser(); @@ -958,6 +939,8 @@ public function testAnnotationWithTargetEmptyError() DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('@Target expects either a string value, or an array of strings, "NULL" given.'); $parser->parse($docblock, $context); } @@ -1070,13 +1053,11 @@ public function testNotAnAnnotationClassIsIgnoredWithoutWarningWithoutCheating() self::assertEmpty($result); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage Expected PlainValue, got ''' at position 10. - */ public function testAnnotationDontAcceptSingleQuotes() { $parser = $this->createTestParser(); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage("Expected PlainValue, got ''' at position 10."); $parser->parse("@Name(foo='bar')"); } @@ -1093,11 +1074,11 @@ public function testAnnotationDoesntThrowExceptionWhenAtSignIsNotFollowedByIdent /** * @group DCOM-41 - * @expectedException \Doctrine\Common\Annotations\AnnotationException */ public function testAnnotationThrowsExceptionWhenAtSignIsNotFollowedByIdentifierInNestedAnnotation() { $parser = new DocParser(); + $this->expectException(AnnotationException::class); $parser->parse("@Doctrine\Tests\Common\Annotations\Name(@')"); } @@ -1135,12 +1116,12 @@ public function createTestParser() /** * @group DDC-78 - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage Expected PlainValue, got ''' at position 10 in class \Doctrine\Tests\Common\Annotations\Name */ public function testSyntaxErrorWithContextDescription() { $parser = $this->createTestParser(); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage("Expected PlainValue, got ''' at position 10 in class \Doctrine\Tests\Common\Annotations\Name"); $parser->parse("@Name(foo='bar')", "class \Doctrine\Tests\Common\Annotations\Name"); } @@ -1199,7 +1180,7 @@ public function testCastInt() $result = $parser->parse('@Name(foo=1234)'); $annot = $result[0]; - self::assertInternalType('int', $annot->foo); + self::assertIsInt($annot->foo); } /** @@ -1211,7 +1192,7 @@ public function testCastNegativeInt() $result = $parser->parse('@Name(foo=-1234)'); $annot = $result[0]; - self::assertInternalType('int', $annot->foo); + self::assertIsInt($annot->foo); } /** @@ -1223,7 +1204,7 @@ public function testCastFloat() $result = $parser->parse('@Name(foo=1234.345)'); $annot = $result[0]; - self::assertInternalType('float', $annot->foo); + self::assertIsFloat($annot->foo); } /** @@ -1235,17 +1216,13 @@ public function testCastNegativeFloat() $result = $parser->parse('@Name(foo=-1234.345)'); $annot = $result[0]; - self::assertInternalType('float', $annot->foo); + self::assertIsFloat($annot->foo); $result = $parser->parse('@Marker(-1234.345)'); $annot = $result[0]; - self::assertInternalType('float', $annot->value); + self::assertIsFloat($annot->value); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage [Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared on some class does not have a property named "invalidaProperty". Available properties: data, name - */ public function testSetValuesExeption() { $docblock = <<expectException(AnnotationException::class); + $this->expectExceptionMessage('[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared on some class does not have a property named "invalidaProperty". Available properties: data, name'); $this->createTestParser()->parse($docblock, 'some class'); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @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. - */ public function testInvalidIdentifierInAnnotation() { $parser = $this->createTestParser(); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage("[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."); $parser->parse('@Foo\3.42'); } @@ -1311,13 +1288,11 @@ public function testArrayWithColon() self::assertEquals(['foo' => 'bar'], $annots[0]->value); } - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage [Semantical Error] Couldn't find constant foo. - */ public function testInvalidContantName() { $parser = $this->createTestParser(); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage("[Semantical Error] Couldn't find constant foo."); $parser->parse('@Name(foo: "bar")'); } @@ -1462,6 +1437,7 @@ class Marker { use Doctrine\Common\Annotations\Annotation; + /** @Annotation */ class Name extends Annotation { } diff --git a/tests/Doctrine/Tests/Common/Annotations/FileCacheReaderTest.php b/tests/Doctrine/Tests/Common/Annotations/FileCacheReaderTest.php index 76c0c1d37..027bd6a8b 100644 --- a/tests/Doctrine/Tests/Common/Annotations/FileCacheReaderTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/FileCacheReaderTest.php @@ -17,7 +17,7 @@ protected function getReader(): Reader return new FileCacheReader(new AnnotationReader(), $this->cacheDir); } - public function tearDown() + public function tearDown(): void { foreach (glob($this->cacheDir.'/*.php') AS $file) { unlink($file); @@ -32,7 +32,11 @@ public function testAttemptToCreateAnnotationCacheDir() { $this->cacheDir = sys_get_temp_dir() . '/not_existed_dir_' . uniqid('', true); - self::assertDirectoryNotExists($this->cacheDir); + if (method_exists($this, 'assertDirectoryDoesNotExist')) { + self::assertDirectoryDoesNotExist($this->cacheDir); + } else { + self::assertDirectoryNotExists($this->cacheDir); + } new FileCacheReader(new AnnotationReader(), $this->cacheDir); diff --git a/tests/Doctrine/Tests/Common/Annotations/SimpleAnnotationReaderTest.php b/tests/Doctrine/Tests/Common/Annotations/SimpleAnnotationReaderTest.php index ebad718cc..a2d1431d8 100644 --- a/tests/Doctrine/Tests/Common/Annotations/SimpleAnnotationReaderTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/SimpleAnnotationReaderTest.php @@ -15,6 +15,7 @@ class SimpleAnnotationReaderTest extends AbstractReaderTest */ public function testImportDetectsNotImportedAnnotation() { + $this->ignoreIssues(); parent::testImportDetectsNotImportedAnnotation(); } @@ -26,6 +27,7 @@ public function testImportDetectsNotImportedAnnotation() */ public function testImportDetectsNonExistentAnnotation() { + $this->ignoreIssues(); parent::testImportDetectsNonExistentAnnotation(); } @@ -37,6 +39,7 @@ public function testImportDetectsNonExistentAnnotation() */ public function testClassWithInvalidAnnotationTargetAtClassDocBlock() { + $this->ignoreIssues(); parent::testClassWithInvalidAnnotationTargetAtClassDocBlock(); } @@ -48,6 +51,7 @@ public function testClassWithInvalidAnnotationTargetAtClassDocBlock() */ public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock() { + $this->ignoreIssues(); parent::testClassWithInvalidAnnotationTargetAtPropertyDocBlock(); } @@ -59,6 +63,7 @@ public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock() */ public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock() { + $this->ignoreIssues(); parent::testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock(); } @@ -70,6 +75,7 @@ public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock() */ public function testClassWithInvalidAnnotationTargetAtMethodDocBlock() { + $this->ignoreIssues(); parent::testClassWithInvalidAnnotationTargetAtMethodDocBlock(); } @@ -81,6 +87,7 @@ public function testClassWithInvalidAnnotationTargetAtMethodDocBlock() */ public function testErrorWhenInvalidAnnotationIsUsed() { + $this->ignoreIssues(); parent::testErrorWhenInvalidAnnotationIsUsed(); } diff --git a/tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM55Test.php b/tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM55Test.php index 1036f9805..bbee8591b 100644 --- a/tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM55Test.php +++ b/tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM55Test.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\Common\Annotations\Ticket; +use Doctrine\Common\Annotations\AnnotationException; use Doctrine\Tests\Common\Annotations\Fixtures\Controller; use Doctrine\Common\Annotations\AnnotationReader; use PHPUnit\Framework\TestCase; @@ -11,14 +12,12 @@ */ class DCOM55Test extends TestCase { - /** - * @expectedException \Doctrine\Common\Annotations\AnnotationException - * @expectedExceptionMessage [Semantical Error] The class "Doctrine\Tests\Common\Annotations\Fixtures\Controller" 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\Controller". If it is indeed no annotation, then you need to add @IgnoreAnnotation("Controller") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Ticket\Dummy. - */ public function testIssue() { $class = new \ReflectionClass(__NAMESPACE__ . '\\Dummy'); $reader = new AnnotationReader(); + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('[Semantical Error] The class "Doctrine\Tests\Common\Annotations\Fixtures\Controller" 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\Controller". If it is indeed no annotation, then you need to add @IgnoreAnnotation("Controller") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Ticket\Dummy.'); $reader->getClassAnnotations($class); } From 2615685700bf8388c33f25cb93eab0144d7bd49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jul 2020 21:29:30 +0200 Subject: [PATCH 2/3] Allow PHPUnit above 9.1.5 This should help getting the PHP 8 build closer to a successful one. Fixes #340 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0f9b09386..10aae8725 100644 --- a/composer.json +++ b/composer.json @@ -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 From 47e7d05010cf11e4a99e520ac94e518cd330d1b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jul 2020 22:28:30 +0200 Subject: [PATCH 3/3] Run PHPStan on 7.4 That is a version where we can installed newer versions of PHPUnit, and use methods that may not exist otherwise. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4d1886b8f..b7d9829fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: