Skip to content

Commit 21fff3f

Browse files
committed
Added Blameable UUID test
1 parent 862ac8d commit 21fff3f

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"rector/rector": "^0.19",
6868
"symfony/console": "^5.4 || ^6.0 || ^7.0",
6969
"symfony/phpunit-bridge": "^6.0 || ^7.0",
70+
"symfony/uid": "^7.0",
7071
"symfony/yaml": "^5.4 || ^6.0 || ^7.0"
7172
},
7273
"conflict": {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Blameable;
13+
14+
use Doctrine\Common\EventManager;
15+
use Gedmo\Blameable\Blameable;
16+
use Gedmo\Blameable\BlameableListener;
17+
use Gedmo\Tests\Blameable\Fixture\Entity\Company;
18+
use Gedmo\Tests\Tool\BaseTestCaseORM;
19+
use Symfony\Component\Uid\Uuid;
20+
use Symfony\Component\Uid\UuidV6;
21+
22+
final class BlameableUuidTest extends BaseTestCaseORM
23+
{
24+
private const COMPANY = Company::class;
25+
26+
private UuidV6 $uuid;
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->uuid = Uuid::v6();
33+
34+
$listener = new BlameableListener();
35+
$listener->setUserValue($this->uuid);
36+
37+
$evm = new EventManager();
38+
$evm->addEventSubscriber($listener);
39+
40+
$this->getDefaultMockSqliteEntityManager($evm);
41+
}
42+
43+
public function testBlameableUuid(): void
44+
{
45+
$company = new Company();
46+
$company->setName('ACME');
47+
48+
self::assertInstanceOf(Blameable::class, $company);
49+
50+
$this->em->persist($company);
51+
$this->em->flush();
52+
$this->em->clear();
53+
54+
/**
55+
* @var Company $foundCompany
56+
*/
57+
$foundCompany = $this->em->getRepository(self::COMPANY)->findOneBy(['name' => 'ACME']);
58+
$created = $foundCompany->getCreated();
59+
$createdUuid = $created instanceof Uuid ? $created->toRfc4122() : null;
60+
61+
self::assertSame($this->uuid->toRfc4122(), $createdUuid);
62+
}
63+
64+
protected function getUsedEntityFixtures(): array
65+
{
66+
return [
67+
self::COMPANY,
68+
];
69+
}
70+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Blameable\Fixture\Entity;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Blameable\Blameable;
17+
use Gedmo\Mapping\Annotation as Gedmo;
18+
use Symfony\Component\Uid\Uuid;
19+
use Symfony\Component\Uid\UuidV6;
20+
21+
/**
22+
* @ORM\Entity
23+
*/
24+
#[ORM\Entity]
25+
class Company implements Blameable
26+
{
27+
/**
28+
* @var int|null
29+
*
30+
* @ORM\Id
31+
* @ORM\GeneratedValue
32+
* @ORM\Column(type="integer")
33+
*/
34+
#[ORM\Id]
35+
#[ORM\GeneratedValue]
36+
#[ORM\Column(type: Types::INTEGER)]
37+
private $id;
38+
39+
/**
40+
* @var string|null
41+
*
42+
* @ORM\Column(name="name", type="string", length=128)
43+
*/
44+
#[ORM\Column(name: 'name', type: Types::STRING, length: 128)]
45+
private $name;
46+
47+
/**
48+
* @var UuidV6|null
49+
*
50+
* @Gedmo\Blameable(on="create")
51+
* @ORM\Column(name="created", type="uuid")
52+
*/
53+
#[ORM\Column(name: 'created', type: 'uuid')]
54+
#[Gedmo\Blameable(on: 'create')]
55+
private $created;
56+
57+
public function getId(): ?int
58+
{
59+
return $this->id;
60+
}
61+
62+
public function setName(?string $name): void
63+
{
64+
$this->name = $name;
65+
}
66+
67+
public function getName(): ?string
68+
{
69+
return $this->name;
70+
}
71+
72+
public function getCreated(): ?Uuid
73+
{
74+
return $this->created;
75+
}
76+
77+
public function setCreated(?UuidV6 $created): void
78+
{
79+
$this->created = $created;
80+
}
81+
}

tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
use Doctrine\Common\Annotations\AnnotationReader;
1313
use Doctrine\Common\Annotations\PsrCachedReader;
14+
use Doctrine\DBAL\Types\Type;
1415
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Bridge\Doctrine\Types\UuidType;
1517

1618
/*
1719
* This is bootstrap for phpUnit unit tests,
@@ -30,3 +32,5 @@
3032
$reader = new AnnotationReader();
3133
$reader = new PsrCachedReader($reader, new ArrayAdapter());
3234
$_ENV['annotation_reader'] = $reader;
35+
36+
Type::addType('uuid', UuidType::class);

0 commit comments

Comments
 (0)