Skip to content

Commit

Permalink
Fixed tests. Missing mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Jun 16, 2016
1 parent 6c06960 commit d523b5c
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 80 deletions.
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,35 @@ Content-type: application/json; charset=utf-8

```json
{
"post_id": 9,
"headline": "Hello World",
"body": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
"post_id": 9,
"headline": "Hello World",
"body": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+02:00",
"accepted_at": "2015-07-19T00:00:00+02:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"self": {
"href": "http://example.com/posts/9"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+00:00",
"accepted_at": "2015-07-19T00:00:00+00:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"comments": {
"href": "http://localhost:8000/post/9/comments"
},
"self": {
"href": "http://localhost:8000/post/9"
}
"comments": {
"href": "http://example.com/posts/9/comments"
}
}
}
```

Expand Down
179 changes: 126 additions & 53 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,43 @@ private function getPostObject()
*/
public function testItWillRenamePropertiesAndHideFromClass()
{
$mappings = [
[
'class' => Post::class,
'alias' => 'Message',
'aliased_properties' => [
'title' => 'headline',
'content' => 'body',
],
'hide_properties' => [
'comments',
],
'id_properties' => [
'postId',
],
'urls' => [
// Mandatory
'self' => 'http://example.com/posts/{postId}',
// Optional
'comments' => 'http://example.com/posts/{postId}/comments',
],
],
];
$mappings = $this->mappings();

$expected = <<<JSON
{
"post_id": 9,
"headline": "Hello World",
"body": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
"post_id": 9,
"headline": "Hello World",
"body": "Your first post",
"author": {
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+02:00",
"accepted_at": "2015-07-19T00:00:00+02:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"self": {
"href": "http://example.com/posts/9"
},
"links": {
"comments": {"href": "http://example.com/posts/9/comments" },
"self": {"href": "http://example.com/posts/9" }
"comments": {
"href": "http://example.com/posts/9/comments"
}
}
}
JSON;


$this->assertEquals(
json_decode($expected, true),
json_decode((new JsonSerializer(new Mapper($mappings)))->serialize($this->getPostObject()), true)
Expand All @@ -111,28 +109,7 @@ public function testItWillRenamePropertiesAndHideFromClass()

public function testItCanSerializeArrays()
{
$mappings = [
[
'class' => Post::class,
'alias' => 'Message',
'aliased_properties' => [
'title' => 'headline',
'content' => 'body',
],
'hide_properties' => [
'comments',
],
'id_properties' => [
'postId',
],
'urls' => [
// Mandatory
'self' => 'http://example.com/posts/{postId}',
// Optional
'comments' => 'http://example.com/posts/{postId}/comments',
],
],
];
$mappings = $this->mappings();

$expected = <<<JSON
[
Expand All @@ -144,6 +121,20 @@ public function testItCanSerializeArrays()
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+02:00",
"accepted_at": "2015-07-19T00:00:00+02:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"self": {
"href": "http://example.com/posts/9"
Expand All @@ -161,6 +152,20 @@ public function testItCanSerializeArrays()
"user_id": 1,
"name": "Post Author"
},
"comments": [
{
"comment_id": 1000,
"dates": {
"created_at": "2015-07-18T12:13:00+02:00",
"accepted_at": "2015-07-19T00:00:00+02:00"
},
"comment": "Have no fear, sers, your king is safe.",
"user": {
"user_id": 2,
"name": "Barristan Selmy"
}
}
],
"links": {
"self": {
"href": "http://example.com/posts/9"
Expand All @@ -186,4 +191,72 @@ public function testGetTransformer()

$this->assertInstanceOf(JsonTransformer::class, $serializer->getTransformer());
}


/**
* @return array
*/
protected function mappings()
{
return [
[
'class' => Post::class,
'alias' => 'Message',
'aliased_properties' => [
'author' => 'author',
'title' => 'headline',
'content' => 'body',
],
'hide_properties' => [
],
'id_properties' => [
'postId',
],
'urls' => [
// Mandatory
'self' => 'http://example.com/posts/{postId}',
// Optional
'comments' => 'http://example.com/posts/{postId}/comments',
],
],
[
'class' => User::class,
'alias' => '',
'aliased_properties' => [],
'hide_properties' => [],
'id_properties' => [
'userId',
],
'urls' => [
'self' => 'http://example.com/users/{userId}',
'friends' => 'http://example.com/users/{userId}/friends',
'comments' => 'http://example.com/users/{userId}/comments',
],
],
[
'class' => Comment::class,
'alias' => '',
'aliased_properties' => [],
'hide_properties' => [],
'id_properties' => [
'commentId',
],
'urls' => [
'self' => 'http://example.com/comments/{commentId}',
],
],
[
'class' => CommentId::class,
'alias' => '',
'aliased_properties' => [],
'hide_properties' => [],
'id_properties' => [
'commentId',
],
'urls' => [
'self' => 'http://example.com/comments/{commentId}',
],
],
];
}
}

0 comments on commit d523b5c

Please sign in to comment.