Skip to content

Commit

Permalink
Merge pull request #104 from inlm/pr/bugfix-property-read
Browse files Browse the repository at this point in the history
EntityReflection: property name must be unique
  • Loading branch information
castamir committed Mar 14, 2017
2 parents 7f10b9e + abdb921 commit 2e47308
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/LeanMapper/Reflection/EntityReflection.php
Expand Up @@ -179,6 +179,12 @@ private function parseProperties()
foreach (AnnotationsParser::parseAnnotationValues($annotationType, $member->getDocComment()) as $definition) {
$property = PropertyFactory::createFromAnnotation($annotationType, $definition, $member, $this->mapper);
// collision check
if (isset($this->properties[$property->getName()])) {
throw new InvalidStateException(
"Duplicated property '{$property->getName()}' in entity {$this->getName()}. Please fix property name."
);
}

$column = $property->getColumn();
if ($column !== null and $property->isWritable()) {
if (isset($columns[$column])) {
Expand Down
78 changes: 78 additions & 0 deletions tests/LeanMapper/Entity.propertyCollision.phpt
@@ -0,0 +1,78 @@
<?php

use LeanMapper\Entity;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';

//////////

/**
* @property string $web (website)
* @property string $site (website)
*/
class AuthorWithDuplicatedColumn extends Entity
{
}

Assert::exception(
function () {
$reflection = AuthorWithDuplicatedColumn::getReflection();
},
'LeanMapper\Exception\InvalidStateException',
"Mapping collision in property 'site' (column 'website') in entity AuthorWithDuplicatedColumn. Please fix mapping or make chosen properties read only (using property-read)."
);

//////////

/**
* @property int $id
* @property string $id
*/
class AuthorWithDuplicatedProperty1 extends Entity
{
}

Assert::exception(
function () {
$reflection = AuthorWithDuplicatedProperty1::getReflection();
},
'LeanMapper\Exception\InvalidStateException',
"Duplicated property 'id' in entity AuthorWithDuplicatedProperty1. Please fix property name."
);

//////////

/**
* @property int $id
* @property string $id (name)
*/
class AuthorWithDuplicatedProperty2 extends Entity
{
}

Assert::exception(
function () {
$reflection = AuthorWithDuplicatedProperty2::getReflection();
},
'LeanMapper\Exception\InvalidStateException',
"Duplicated property 'id' in entity AuthorWithDuplicatedProperty2. Please fix property name."
);

//////////

/**
* @property int $id
* @property-read string $id
*/
class AuthorWithDuplicatedReadProperty extends Entity
{
}

Assert::exception(
function () {
$reflection = AuthorWithDuplicatedReadProperty::getReflection();
},
'LeanMapper\Exception\InvalidStateException',
"Duplicated property 'id' in entity AuthorWithDuplicatedReadProperty. Please fix property name."
);

0 comments on commit 2e47308

Please sign in to comment.