Skip to content

Commit

Permalink
Added test cases for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Oct 26, 2022
1 parent 3f4b83c commit f304128
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php
Expand Up @@ -6,6 +6,8 @@
use Doctrine\Common\Annotations\DocParser;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\SingleUseAnnotation;
use Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithEnumProperty;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithEnumAnnotations;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithFullPathUseStatement;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithImportedIgnoredAnnotation;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithPHPCodeSnifferAnnotation;
Expand All @@ -15,6 +17,7 @@
use Doctrine\Tests\Common\Annotations\Fixtures\IgnoredNamespaces\AnnotatedAtMethodLevel;
use Doctrine\Tests\Common\Annotations\Fixtures\IgnoredNamespaces\AnnotatedAtPropertyLevel;
use Doctrine\Tests\Common\Annotations\Fixtures\IgnoredNamespaces\AnnotatedWithAlias;
use Doctrine\Tests\Common\Annotations\Fixtures\Suit;
use InvalidArgumentException;
use LogicException;
use ReflectionClass;
Expand Down Expand Up @@ -295,4 +298,33 @@ public function testFunctionAnnotation(): void
$annotation = $reader->getFunctionAnnotation($ref, Fixtures\Annotation\Autoload::class);
self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotation);
}

/**
* @requires PHP 8.1
* @dataProvider provideEnumProperties
*/
public function testAnnotationWithEnum(string $property, Suit $expectedValue): void
{
$reader = $this->getReader();
$ref = new ReflectionClass(ClassWithEnumAnnotations::class);

$annotation = $reader->getPropertyAnnotation($ref->getProperty($property), AnnotationWithEnumProperty::class);

self::assertSame($expectedValue, $annotation->suit);
}

/**
* @return list<array{string, Suit}>
*/
public function provideEnumProperties(): array
{
if (PHP_VERSION_ID < 80100) {
return [];
}

return [
['annotationWithDefaults', Suit::Hearts],
['annotationWithSpades', Suit::Spades],
];
}
}
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Annotations\Fixtures;

/**
* @Annotation
* @Target("ALL")
* @NamedArgumentConstructor
*/
final class AnnotationWithEnumProperty
{
public function __construct(
public readonly Suit $suit = Suit::Hearts,
) {
}
}
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Annotations\Fixtures;

class ClassWithEnumAnnotations
{
/** @AnnotationWithEnumProperty */
public $annotationWithDefaults;

/** @AnnotationWithEnumProperty(suit=Suit::Spades) */
public $annotationWithSpades;
}
13 changes: 13 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/Fixtures/Suit.php
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Annotations\Fixtures;

enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

0 comments on commit f304128

Please sign in to comment.