Skip to content

Commit 426f3ae

Browse files
committed
Add test for forceUseAttributeReader option
1 parent 9213dce commit 426f3ae

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" >
3+
<entity name="Gedmo\Tests\Mapping\Fixture\Xml\SeparateDoctrineMapping" table="separate_doctrine_mapping">
4+
<id name="id" type="integer" column="id">
5+
<generator strategy="AUTO"/>
6+
</id>
7+
<field name="title" type="string"/>
8+
<field name="created" type="datetime"/>
9+
<field name="updated" type="datetime"/>
10+
</entity>
11+
</doctrine-mapping>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Mapping\Fixture\Xml;
13+
use Gedmo\Mapping\Annotation as Gedmo;
14+
15+
class SeparateDoctrineMapping
16+
{
17+
/**
18+
* @var int
19+
*/
20+
private $id;
21+
22+
private $title;
23+
24+
/**
25+
* @var \DateTime
26+
*/
27+
#[Gedmo\Timestampable(on: 'create')]
28+
private $created;
29+
/**
30+
* @var \DateTime
31+
*/
32+
#[Gedmo\Timestampable(on: 'update')]
33+
private $updated;
34+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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\Mapping;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
16+
use Doctrine\ORM\Mapping\Driver\YamlDriver;
17+
use Gedmo\Tests\Mapping\Fixture\Category;
18+
use Gedmo\Tests\Mapping\Fixture\Xml\SeparateDoctrineMapping;
19+
use Gedmo\Tests\Mapping\Fixture\Xml\Timestampable;
20+
use Gedmo\Tests\Mapping\ORMMappingTestCase;
21+
use Gedmo\Timestampable\TimestampableListener;
22+
use Psr\Cache\CacheItemPoolInterface;
23+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
24+
25+
/**
26+
* These are mapping tests for timestampable extension
27+
*
28+
* @author Gediminas Morkevicius <[email protected]>
29+
*/
30+
final class SeparateWithDoctrineDriverMappingTest extends ORMMappingTestCase
31+
{
32+
private EntityManager $em;
33+
private EntityManager $emWithForceAttr;
34+
/**
35+
* @var CacheItemPoolInterface
36+
*/
37+
protected $cacheWithForceAttr;
38+
39+
protected function setUp(): void
40+
{
41+
if (PHP_VERSION_ID < 80000) {
42+
self::markTestSkipped('Test requires PHP 8.');
43+
}
44+
45+
parent::setUp();
46+
47+
// Base Configuration without forceUseAttributeReader option
48+
$listener = new TimestampableListener();
49+
$listener->setCacheItemPool($this->cache);
50+
51+
$this->em = $this->getBasicEntityManager();
52+
$this->em->getEventManager()->addEventSubscriber($listener);
53+
54+
55+
// Configuration with forceUseAttributeReader option
56+
$this->cacheWithForceAttr = new ArrayAdapter();
57+
$listener = new TimestampableListener();
58+
$listener->setCacheItemPool($this->cacheWithForceAttr);
59+
$listener->setForceUseAttributeReader(true);
60+
$this->emWithForceAttr = $this->getBasicEntityManager();
61+
$this->emWithForceAttr->getEventManager()->addEventSubscriber($listener);
62+
63+
64+
}
65+
66+
67+
/**
68+
* @return \Generator<string, array{class-string}>
69+
*
70+
* @note the XML fixture has a different mapping from the other configs, so it is tested separately
71+
*/
72+
public static function dataTimestampableObject(): \Generator
73+
{
74+
yield 'Model with separated doctrine(XML Driver), gedmo(Attribute Driver)' => [SeparateDoctrineMapping::class, false, true];
75+
yield 'Model without attributes (XML Driver only)' => [Timestampable::class, false, false];
76+
yield 'Model with attributes (Attribute Driver only)' => [Category::class, true, true];
77+
78+
}
79+
80+
/**
81+
* @param class-string $className
82+
*
83+
* @dataProvider dataTimestampableObject
84+
*/
85+
public function testForceGedmoInAttributeDriverMapping(string $className, bool $doctrineMappingInAttributes, bool $gedmoMappingInAttributes): void
86+
{
87+
// This entityManager configured to enforce the use of attributes for gedmo.
88+
$em = $this->emWithForceAttr;
89+
$cache = $this->cacheWithForceAttr;
90+
$em->getClassMetadata($className);
91+
92+
$this->em->getClassMetadata($className);
93+
$cacheId = ExtensionMetadataFactory::getCacheId($className, 'Gedmo\Timestampable');
94+
$config = $cache->getItem($cacheId)->get();
95+
96+
if($gedmoMappingInAttributes) {
97+
static::assertArrayHasKey('create', $config);
98+
static::assertSame('created', $config['create'][0]);
99+
static::assertArrayHasKey('update', $config);
100+
static::assertSame('updated', $config['update'][0]);
101+
}else{
102+
static::assertArrayNotHasKey('create', $config);
103+
static::assertArrayNotHasKey('update', $config);
104+
}
105+
}
106+
/**
107+
* @param class-string $className
108+
*
109+
* @dataProvider dataTimestampableObject
110+
*/
111+
public function testStandardWayMapping(string $className, bool $doctrineMappingInAttributes, bool $gedmoMappingInAttributes): void
112+
{
113+
$em = $this->em;
114+
$cache = $this->cache;
115+
$em->getClassMetadata($className);
116+
117+
$this->em->getClassMetadata($className);
118+
$cacheId = ExtensionMetadataFactory::getCacheId($className, 'Gedmo\Timestampable');
119+
$config = $cache->getItem($cacheId)->get();
120+
121+
if($gedmoMappingInAttributes && !$doctrineMappingInAttributes) {
122+
// example: doctrine use XmlDriver, Gedmo - use AttributeDriver,
123+
// Gedmo attributes - unreadable
124+
static::assertArrayNotHasKey('create', $config);
125+
static::assertArrayNotHasKey('update', $config);
126+
}else{
127+
static::assertArrayHasKey('create', $config);
128+
static::assertSame('created', $config['create'][0]);
129+
static::assertArrayHasKey('update', $config);
130+
static::assertSame('updated', $config['update'][0]);
131+
}
132+
133+
}
134+
135+
}

0 commit comments

Comments
 (0)