Repository: DamienHarper/auditor (php)

Issue:
BackedEnum could not be converted to int

| Q                    | A
| ---------------------| -----------------------
| `auditor` version    | 2.0.5
| PHP version          | 8.1.2
| Database             | MySQL

#### Summary

ErrorException

Warning: Object of class App\Enum\ProductType could not be converted to int

#### Current behavior

`vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\AuditTrait.php&line=89#line89)(line 89)`

https://github.com/DamienHarper/auditor/blob/master/src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php#L88

#### Current code
````
case DoctrineHelper::getDoctrineType('INTEGER'):
case DoctrineHelper::getDoctrineType('SMALLINT'):
$convertedValue = (int) $value; // @phpstan-ignore-line
break;
````
Bug fixed
````
case DoctrineHelper::getDoctrineType('INTEGER'):
case DoctrineHelper::getDoctrineType('SMALLINT'):
$convertedValue = (int) $value instanceof \BackedEnum ? $value->value : $value ; // @phpstan-ignore-line
break;
````


Pull request:
BackedEnum could not be converted to int
Closes #115 

Patch:
diff --git a/src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php b/src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php
index 2af674c..7706197 100644
--- a/src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php
+++ b/src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php
@@ -11,6 +11,7 @@ use Doctrine\DBAL\Types\Type;
 use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
 use Throwable;
+use UnitEnum;
 
 trait AuditTrait
 {
@@ -74,6 +75,9 @@ trait AuditTrait
         if (null === $value) {
             return null;
         }
+        if (interface_exists(UnitEnum::class) && $value instanceof UnitEnum && property_exists($value, 'value')) {
+            $value = $value->value;
+        }
 
         $platform = $entityManager->getConnection()->getDatabasePlatform();
 


Test patch:
diff --git a/tests/Provider/Doctrine/Fixtures/Issue115/DummyEntity.php b/tests/Provider/Doctrine/Fixtures/Issue115/DummyEntity.php
new file mode 100644
index 0000000..5dc47ae
--- /dev/null
+++ b/tests/Provider/Doctrine/Fixtures/Issue115/DummyEntity.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue115;
+
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ * @ORM\Table(name="dummy_entity")
+ */
+#[ORM\Entity, ORM\Table(name: 'dummy_entity')]
+class DummyEntity
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="NONE")
+     * @ORM\Column(type="bigint")
+     */
+    #[ORM\Id, ORM\GeneratedValue(strategy: 'NONE'), ORM\Column(type: 'bigint')]
+    private DummyEnum $id;
+
+    public function __construct()
+    {
+        $this->id = DummyEnum::A;
+    }
+
+    public function getId(): ?DummyEnum
+    {
+        return $this->id;
+    }
+
+    public function setId(?DummyEnum $id): void
+    {
+        $this->id = $id;
+    }
+}
diff --git a/tests/Provider/Doctrine/Fixtures/Issue115/DummyEnum.php b/tests/Provider/Doctrine/Fixtures/Issue115/DummyEnum.php
new file mode 100644
index 0000000..35a500c
--- /dev/null
+++ b/tests/Provider/Doctrine/Fixtures/Issue115/DummyEnum.php
@@ -0,0 +1,12 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue115;
+
+enum DummyEnum: string
+{
+    case A = 'a';
+
+    case B = 'b';
+}
diff --git a/tests/Provider/Doctrine/Issues/Issue115Test.php b/tests/Provider/Doctrine/Issues/Issue115Test.php
new file mode 100644
index 0000000..6118371
--- /dev/null
+++ b/tests/Provider/Doctrine/Issues/Issue115Test.php
@@ -0,0 +1,77 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DH\Auditor\Tests\Provider\Doctrine\Issues;
+
+use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor;
+use DH\Auditor\Provider\Doctrine\DoctrineProvider;
+use DH\Auditor\Provider\Doctrine\Model\Transaction;
+use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
+use DH\Auditor\Provider\Doctrine\Service\AuditingService;
+use DH\Auditor\Provider\Doctrine\Service\StorageService;
+use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue115\DummyEntity;
+use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue115\DummyEnum;
+use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\DefaultSchemaSetupTrait;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @internal
+ *
+ * @small
+ *
+ * @requires PHP >= 8.1
+ */
+final class Issue115Test extends TestCase
+{
+    use DefaultSchemaSetupTrait;
+
+    public function testIssue115(): void
+    {
+        $reader = new Reader($this->provider);
+
+        /** @var StorageService $storageService */
+        $storageService = $this->provider->getStorageServiceForEntity(DummyEntity::class);
+        $entityManager = $storageService->getEntityManager();
+        $processor = new TransactionProcessor($this->provider);
+        $transaction = new Transaction($entityManager);
+        $entity = new DummyEntity();
+        $entity->setId(DummyEnum::A);
+        $transaction->insert($entity, [
+            'id' => [DummyEnum::A, DummyEnum::B],
+        ]);
+
+        $processor->process($transaction);
+
+        $audits = $reader->createQuery(DummyEntity::class)->execute();
+        self::assertCount(1, $audits, 'results count ok.');
+
+        /** @var \DH\Auditor\Model\Entry $audit */
+        $audit = $audits[0];
+        $diffs = $audit->getDiffs()['id'];
+        self::assertSame('a', $diffs['old']);
+        self::assertSame('b', $diffs['new']);
+    }
+
+    private function configureEntities(): void
+    {
+        $this->provider->getConfiguration()->setEntities([
+            DummyEntity::class => ['enabled' => true],
+        ]);
+    }
+
+    private function createAndInitDoctrineProvider(): void
+    {
+        $auditor = $this->createAuditor();
+        $this->provider = new DoctrineProvider($this->createProviderConfiguration());
+
+        $entityManager = $this->createEntityManager([
+            __DIR__.'/../../../../src/Provider/Doctrine/Auditing/Annotation',
+            __DIR__.'/../Fixtures/Issue115',
+        ]);
+        $this->provider->registerStorageService(new StorageService('default', $entityManager));
+        $this->provider->registerAuditingService(new AuditingService('default', $entityManager));
+
+        $auditor->registerProvider($this->provider);
+    }
+}