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

Fix audit revision query for enum column mapping #534

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"doctrine/common": "^3.1",
"doctrine/dbal": "^2.13 || ^3.0",
"doctrine/event-manager": "^1.1",
"doctrine/orm": "^2.10",
"doctrine/orm": "^2.11",
phansys marked this conversation as resolved.
Show resolved Hide resolved
"doctrine/persistence": "^2.2 || ^3.0",
"symfony/config": "^4.4 || ^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0",
Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/LogRevisionsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,12 @@ private function saveRevisionEntityData(EntityManagerInterface $em, ClassMetadat
);
}

foreach ($params as $key => $parameterValue) {
if (\interface_exists(\BackedEnum::class) && $parameterValue instanceof \BackedEnum) {
$params[$key] = $parameterValue->value;
}
}

$conn->executeStatement($this->getInsertRevisionSQL($em, $class), $params, $types);
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Fixtures/Issue/IssueEntityWithEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\EntityAuditBundle\Tests\Fixtures\Issue;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class IssueEntityWithEnum
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private ?int $id = null;

/**
* @ORM\Column(enumType="Status::class")
*/
private Status $status;

public function __construct(Status $status)
{
$this->status = $status;
}

public function getId(): ?int
{
return $this->id;
}

public function getStatus(): Status
{
return $this->status;
}

public function setStatus(Status $status): self
{
$this->status = $status;

return $this;
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Issue/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Sonata\EntityAuditBundle\Tests\Fixtures\Issue;

enum Status: string
{
case Foo = 'bar';
case Qwe = 'baz';
}
70 changes: 70 additions & 0 deletions tests/Issue/IssueEntityWithEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\EntityAuditBundle\Tests\Issue;

use Sonata\EntityAuditBundle\Tests\BaseTest;
use Sonata\EntityAuditBundle\Tests\Fixtures\Issue\IssueEntityWithEnum;
use Sonata\EntityAuditBundle\Tests\Fixtures\Issue\Status;

/**
* @requires PHP 8.1
*/
final class IssueEntityWithEnumTest extends BaseTest
{
protected $schemaEntities = [
IssueEntityWithEnum::class,
];

protected $auditedEntities = [
IssueEntityWithEnum::class,
];

public function testIssueEntityWithEnums(): void
{
$entity = new IssueEntityWithEnum(Status::Foo);

$this->em->persist($entity);
$this->em->flush();

\assert(\is_int($entity->getId()));

$this->em->clear();

/** @var IssueEntityWithEnum $entity */
$entity = $this->em->getRepository(IssueEntityWithEnum::class)->findOneBy(['id' => $entity->getId()]);

$entity->setStatus(Status::Qwe);
$this->em->flush();

$reader = $this->auditManager->createAuditReader($this->em);

$auditEntity = $reader->find(IssueEntityWithEnum::class, $entity->getId(), 1);
self::assertInstanceOf(IssueEntityWithEnum::class, $auditEntity);
self::assertSame(Status::Foo, $auditEntity->getStatus());

$auditEntity = $reader->find(IssueEntityWithEnum::class, $entity->getId(), 2);
self::assertInstanceOf(IssueEntityWithEnum::class, $auditEntity);
self::assertSame(Status::Qwe, $auditEntity->getStatus());

$this->em->clear();

/** @var IssueEntityWithEnum $entity */
$entity = $this->em->getRepository(IssueEntityWithEnum::class)->findOneBy(['id' => $entity->getId()]);

$this->em->remove($entity);
$this->em->flush();

self::assertCount(0, $reader->findRevisions(IssueEntityWithEnum::class, $entity->getId()));
}
}