Skip to content

Commit

Permalink
Merge pull request #41 from compwright/to-array-test
Browse files Browse the repository at this point in the history
Test toArray() more thoroughly
  • Loading branch information
harikt authored Jul 3, 2024
2 parents 92d08c3 + 83b07dd commit e71d171
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/ToArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function ($entity)
return $entity->toArray();
}

if (is_object($entity)) {
return (array) $entity;
}

return $entity;
},
$this->data
Expand Down
7 changes: 6 additions & 1 deletion tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function testObjectsInCollectionAreInIdentityMap()

public function testToArray()
{
$this->assertEquals($this->data, $this->collection->toArray());
$expected = array_map(
fn (object $entity): array => (array) $entity,
$this->data
);
$actual = $this->collection->toArray();
$this->assertEquals($expected, $actual);
}
}
7 changes: 4 additions & 3 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ public function testMagicPropertyAccess()
public function testToArray()
{
$entity = $this->newGenericEntity();
$expect = [
$expected = [
'foo' => 'bar',
'baz' => 'dim',
'zim' => 'gir',
'related' => (object) [
'related' => [
'foreign_field' => 'foreign_value',
],
];
$this->assertEquals($expect, $entity->toArray());
$actual = $entity->toArray();
$this->assertEquals($expected, $actual);
}
}
30 changes: 30 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,34 @@ public function testClear()
$this->assertSame([], $this->manager->$type->getAllEntities());
}
}

public function testToArray()
{
$relation_builder = new RelationBuilder;
$type_builder = new TypeBuilder;
$this->manager = new Manager($type_builder, $relation_builder);

// toArray() cannot handle circular relationshiops
$types = include __DIR__ . DIRECTORY_SEPARATOR . 'fixture_types.php';
unset($types['authors']['relation_names']);
unset($types['comments']['relation_names']);
unset($types['metas']['relation_names']);
unset($types['tags']['relation_names']);
foreach ($types as $name => $info) {
$this->manager->setType($name, $info);
}

// load data into the types
$fixture = include __DIR__ . '/fixture_data.php';
foreach ($fixture as $type => $data) {
$this->manager->$type->load($data);
}

$postIds = array_column($fixture['posts'], 'id');
$actual = $this->manager->posts->getCollection($postIds)->toArray();

$expected = include __DIR__ . '/to_array.php';

$this->assertEquals($expected, $actual);
}
}
171 changes: 171 additions & 0 deletions tests/to_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

return [
[
'id' => '1',
'author_id' => '1',
'body' => 'Anna post #1',
'fake_field' => '69',
'null_field' => null,
'meta' => [
'id' => '1',
'post_id' => '1',
'data' => 'meta 1',
],
'comments' => [
[
'id' => '1',
'post_id' => '1',
'body' => 'comment #1 on anna #1',
],
[
'id' => '2',
'post_id' => '1',
'body' => 'comment #2 on anna #1',
],
[
'id' => '3',
'post_id' => '1',
'body' => 'comment #3 on anna #1',
],
],
'author' => [
'id' => '1',
'name' => 'Anna',
],
'tags' => [
[
'id' => '1',
'name' => 'zim',
],
[
'id' => '2',
'name' => 'dib',
],
],
],
[
'id' => '2',
'author_id' => '1',
'body' => 'Anna post #2',
'fake_field' => '69',
'null_field' => null,
'meta' => [
'id' => '2',
'post_id' => '2',
'data' => 'meta 2',
],
'comments' => [],
'author' => [
'id' => '1',
'name' => 'Anna',
],
'tags' => [
[
'id' => '2',
'name' => 'dib',
],
[
'id' => '3',
'name' => 'gir',
],
],
],
[
'id' => '3',
'author_id' => '1',
'body' => 'Anna post #3',
'fake_field' => '69',
'null_field' => null,
'meta' => [
'id' => '3',
'post_id' => '3',
'data' => 'meta 3',
],
'comments' => [],
'author' => [
'id' => '1',
'name' => 'Anna',
],
'tags' => [
[
'id' => '3',
'name' => 'gir',
],
[
'id' => '1',
'name' => 'zim',
],
],
],
[
'id' => '4',
'author_id' => '2',
'body' => 'Clara post #1',
'fake_field' => '88',
'null_field' => null,
'meta' => [
'id' => '4',
'post_id' => '4',
'data' => 'meta 4',
],
'comments' => [],
'author' => [
'id' => '2',
'name' => 'Betty',
],
'tags' => [
[
'id' => '1',
'name' => 'zim',
],
[
'id' => '2',
'name' => 'dib',
],
[
'id' => '3',
'name' => 'gir',
],
],
],
[
'id' => '5',
'author_id' => '2',
'body' => 'Clara post #2',
'fake_field' => '88',
'null_field' => null,
'meta' => [
'id' => '5',
'post_id' => '5',
'data' => 'meta 5',
],
'comments' => [
[
'id' => '4',
'post_id' => '5',
'body' => 'comment #1 on clara #2',
],
[
'id' => '5',
'post_id' => '5',
'body' => 'comment #2 on clara #2',
],
[
'id' => '6',
'post_id' => '5',
'body' => 'comment #3 on clara #2',
],
],
'author' => [
'id' => '2',
'name' => 'Betty',
],
'tags' => [
[
'id' => '2',
'name' => 'dib',
],
],
],
];

0 comments on commit e71d171

Please sign in to comment.