Skip to content

Commit

Permalink
fix: if xml value is set, change value in class metadata (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfuentesboost authored Aug 31, 2023
1 parent 52e6411 commit c4c6f85
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ a release.
## [Unreleased]
### Changed
- Add conflict with "doctrine/orm" >=2.16 (temporary change until code is fixed)
### Fixed
- References: fixed condition in `XML` Driver that did not allow to retrieve from the entity definition the `mappedBy` and `inversedBy` fields.

### Fixed
- Fix bug collecting metadata for inherited mapped classes
Expand Down
2 changes: 2 additions & 0 deletions schemas/orm/doctrine-extensions-mapping-2-2.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<xs:element name="loggable" type="gedmo:loggable"/>
<xs:element name="soft-deleteable" type="gedmo:soft-deleteable"/>
<xs:element name="uploadable" type="gedmo:uploadable"/>
<xs:element name="reference" type="gedmo:reference"/>
<!-- field -->
<xs:element name="slug" type="gedmo:slug"/>
<xs:element name="translatable" type="gedmo:translatable"/>
Expand Down Expand Up @@ -65,6 +66,7 @@
<xs:complexType name="reference">
<xs:attribute name="type" type="gedmo:reference-type" use="required" />
<xs:attribute name="field" type="xs:string" use="required" />
<xs:attribute name="reference" type="xs:string" use="required" />
<xs:attribute name="identifier" type="xs:string" use="required" />
<xs:attribute name="class" type="xs:string" use="required" />
<xs:attribute name="mappedBy" type="xs:string" use="optional" />
Expand Down
4 changes: 2 additions & 2 deletions src/References/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public function readExtendedMetadata($meta, array &$config)
'identifier' => $identifier,
];

if (!$this->_isAttributeSet($element, 'mappedBy')) {
if ($this->_isAttributeSet($element, 'mappedBy')) {
$config[$reference][$field]['mappedBy'] = $this->_getAttribute($element, 'mappedBy');
}
if (!$this->_isAttributeSet($element, 'inversedBy')) {
if ($this->_isAttributeSet($element, 'inversedBy')) {
$config[$reference][$field]['inversedBy'] = $this->_getAttribute($element, 'inversedBy');
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Gedmo\Tests\Mapping\Fixture\Xml\References" table="references">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" type="string" length="128"/>
<gedmo:reference field="users" reference="referenceMany" type="document" identifier="referecesId" class="Gedmo\Tests\Mapping\Fixture\Document\User" mappedBy="reference"/>
</entity>
</doctrine-mapping>
32 changes: 32 additions & 0 deletions tests/Gedmo/Mapping/Fixture/Xml/References.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Mapping\Fixture\Xml;

use Gedmo\Tests\Mapping\Fixture\Document\User;

class References
{
/**
* @var int
*/
private $id;

/**
* @var string
*/
private $name;

/**
* @var User[]
*/
private $users;
}
80 changes: 80 additions & 0 deletions tests/Gedmo/Mapping/Xml/ReferencesMappingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Mapping\Xml;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Gedmo\References\ReferencesListener;
use Gedmo\Tests\Mapping\Fixture\Xml\References;
use Gedmo\Tests\Tool\BaseTestCaseOM;

/**
* @author Guillermo Fuentes <guillermofuentesquijada@gmail.com>
*/
final class ReferencesMappingTest extends BaseTestCaseOM
{
/**
* @var EntityManager
*/
private $em;

/**
* @var ReferencesListener
*/
private $referencesListener;

protected function setUp(): void
{
parent::setUp();

$reader = new AnnotationReader();
$annotationDriver = new AnnotationDriver($reader);

$xmlDriver = new XmlDriver(__DIR__.'/../Driver/Xml');

$chain = new MappingDriverChain();
$chain->addDriver($xmlDriver, 'Gedmo\Tests\Mapping\Fixture\Xml');
$chain->addDriver($annotationDriver, 'Gedmo\Tests\Mapping\Fixture');

$this->referencesListener = new ReferencesListener();
$this->evm = new EventManager();
$this->evm->addEventSubscriber($this->referencesListener);

$this->em = $this->getDefaultMockSqliteEntityManager([
References::class,
], $chain);
}

public function testMetadata(): void
{
$meta = $this->em->getClassMetadata(References::class);
$config = $this->referencesListener->getConfiguration($this->em, $meta->getName());

static::assertArrayHasKey('referenceMany', $config);
static::assertArrayHasKey('useObjectClass', $config);
static::assertSame(References::class, $config['useObjectClass']);
$configInternal = $config['referenceMany'];
static::assertArrayHasKey('users', $configInternal);
$configUsers = $configInternal['users'];
static::assertArrayHasKey('field', $configUsers);
static::assertArrayHasKey('type', $configUsers);
static::assertSame('document', $configUsers['type']);
static::assertArrayHasKey('class', $configUsers);
static::assertArrayHasKey('identifier', $configUsers);
static::assertArrayHasKey('mappedBy', $configUsers);
static::assertSame('reference', $configUsers['mappedBy']);
}
}

0 comments on commit c4c6f85

Please sign in to comment.