Skip to content

Commit

Permalink
Fix Mongo\Mapper and setTargetModel in Relation\BelongsTo.
Browse files Browse the repository at this point in the history
Tests for fetchRelations.
  • Loading branch information
Bukarinov committed Dec 29, 2012
1 parent 03e418d commit 1880698
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 16 deletions.
2 changes: 1 addition & 1 deletion library/GeometriaLab/Model/Persistent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function fetchRelations($relationNames = null, $refresh = false)
$relationNames = $this->parseRelationNames($relationNames);

This comment has been minimized.

Copy link
@shumkov

shumkov Dec 29, 2012

Contributor

проверяем есть ли вообще релейшены

}

foreach ($relationNames as $relationName => $childRelations) {
foreach ((array) $relationNames as $relationName => $childRelations) {
$relation = $this->getRelation($relationName);
if ($relation !== null) {
$relation->setTargetObjectsToCollection($this, $refresh, $childRelations);
Expand Down
9 changes: 7 additions & 2 deletions library/GeometriaLab/Model/Persistent/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function setTargetModel(ModelInterface $targetModel = null)
throw new \InvalidArgumentException('Target property is null');
}

$this->targetModel = false;
$this->targetModel = $targetModel;
} else {
$targetPropertyValue = null;

Expand Down Expand Up @@ -101,7 +101,12 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
$relation = $model->getRelation($this->getProperty()->getName());
if ($refresh || !$relation->hasTargetModel()) {
if ($relation instanceof HasMany) {
$hasTargetModel = $relation->hasTargetModels();
} else {
$hasTargetModel = $relation->hasTargetModel();
}
if ($refresh || !$hasTargetModel) {
// TODO '0' value will not pass check, should it?
$value = $model->get($this->getProperty()->getOriginProperty());
if ($value) {
Expand Down
11 changes: 8 additions & 3 deletions library/GeometriaLab/Model/Persistent/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
// TODO 0 value will not pass check, should it ?
$values = $model->get($this->getProperty()->getOriginProperty());
$values = (array) $model->get($this->getProperty()->getOriginProperty());

This comment has been minimized.

Copy link
@shumkov

shumkov Dec 29, 2012

Contributor

сомнительный кусок

if ($values) {
$relation = $model->getRelation($this->getProperty()->getName());
if ($refresh || !$relation->hasTargetModel()) {
if ($relation instanceof HasMany) {
$hasTargetModel = $relation->hasTargetModels();
} else {
$hasTargetModel = $relation->hasTargetModel();
}
if ($refresh || !$hasTargetModel) {
$localModels[] = array(
'model' => $model,
'values' => (array) $values,
'values' => $values,
);
$fetchValues = array_merge($fetchValues, $values);
}
Expand Down
7 changes: 6 additions & 1 deletion library/GeometriaLab/Model/Persistent/Relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
$relation = $model->getRelation($this->getProperty()->getName());
if ($refresh || !$relation->hasTargetModel()) {
if ($relation instanceof HasMany) {
$hasTargetModel = $relation->hasTargetModels();
} else {
$hasTargetModel = $relation->hasTargetModel();
}
if ($refresh || !$hasTargetModel) {
// TODO '0' value will not pass check, should it?
$value = $model->get($this->getProperty()->getOriginProperty());
if ($value) {
Expand Down
18 changes: 16 additions & 2 deletions library/GeometriaLab/Mongo/Model/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,24 @@ static public function setServiceManager(ZendServiceManager $serviceManager)
protected function transformModelDataForStorage(array $data)
{
if (isset($data['id'])) {
$data['_id'] = new \MongoId($data['id']);
if ((string)$data['_id'] != $data['id']) {
if (is_array($data['id'])) {
foreach ($data['id'] as &$value) {
if (is_array($value)) {
foreach ($value as &$val) {
$val = new \MongoId($val);
}
} else {
$value = new \MongoId($value);
}
}
$data['_id'] = $data['id'];
} else {
$data['_id'] = new \MongoId($data['id']);
if ((string)$data['_id'] != $data['id']) {
$data['_id'] = $data['id'];
}
}

unset($data['id']);
}

Expand Down
179 changes: 172 additions & 7 deletions tests/GeometriaLabTest/Model/Persistent/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,180 @@

namespace GeometriaLabTest\Model\Persistent;

use GeometriaLab\Model\Persistent\Collection,
GeometriaLabTest\Model\Persistent\TestModels\Model;
use GeometriaLabTest\Mongo\AbstractTestCase,
GeometriaLabTest\Model\Persistent\TestModels\Relations\Man,
GeometriaLabTest\Model\Persistent\TestModels\Relations\Dog,
GeometriaLabTest\Model\Persistent\TestModels\Relations\Woman;

class CollectionTest extends \PHPUnit_Framework_TestCase
class CollectionTest extends AbstractTestCase
{
public function testFetchRelations()
public function testFetchAllRelations()
{
$this->setExpectedException('\RuntimeException', 'Not implemented yet!');
$c = new Collection();
$c->fetchRelations(array('foo'));
$this->createModels();
$this->createModels();

$collection = Man::getMapper()->getAll();

foreach ($collection as $model) {
/* @var Man $model */
$this->assertFalse($model->getRelation('dog')->hasTargetModel());
$this->assertFalse($model->getRelation('women')->hasTargetModels());
}

$collection->fetchRelations(
null

This comment has been minimized.

Copy link
@shumkov

shumkov Dec 29, 2012

Contributor

remove

);

foreach ($collection as $model) {
/* @var Man $model */
$this->assertTrue($model->getRelation('dog')->hasTargetModel());
$this->assertTrue($model->getRelation('women')->hasTargetModels());
}
}

public function testFetchOneRelation()
{
$this->createModels();
$this->createModels();

$collection = Man::getMapper()->getAll();

foreach ($collection as $model) {
/* @var Man $model */
$this->assertFalse($model->getRelation('dog')->hasTargetModel());
$this->assertFalse($model->getRelation('women')->hasTargetModels());
}

$collection->fetchRelations(
'dog'
);

foreach ($collection as $model) {
/* @var Man $model */
$this->assertTrue($model->getRelation('dog')->hasTargetModel());
$this->assertFalse($model->getRelation('women')->hasTargetModels());
}
}

public function testNestedHasOneRelation()
{
$this->createModels();
$this->createModels();

$collection = Man::getMapper()->getAll();

foreach ($collection as $model) {
/* @var Man $model */
$this->assertFalse($model->getRelation('dog')->hasTargetModel());
}

$collection->fetchRelations(array(
'dog' => 'man',
));

foreach ($collection as $model) {
/* @var Man $model */
$this->assertTrue($model->getRelation('dog')->hasTargetModel());
$this->assertTrue($model->get('dog')->getRelation('man')->hasTargetModel());
}
}

public function testNestedBelongsToRelation()
{
$this->createModels();
$this->createModels();

$collection = Dog::getMapper()->getAll();

foreach ($collection as $model) {
/* @var Dog $model */
$this->assertFalse($model->getRelation('man')->hasTargetModel());
}

$collection->fetchRelations(array(
'man' => 'women',
));

foreach ($collection as $model) {
/* @var Dog $model */
$this->assertTrue($model->getRelation('man')->hasTargetModel());
$this->assertTrue($model->get('man')->getRelation('women')->hasTargetModels());

foreach ($model->get('man')->get('women') as $woman) {
$this->assertInstanceOf('\GeometriaLabTest\Model\Persistent\TestModels\Relations\Woman', $woman);
}
}
}

public function testNestedHasManyRelation()
{
$this->createModels();
$this->createModels();

$query = Woman::getMapper()->createQuery();
$collection = Woman::getMapper()->getAll($query);

foreach ($collection as $model) {
/* @var Woman $model */
$this->assertFalse($model->getRelation('man')->hasTargetModel());
}

$collection->fetchRelations(array(
'man' => 'dog',
));

foreach ($collection as $model) {
/* @var Woman $model */
$this->assertTrue($model->getRelation('man')->hasTargetModel());
$this->assertTrue($model->get('man')->getRelation('dog')->hasTargetModel());
}
}

public function testNotExistentRelation()
{
$this->setExpectedException("\\InvalidArgumentException", "Model doesn't have 'bad' relation");

$this->createModels();

$collection = Woman::getMapper()->getAll();
$collection->fetchRelations('bad');
}

public function testBadChildRelationName()
{
$this->setExpectedException("\\InvalidArgumentException", "Child relation must be a string but integer is given.");

$this->createModels();

$collection = Woman::getMapper()->getAll();
$collection->fetchRelations(array('man' => 1));
}

protected function createModels()
{
$philip = new Man(array(
'name' => 'Philip',
));
$philip->save();

$seymour = new Dog(array(
'name' => 'Seymour',
'manId' => $philip->id,
));
$seymour->save();

$michelle = new Woman(array(
'name' => 'Michelle',
'manId' => $philip->id,
));
$michelle->save();

$leela = new Woman(array(
'name' => 'Leela',
'manId' => $philip->id,
));
$leela->save();


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;

use GeometriaLab\Model\Persistent\AbstractModel;

/**
* @property string $id {"primary": true}
* @property string $name
* @property string $manId
* @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Man $man {"relation": "belongsTo", "originProperty": "manId"}
*
* @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "dog"}
*/
class Dog extends AbstractModel
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;

use GeometriaLab\Model\Persistent\AbstractModel;

/**
* @property string $id {"primary": true}
* @property string $name
*
* @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Dog $dog {"relation": "hasOne", "targetProperty": "manId"}
* @property \GeometriaLab\Model\Persistent\Collection $women {"relation": "hasMany",
* "targetProperty": "manId",
* "targetModelClass": "\\GeometriaLabTest\\Model\\Persistent\\TestModels\\Relations\\Woman"}
*
* @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "man"}
*/
class Man extends AbstractModel
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;

use GeometriaLab\Model\Persistent\AbstractModel;

/**
* @property string $id {"primary": true}
* @property string $name
* @property string $manId
* @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Man $man {"relation": "belongsTo", "originProperty": "manId"}
*
* @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "woman"}
*/
class Woman extends AbstractModel
{

}

0 comments on commit 1880698

Please sign in to comment.