Skip to content

Commit 3ce65d7

Browse files
committed
fix subnamespace collections matching in ModelValidator
1 parent b65921f commit 3ce65d7

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

Diff for: lib/Webforge/Doctrine/Compiler/Model.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getAssociationFor(GeneratedEntity $entity, GeneratedProperty $pr
3737
foreach ($this->associations as $associationsPair) {
3838
if (
3939
$associationsPair->owning->property === $property && $entity->equals($associationsPair->owning->entity) ||
40-
$associationsPair->inverse->property === $property && $entity->equals($associationsPair->inverse->entity)
40+
isset($associationsPair->inverse) && $associationsPair->inverse->property === $property && $entity->equals($associationsPair->inverse->entity)
4141
) {
4242
return $associationsPair;
4343
}

Diff for: lib/Webforge/Doctrine/Compiler/ModelValidator.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,14 @@ protected function validateType(stdClass $property, stdClass $entity, Model $mod
145145
throw new InvalidModelException(sprintf("The type: '%s' cannot be parsed for entity '%s'.", $typeName, $entity->fqn), 0, $e);
146146
}
147147

148-
if ($type instanceof CollectionType && $type->getType() instanceof ObjectType && $model->hasEntity($referenceEntityName = $type->getType()->getClass()->getName())) {
149-
$type = new EntityCollectionReference($model->getEntity($referenceEntityName));
148+
if ($type instanceof CollectionType && $type->getType() instanceof ObjectType) {
149+
if ($model->hasEntity($referenceEntityName = $type->getType()->getClass()->getFQN())) { // FQN is here subnamespace + name
150+
$type = new EntityCollectionReference($model->getEntity($referenceEntityName));
151+
} else {
152+
throw new InvalidModelException(
153+
sprintf("The entityName '%s' in type from property %s::%s cannot be found in model. Did you misspelled the name of the entity?", $referenceEntityName, $entity->fqn, $property->name)
154+
);
155+
}
150156
}
151157
}
152158

Diff for: lib/Webforge/Doctrine/Compiler/Test/ModelBase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function assertIsMappedBy($mappedBy, Array $association) {
3636
}
3737

3838
protected function assertIsInversedBy($mappedBy, Array $association) {
39-
$this->assertEquals($mappedBy, $association['inversedBy'], 'is invseredBy does not match');
39+
$this->assertEquals($mappedBy, $association['inversedBy'], 'is inversedBy does not match');
4040
}
4141

4242
protected function assertHasTargetENtity($fqn, Array $association) {

Diff for: model-tests/ModelAssociationsOneToManyTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ public function testManyToOneDoctrineMetadata() {
9393
$this->assertHasTargetEntity($this->authorClass, $author);
9494
$this->assertIsInversedBy('writtenPosts', $author);
9595
}
96+
97+
public function testManyToOneWithSubNamespacesMetadata() {
98+
$paragraphClass = $this->elevateFull('ACME\Blog\Entities\ContentStream\Paragraph');
99+
$streamClass = $this->elevateFull('ACME\Blog\Entities\ContentStream\Stream');
100+
$metadata = $this->assertDoctrineMetadata($streamClass);
101+
102+
$paragraphs = $this->assertAssociationMapping('paragraphs', $metadata);
103+
104+
$this->assertHasTargetEntity($paragraphClass, $paragraphs);
105+
}
96106
}

Diff for: tests/Webforge/Doctrine/Compiler/CompilerAcceptanceTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function testThrowsModelException_WhenAssociationsAreAmbigous() {
7373
"id": { "type": "DefaultId" },
7474
"author": { "type": "Author" },
7575
"revisor": { "type": "Author", "nullable": true },
76-
"categories": { "type": "Collection<Category>", "isOwning": true },
7776
"created": { "type": "DateTime" },
7877
"modified": { "type": "DateTime", "nullable": true }
7978
},

Diff for: tests/Webforge/Doctrine/Compiler/ModelValidatorTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testLikesShortPropertiesWithOnlyTypeAndName() {
7777
7878
"properties": {
7979
"id": "DefaultId",
80-
"posts": { "type": "Collection<Post>" }
80+
"label": { "type": "String" }
8181
}
8282
}
8383
]
@@ -169,6 +169,19 @@ public function testDoesNotLikeMalFormedTypes() {
169169
));
170170
}
171171

172+
public function testDoesNotLikeCollectionTypesWithWrongEntityName() {
173+
$this->assertInvalid($this->wrapEntity(
174+
(object) array(
175+
"name"=>"User",
176+
"properties"=>(object) array(
177+
"email"=>(object) array(
178+
'type'=>'Collection<NonExistingEntity>'
179+
)
180+
)
181+
)
182+
));
183+
}
184+
172185
public function testDoesNotLikeEntitiesThatArentExisting() {
173186
$jsonModel = <<<'JSON'
174187
{

0 commit comments

Comments
 (0)