Skip to content

Commit 1136071

Browse files
authored
Merge pull request nilportugues#72 from franbenz/doctrine-tests
New Doctrine test
2 parents 96ffd21 + 03ea904 commit 1136071

File tree

9 files changed

+573
-0
lines changed

9 files changed

+573
-0
lines changed

tests/Integrations/Doctrine/DoctrineTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use NilPortugues\Api\Mapping\Mapper;
88
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer;
99
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CustomerMapping;
10+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\PostMapping;
11+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CommentMapping;
12+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post;
13+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment;
1014

1115
class DoctrineTest extends AbstractTestCase
1216
{
@@ -57,4 +61,107 @@ public function testPersistAndSerializeSimpleEntity()
5761
$this->assertEquals($newCustomer->getName(), $savedCustomer[0]->getName());
5862
$this->assertEquals(json_decode($expected, true), json_decode($customerSerialize, true));
5963
}
64+
65+
public function testPersistAndSerializeComplexEntity()
66+
{
67+
$newCustomer = new Customer();
68+
$newCustomer->setActive(true);
69+
$newCustomer->setName('Name 1');
70+
71+
self::$entityManager->persist($newCustomer);
72+
self::$entityManager->flush();
73+
74+
$newPost = new Post();
75+
$newPost->setCustomer($newCustomer);
76+
$newPost->setDate(new \DateTime('2016-07-12 16:30:12.000000'));
77+
$newPost->setDescription('Description test');
78+
self::$entityManager->persist($newPost);
79+
self::$entityManager->flush();
80+
81+
$newComment = new Comment();
82+
$newComment->setPost($newPost);
83+
$newComment->setComment('Comment 1');
84+
self::$entityManager->persist($newComment);
85+
self::$entityManager->flush();
86+
87+
$repoCustomer = self::$entityManager->getRepository(Comment::class);
88+
$savedComment = $repoCustomer->findAll();
89+
90+
$classConfig = [
91+
CustomerMapping::class,
92+
PostMapping::class,
93+
CommentMapping::class,
94+
];
95+
96+
$expected = <<<JSON
97+
{
98+
"data":
99+
[{
100+
"type":"comment",
101+
"id":"1",
102+
"attributes":
103+
{
104+
"comment":"Comment 1",
105+
"id":1,
106+
"parent_comment":null,
107+
"parent_id":null
108+
},
109+
"links":
110+
{"self":{"href":"http://example.com/comment/1"}},
111+
"relationships":
112+
{
113+
"post":
114+
{
115+
"data":
116+
{
117+
"type":"post",
118+
"id":"1"
119+
}
120+
}
121+
}
122+
}
123+
],
124+
"included":
125+
[
126+
{
127+
"type":"customer",
128+
"id":"2",
129+
"attributes":
130+
{
131+
"name":"Name 1",
132+
"active":true
133+
},
134+
"links":{"self":{"href":"http://example.com/customer/2"}}
135+
},
136+
{ "type":"post",
137+
"id":"1",
138+
"attributes":
139+
{
140+
"date":{"date":"2016-07-12 16:30:12.000000","timezone_type":3,"timezone":"Europe/Madrid"},
141+
"description":"Description test"
142+
},
143+
"relationships":
144+
{
145+
"customer":
146+
{
147+
"data":
148+
{
149+
"type":"customer",
150+
"id":"2"
151+
}
152+
}
153+
},
154+
"links":{"self":{"href":"http://example.com/post/1"}}
155+
}
156+
],
157+
"jsonapi":{"version":"1.0"}
158+
}
159+
JSON;
160+
$mapper = new Mapper($classConfig);
161+
$transformer = new JsonApiTransformer($mapper);
162+
$serializer = new JsonApiSerializer($transformer);
163+
$customerSerialize = $serializer->serialize($savedComment);
164+
165+
$this->assertEquals(json_decode($expected, true), json_decode($customerSerialize, true));
166+
}
60167
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;
4+
5+
/**
6+
* Comment.
7+
*/
8+
class Comment
9+
{
10+
/**
11+
* @var int
12+
*/
13+
private $id;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $comment;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $parent_id;
24+
25+
/**
26+
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment
27+
*/
28+
private $parentComment;
29+
30+
/**
31+
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post
32+
*/
33+
private $post;
34+
35+
/**
36+
* Get id.
37+
*
38+
* @return int
39+
*/
40+
public function getId()
41+
{
42+
return $this->id;
43+
}
44+
45+
/**
46+
* Set comment.
47+
*
48+
* @param string $comment
49+
*
50+
* @return Comment
51+
*/
52+
public function setComment($comment)
53+
{
54+
$this->comment = $comment;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* Get comment.
61+
*
62+
* @return string
63+
*/
64+
public function getComment()
65+
{
66+
return $this->comment;
67+
}
68+
69+
/**
70+
* Set parentId.
71+
*
72+
* @param int $parentId
73+
*
74+
* @return Comment
75+
*/
76+
public function setParentId($parentId)
77+
{
78+
$this->parent_id = $parentId;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* Get parentId.
85+
*
86+
* @return int
87+
*/
88+
public function getParentId()
89+
{
90+
return $this->parent_id;
91+
}
92+
93+
/**
94+
* Set parentComment.
95+
*
96+
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment $parentComment
97+
*
98+
* @return Comment
99+
*/
100+
public function setParentComment(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment $parentComment = null)
101+
{
102+
$this->parentComment = $parentComment;
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Get parentComment.
109+
*
110+
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment
111+
*/
112+
public function getParentComment()
113+
{
114+
return $this->parentComment;
115+
}
116+
117+
/**
118+
* Set post.
119+
*
120+
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post $post
121+
*
122+
* @return Comment
123+
*/
124+
public function setPost(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post $post = null)
125+
{
126+
$this->post = $post;
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* Get post.
133+
*
134+
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post
135+
*/
136+
public function getPost()
137+
{
138+
return $this->post;
139+
}
140+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
7+
/**
8+
* CommentRepository.
9+
*
10+
* This class was generated by the Doctrine ORM. Add your own custom
11+
* repository methods below.
12+
*/
13+
class CommentRepository extends EntityRepository
14+
{
15+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;
4+
5+
/**
6+
* Post.
7+
*/
8+
class Post
9+
{
10+
/**
11+
* @var int
12+
*/
13+
private $id;
14+
15+
/**
16+
* @var \DateTime
17+
*/
18+
private $date;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $description;
24+
25+
/**
26+
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer
27+
*/
28+
private $customer;
29+
30+
/**
31+
* Get id.
32+
*
33+
* @return int
34+
*/
35+
public function getId()
36+
{
37+
return $this->id;
38+
}
39+
40+
/**
41+
* Set date.
42+
*
43+
* @param \DateTime $date
44+
*
45+
* @return Post
46+
*/
47+
public function setDate($date)
48+
{
49+
$this->date = $date;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* Get date.
56+
*
57+
* @return \DateTime
58+
*/
59+
public function getDate()
60+
{
61+
return $this->date;
62+
}
63+
64+
/**
65+
* Set description.
66+
*
67+
* @param string $description
68+
*
69+
* @return Post
70+
*/
71+
public function setDescription($description)
72+
{
73+
$this->description = $description;
74+
75+
return $this;
76+
}
77+
78+
/**
79+
* Get description.
80+
*
81+
* @return string
82+
*/
83+
public function getDescription()
84+
{
85+
return $this->description;
86+
}
87+
88+
/**
89+
* Set customer.
90+
*
91+
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer $customer
92+
*
93+
* @return Post
94+
*/
95+
public function setCustomer(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer $customer = null)
96+
{
97+
$this->customer = $customer;
98+
99+
return $this;
100+
}
101+
102+
/**
103+
* Get customer.
104+
*
105+
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer
106+
*/
107+
public function getCustomer()
108+
{
109+
return $this->customer;
110+
}
111+
}

0 commit comments

Comments
 (0)