Skip to content

New Doctrine test #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions tests/Integrations/Doctrine/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use NilPortugues\Api\Mapping\Mapper;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CustomerMapping;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\PostMapping;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CommentMapping;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post;
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment;

class DoctrineTest extends AbstractTestCase
{
Expand Down Expand Up @@ -57,4 +61,107 @@ public function testPersistAndSerializeSimpleEntity()
$this->assertEquals($newCustomer->getName(), $savedCustomer[0]->getName());
$this->assertEquals(json_decode($expected, true), json_decode($customerSerialize, true));
}

public function testPersistAndSerializeComplexEntity()
{
$newCustomer = new Customer();
$newCustomer->setActive(true);
$newCustomer->setName('Name 1');

self::$entityManager->persist($newCustomer);
self::$entityManager->flush();

$newPost = new Post();
$newPost->setCustomer($newCustomer);
$newPost->setDate(new \DateTime('2016-07-12 16:30:12.000000'));
$newPost->setDescription('Description test');
self::$entityManager->persist($newPost);
self::$entityManager->flush();

$newComment = new Comment();
$newComment->setPost($newPost);
$newComment->setComment('Comment 1');
self::$entityManager->persist($newComment);
self::$entityManager->flush();

$repoCustomer = self::$entityManager->getRepository(Comment::class);
$savedComment = $repoCustomer->findAll();

$classConfig = [
CustomerMapping::class,
PostMapping::class,
CommentMapping::class,
];

$expected = <<<JSON
{
"data":
[{
"type":"comment",
"id":"1",
"attributes":
{
"comment":"Comment 1",
"id":1,
"parent_comment":null,
"parent_id":null
},
"links":
{"self":{"href":"http://example.com/comment/1"}},
"relationships":
{
"post":
{
"data":
{
"type":"post",
"id":"1"
}
}
}
}
],
"included":
[
{
"type":"customer",
"id":"2",
"attributes":
{
"name":"Name 1",
"active":true
},
"links":{"self":{"href":"http://example.com/customer/2"}}
},
{ "type":"post",
"id":"1",
"attributes":
{
"date":{"date":"2016-07-12 16:30:12.000000","timezone_type":3,"timezone":"Europe/Madrid"},
"description":"Description test"
},
"relationships":
{
"customer":
{
"data":
{
"type":"customer",
"id":"2"
}
}
},
"links":{"self":{"href":"http://example.com/post/1"}}
}
],
"jsonapi":{"version":"1.0"}
}
JSON;
$mapper = new Mapper($classConfig);
$transformer = new JsonApiTransformer($mapper);
$serializer = new JsonApiSerializer($transformer);
$customerSerialize = $serializer->serialize($savedComment);

$this->assertEquals(json_decode($expected, true), json_decode($customerSerialize, true));
}
}
140 changes: 140 additions & 0 deletions tests/Integrations/Doctrine/Entity/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;

/**
* Comment.
*/
class Comment
{
/**
* @var int
*/
private $id;

/**
* @var string
*/
private $comment;

/**
* @var int
*/
private $parent_id;

/**
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment
*/
private $parentComment;

/**
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post
*/
private $post;

/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Set comment.
*
* @param string $comment
*
* @return Comment
*/
public function setComment($comment)
{
$this->comment = $comment;

return $this;
}

/**
* Get comment.
*
* @return string
*/
public function getComment()
{
return $this->comment;
}

/**
* Set parentId.
*
* @param int $parentId
*
* @return Comment
*/
public function setParentId($parentId)
{
$this->parent_id = $parentId;

return $this;
}

/**
* Get parentId.
*
* @return int
*/
public function getParentId()
{
return $this->parent_id;
}

/**
* Set parentComment.
*
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment $parentComment
*
* @return Comment
*/
public function setParentComment(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment $parentComment = null)
{
$this->parentComment = $parentComment;

return $this;
}

/**
* Get parentComment.
*
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment
*/
public function getParentComment()
{
return $this->parentComment;
}

/**
* Set post.
*
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post $post
*
* @return Comment
*/
public function setPost(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post $post = null)
{
$this->post = $post;

return $this;
}

/**
* Get post.
*
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post
*/
public function getPost()
{
return $this->post;
}
}
15 changes: 15 additions & 0 deletions tests/Integrations/Doctrine/Entity/CommentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;

use Doctrine\ORM\EntityRepository;

/**
* CommentRepository.
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CommentRepository extends EntityRepository
{
}
111 changes: 111 additions & 0 deletions tests/Integrations/Doctrine/Entity/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity;

/**
* Post.
*/
class Post
{
/**
* @var int
*/
private $id;

/**
* @var \DateTime
*/
private $date;

/**
* @var string
*/
private $description;

/**
* @var \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer
*/
private $customer;

/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Set date.
*
* @param \DateTime $date
*
* @return Post
*/
public function setDate($date)
{
$this->date = $date;

return $this;
}

/**
* Get date.
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}

/**
* Set description.
*
* @param string $description
*
* @return Post
*/
public function setDescription($description)
{
$this->description = $description;

return $this;
}

/**
* Get description.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}

/**
* Set customer.
*
* @param \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer $customer
*
* @return Post
*/
public function setCustomer(\NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer $customer = null)
{
$this->customer = $customer;

return $this;
}

/**
* Get customer.
*
* @return \NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer
*/
public function getCustomer()
{
return $this->customer;
}
}
Loading