Skip to content

Commit

Permalink
Add tests for ids option in marshaller.
Browse files Browse the repository at this point in the history
Add tests for the various cases that `ids` will frequently be used in.
  • Loading branch information
markstory committed Sep 3, 2015
1 parent 9386230 commit 10e8146
Showing 1 changed file with 156 additions and 2 deletions.
158 changes: 156 additions & 2 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -490,6 +490,52 @@ public function testOneBelongsToManyJoinData()
);
}

/**
* Test that the ids option restricts to only accepting ids for belongs to many associations.
*
* @return void
*/
public function testOneBelongsToManyOnlyIdsRejectArray()
{
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
['tag' => 'news'],
['tag' => 'cakephp'],
],
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, [
'associated' => ['Tags' => ['ids' => true]]
]);
$this->assertEmpty($result->tags, 'Only ids should be marshalled.');
}

/**
* Test that the ids option restricts to only accepting ids for belongs to many associations.
*
* @return void
*/
public function testOneBelongsToManyOnlyIdsWithIds()
{
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
'_ids' => [1, 2],
['tag' => 'news'],
],
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, [
'associated' => ['Tags' => ['ids' => true]]
]);
$this->assertCount(2, $result->tags, 'Ids should be marshalled.');
}

/**
* Test marshalling nested associations on the _joinData structure.
*
Expand Down Expand Up @@ -808,7 +854,7 @@ public function testBelongsToManyWithMixedData()
*
* @return void
*/
public function testHasManyWithIds()
public function testOneHasManyWithIds()
{
$data = [
'title' => 'article',
Expand All @@ -825,12 +871,58 @@ public function testHasManyWithIds()
$this->assertEquals($article->comments[1], $this->comments->get(2));
}

/**
* Test that the ids option restricts to only accepting ids for hasmany associations.
*
* @return void
*/
public function testOneHasManyOnlyIdsRejectArray()
{
$data = [
'title' => 'article',
'body' => 'some content',
'comments' => [
['comment' => 'first comment'],
['comment' => 'second comment'],
]
];

$marshaller = new Marshaller($this->articles);
$article = $marshaller->one($data, [
'associated' => ['Comments' => ['ids' => true]]
]);
$this->assertEmpty($article->comments);
}

/**
* Test that the ids option restricts to only accepting ids for hasmany associations.
*
* @return void
*/
public function testOneHasManyOnlyIdsWithIds()
{
$data = [
'title' => 'article',
'body' => 'some content',
'comments' => [
'_ids' => [1, 2],
['comment' => 'first comment'],
]
];

$marshaller = new Marshaller($this->articles);
$article = $marshaller->one($data, [
'associated' => ['Comments' => ['ids' => true]]
]);
$this->assertCount(2, $article->comments);
}

/**
* Test HasMany association with invalid data
*
* @return void
*/
public function testHasManyInvalidData()
public function testOneHasManyInvalidData()
{
$data = [
'title' => 'new title',
Expand Down Expand Up @@ -1505,6 +1597,68 @@ public function testMergeBelongsToManyEntitiesFromIdsEmptyValue()
$this->assertCount(0, $result->tags);
}

/**
* Test that the ids option restricts to only accepting ids for belongs to many associations.
*
* @return void
*/
public function testMergeBelongsToManyOnlyIdsRejectArray()
{
$entity = new Entity([
'title' => 'Haz tags',
'body' => 'Some content here',
'tags' => [
new Entity(['id' => 1, 'name' => 'Cake']),
new Entity(['id' => 2, 'name' => 'PHP'])
]
]);

$data = [
'title' => 'Haz moar tags',
'tags' => [
['name' => 'new'],
['name' => 'awesome']
]
];
$entity->accessible('*', true);
$marshall = new Marshaller($this->articles);
$result = $marshall->merge($entity, $data, [
'associated' => ['Tags' => ['ids' => true]]
]);
$this->assertCount(0, $result->tags);
}

/**
* Test that the ids option restricts to only accepting ids for belongs to many associations.
*
* @return void
*/
public function testMergeBelongsToManyOnlyIdsWithIds()
{
$entity = new Entity([
'title' => 'Haz tags',
'body' => 'Some content here',
'tags' => [
new Entity(['id' => 1, 'name' => 'Cake']),
new Entity(['id' => 2, 'name' => 'PHP'])
]
]);

$data = [
'title' => 'Haz moar tags',
'tags' => [
'_ids' => [3]
]
];
$entity->accessible('*', true);
$marshall = new Marshaller($this->articles);
$result = $marshall->merge($entity, $data, [
'associated' => ['Tags' => ['ids' => true]]
]);
$this->assertCount(1, $result->tags);
$this->assertEquals('tag3', $result->tags[0]->name);
}

/**
* Test that invalid _joinData (scalar data) is not marshalled.
*
Expand Down

0 comments on commit 10e8146

Please sign in to comment.