Skip to content

Commit 1cc6c51

Browse files
committed
Require ramsey/uuid-doctrine for testing (dev)
Added Blameable UUID test
1 parent bf2e44b commit 1cc6c51

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"phpstan/phpstan-doctrine": "^1.0",
6464
"phpstan/phpstan-phpunit": "^1.0",
6565
"phpunit/phpunit": "^8.5 || ^9.5",
66+
"ramsey/uuid-doctrine": "^2.0",
6667
"rector/rector": "^0.15.20",
6768
"symfony/console": "^4.4 || ^5.3 || ^6.0",
6869
"symfony/phpunit-bridge": "^6.0",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Tests\Blameable\Fixture\Entity\Company;
16+
use Gedmo\Blameable\Blameable;
17+
use Gedmo\Blameable\BlameableListener;
18+
use Gedmo\Tests\Tool\BaseTestCaseORM;
19+
use Ramsey\Uuid\Uuid;
20+
use Ramsey\Uuid\UuidInterface;
21+
22+
final class BlameableUuidTest extends BaseTestCaseORM
23+
{
24+
private const COMPANY = Company::class;
25+
26+
/**
27+
* @var UuidInterface $uuid
28+
*/
29+
private $uuid;
30+
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
$this->uuid = Uuid::uuid6();
36+
37+
$listener = new BlameableListener();
38+
$listener->setUserValue($this->uuid);
39+
40+
$evm = new EventManager();
41+
$evm->addEventSubscriber($listener);
42+
43+
$this->getDefaultMockSqliteEntityManager($evm);
44+
}
45+
46+
public function testBlameableUuid(): void
47+
{
48+
$company = new Company();
49+
$company->setName('ACME');
50+
51+
BlameableUuidTest::assertInstanceOf(Blameable::class, $company);
52+
53+
$this->em->persist($company);
54+
$this->em->flush();
55+
$this->em->clear();
56+
57+
/**
58+
* @var Company $foundCompany
59+
*/
60+
$foundCompany = $this->em->getRepository(self::COMPANY)->findOneBy(['name' => 'ACME']);
61+
$created = $foundCompany->getCreated();
62+
$createdUuid = $created instanceof UuidInterface ? $created->toString() : null;
63+
64+
BlameableUuidTest::assertSame($this->uuid->toString(), $createdUuid);
65+
}
66+
67+
protected function getUsedEntityFixtures(): array
68+
{
69+
return [
70+
self::COMPANY,
71+
];
72+
}
73+
}
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 Ramsey\Uuid\Rfc4122\UuidV6;
19+
use Ramsey\Uuid\UuidInterface;
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(): ?UuidInterface
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
$reader = new AnnotationReader();
3131
$reader = new PsrCachedReader($reader, new ArrayAdapter());
3232
$_ENV['annotation_reader'] = $reader;
33+
34+
\Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');

0 commit comments

Comments
 (0)