Skip to content

Commit

Permalink
Add pkName::summarize to AuditTrait (#118)
Browse files Browse the repository at this point in the history
* Add pkName to AuditTrait

* Dummy Entity

* add test

* cs

Co-authored-by: a.dmitryuk <[email protected]>
  • Loading branch information
dmitryuk and a.dmitryuk authored Oct 29, 2022
1 parent e2703a9 commit 4072a4e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Provider/Doctrine/Auditing/Transaction/AuditTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ private function summarize(EntityManagerInterface $entityManager, ?object $entit
$label = DoctrineHelper::getRealClassName($entity).(null === $pkValue ? '' : '#'.$pkValue);
}

if ('id' !== $pkName) {
$extra['pkName'] = $pkName;
}

return [
$pkName => $pkValue,
'class' => $meta->name,
Expand Down
36 changes: 36 additions & 0 deletions tests/Provider/Doctrine/Fixtures/Issue112/DummyEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue112;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="author")
*/
#[ORM\Entity]
#[ORM\Table(name: 'author')]
class DummyEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned": true})
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
protected int $primaryKey;

public function getPrimaryKey(): int
{
return $this->primaryKey;
}

public function setPrimaryKey(int $primaryKey): void
{
$this->primaryKey = $primaryKey;
}
}
35 changes: 35 additions & 0 deletions tests/Provider/Doctrine/Issues/Issue112Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DH\Auditor\Tests\Provider\Doctrine\Issues;

use DH\Auditor\Provider\Doctrine\Auditing\Transaction\AuditTrait;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue112\DummyEntity;
use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\DefaultSchemaSetupTrait;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @small
*/
final class Issue112Test extends TestCase
{
use AuditTrait;
use DefaultSchemaSetupTrait;

/**
* @throws \Doctrine\ORM\Mapping\MappingException
* @throws \Doctrine\DBAL\Exception
* @throws \DH\Auditor\Exception\MappingException
*/
public function testSummarizeWithUnusualPK(): void
{
$entityManager = $this->createEntityManager();
$entity = new DummyEntity();
$entity->setPrimaryKey(2);
$data = $this->summarize($entityManager, $entity);
self::assertSame('primaryKey', $data['pkName']);
}
}

0 comments on commit 4072a4e

Please sign in to comment.