Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Doctrine annotations #129

Merged
merged 2 commits into from
Nov 29, 2022
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: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"doctrine/annotations": "^1.8",
"doctrine/orm": "^2.7",
"symfony/cache": "^4.0|^5.0|^6.0",
"symfony/event-dispatcher": "^4.0|^5.0|^6.0",
Expand Down
10 changes: 8 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -25,7 +26,12 @@
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::TYPE_DECLARATION_STRICT,
// SetList::TYPE_DECLARATION,
// SetList::TYPE_DECLARATION_STRICT,
]);

// Doctrine rules
$rectorConfig->sets([
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
};
26 changes: 5 additions & 21 deletions src/Provider/Doctrine/Auditing/Annotation/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@

namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\Persistence\Mapping\ClassMetadata;
use ReflectionClass;

class AnnotationLoader
{
private ?AnnotationReader $reader = null;

private EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager, bool $useAttributesOnly = false)
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->reader = class_exists(AnnotationReader::class) && !$useAttributesOnly ? new AnnotationReader() : null;
}

public function load(): array
Expand All @@ -45,38 +41,29 @@ private function getEntityConfiguration(ClassMetadata $metadata): ?array
$reflection = $metadata->getReflectionClass();

// Check that we have an Entity annotation or attribute
// TODO: only rely on PHP attributes for next major release
$attributes = \PHP_VERSION_ID >= 80000 && method_exists($reflection, 'getAttributes') ? $reflection->getAttributes(Entity::class) : null;
$attributes = $reflection->getAttributes(Entity::class);
if (\is_array($attributes) && [] !== $attributes) {
$annotation = $attributes[0]->newInstance();
} elseif (null !== $this->reader) {
$annotation = $this->reader->getClassAnnotation($reflection, Entity::class);
}

if (null === $annotation) {
return null;
}

// Check that we have an Auditable annotation or attribute
// TODO: only rely on PHP attributes for next major release
$attributes = \PHP_VERSION_ID >= 80000 && method_exists($reflection, 'getAttributes') ? $reflection->getAttributes(Auditable::class) : null;
$attributes = $reflection->getAttributes(Auditable::class);
if (\is_array($attributes) && [] !== $attributes) {
$auditableAnnotation = $attributes[0]->newInstance();
} elseif (null !== $this->reader) {
$auditableAnnotation = $this->reader->getClassAnnotation($reflection, Auditable::class);
}

if (null === $auditableAnnotation) {
return null;
}

// Check that we have a Security annotation or attribute
// TODO: only rely on PHP attributes for next major release
$attributes = \PHP_VERSION_ID >= 80000 && method_exists($reflection, 'getAttributes') ? $reflection->getAttributes(Security::class) : null;
$attributes = $reflection->getAttributes(Security::class);
if (\is_array($attributes) && [] !== $attributes) {
$securityAnnotation = $attributes[0]->newInstance();
} elseif (null !== $this->reader) {
$securityAnnotation = $this->reader->getClassAnnotation($reflection, Security::class);
}

$roles = null === $securityAnnotation ? null : [Security::VIEW_SCOPE => $securityAnnotation->view];
Expand All @@ -97,12 +84,9 @@ private function getAllProperties(ReflectionClass $reflection): array
$properties = [];

foreach ($reflection->getProperties() as $property) {
// TODO: only rely on PHP attributes for next major release
$attributes = \PHP_VERSION_ID >= 80000 && method_exists($property, 'getAttributes') ? $property->getAttributes(Ignore::class) : null;
$attributes = $property->getAttributes(Ignore::class);
if (\is_array($attributes) && [] !== $attributes) {
$annotationProperty = $attributes[0]->newInstance();
} elseif (null !== $this->reader) {
$annotationProperty = $this->reader->getPropertyAnnotation($property, Ignore::class);
}

if (null !== $annotationProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,84 +25,26 @@ public function testLoadEntitiesWithoutAnnotationAndAttribute(): void
__DIR__.'/../../Traits',
],
'default',
null,
false
null
);

$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
self::assertCount(0, $loaded, 'No annotation loaded using annotation driver');

if (\PHP_VERSION_ID >= 80000) {
$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/../../Traits',
],
'default',
null,
true
);

$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
self::assertCount(0, $loaded, 'No annotation loaded using attribute driver');
}
}

public function testLoadEntitiesWithAnnotationsOnly(): void
{
$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/../../Fixtures/Entity/Annotation',
],
'default',
null,
false
);
$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
self::assertCount(2, $loaded);
self::assertCount(0, $loaded, 'No annotation loaded using attribute driver');
}

public function testLoadEntitiesWithAttributesOnly(): void
{
if (\PHP_VERSION_ID < 80000) {
self::markTestSkipped('PHP > 8.0 is required.');
}

$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/../../Fixtures/Entity/Attribute',
],
'default',
null,
true
null
);
$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
self::assertCount(2, $loaded);
}

public function testLoadEntitiesWithAnnotationsOnlyButNoAnnotationReader(): void
{
if (\PHP_VERSION_ID < 80000) {
self::markTestSkipped('PHP > 8.0 is required.');
}

$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/../../Fixtures/Entity/Annotation',
],
'default',
null,
false
);
$annotationLoader = new AnnotationLoader($entityManager, true);
$loaded = $annotationLoader->load();
self::assertCount(0, $loaded);
}
}
37 changes: 1 addition & 36 deletions tests/Provider/Doctrine/DoctrineProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use DH\Auditor\Security\SecurityProviderInterface;
use DH\Auditor\Tests\Fixtures\Provider\AuditNoStorageProvider;
use DH\Auditor\Tests\Fixtures\Provider\StorageNoAuditProvider;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Annotation\AuditedEntity as AuditedEntityWithAnnotation;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Attribute\AuditedEntity as AuditedEntityWithAttribute;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Comment;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Post;
Expand Down Expand Up @@ -413,49 +412,15 @@ public function testIsAuditedFieldReturnsFalseIfEntityIsNotAudited(): void
self::assertFalse($provider->isAuditedField(Comment::class, 'id'), 'Field "'.Comment::class.'::$id" is audited but "'.Comment::class.'" entity is not.');
}

public function testLoadEntitiesWithAnnotationsOnly(): void
{
$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/Fixtures/Entity/Annotation',
],
'default',
null,
false
);
$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
self::assertCount(2, $loaded);

$auditor = $this->createAuditor();
$provider = new DoctrineProvider($this->createProviderConfiguration(['entities' => $loaded]));
$provider->registerStorageService(new StorageService('default', $entityManager));
$provider->registerAuditingService(new AuditingService('default', $entityManager));

$auditor->registerProvider($provider);

self::assertTrue($provider->isAudited(AuditedEntityWithAnnotation::class), '"'.AuditedEntityWithAnnotation::class.'" is audited.');
self::assertTrue($provider->isAuditedField(AuditedEntityWithAnnotation::class, 'auditedField'), 'Field "'.AuditedEntityWithAnnotation::class.'::$auditedField" is audited.');
self::assertFalse($provider->isAuditedField(AuditedEntityWithAnnotation::class, 'ignoredField'), 'Field "'.AuditedEntityWithAnnotation::class.'::$ignoredField" is ignored.');
self::assertFalse($provider->isAuditedField(AuditedEntityWithAnnotation::class, 'ignoredProtectedField'), 'Field "'.AuditedEntityWithAnnotation::class.'::$ignoredProtectedField" is ignored.');
self::assertFalse($provider->isAuditedField(AuditedEntityWithAnnotation::class, 'ignoredPrivateField'), 'Field "'.AuditedEntityWithAnnotation::class.'::$ignoredPrivateField" is ignored.');
}

public function testLoadEntitiesWithAttributesOnly(): void
{
if (\PHP_VERSION_ID < 80000) {
self::markTestSkipped('PHP > 8.0 is required.');
}

$entityManager = $this->createEntityManager(
[
__DIR__.'/../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/Fixtures/Entity/Attribute',
],
'default',
null,
true
null
);
$annotationLoader = new AnnotationLoader($entityManager);
$loaded = $annotationLoader->load();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Attribute;

use DH\Auditor\Provider\Doctrine\Auditing\Annotation as Audit;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
Expand All @@ -20,6 +21,6 @@ class AuditableButUnauditedEntity

#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private int $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Attribute;

use DH\Auditor\Provider\Doctrine\Auditing\Annotation as Audit;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
Expand All @@ -24,6 +25,6 @@ class AuditedEntity
private string $ignoredPrivateField;

#[ORM\Id, ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column(type: 'integer')]
#[ORM\Column(type: Types::INTEGER)]
private int $id;
}
Loading