Skip to content

Commit f886952

Browse files
authored
Merge pull request nilportugues#73 from franbenz/doctrine-tests
New Doctrine serialization test: Comment refers to Comment
2 parents 1136071 + ffb9cb3 commit f886952

File tree

5 files changed

+278
-135
lines changed

5 files changed

+278
-135
lines changed

src/Helpers/DataIncludedHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ protected static function removeResourcesNotIncluded(array &$mappings, $parentTy
264264
*/
265265
protected static function isDeleteableIncludedResource(array &$mappings, $parentType, $includeValue)
266266
{
267-
return count($mappings[$parentType]->getIncludedResources()) > 0
267+
return !empty($mappings[$parentType])
268+
&& count($mappings[$parentType]->getIncludedResources()) > 0
268269
&& false === in_array($includeValue[Serializer::CLASS_IDENTIFIER_KEY], $mappings[$parentType]->getIncludedResources(), true);
269270
}
270271
}

src/Helpers/DataLinksHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function setResponseDataLinks(array &$mappings, array &$value)
3232
$data = [];
3333
$type = $value[Serializer::CLASS_IDENTIFIER_KEY];
3434

35-
if (\is_scalar($type)) {
35+
if (\is_scalar($type) && !empty($mappings[$type])) {
3636
$copy = $value;
3737
RecursiveFormatterHelper::formatScalarValues($copy);
3838
if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) {
@@ -105,7 +105,7 @@ public static function setResponseDataRelationship(array &$mappings, array &$arr
105105
$parentType
106106
);
107107

108-
if (!empty($selfLink = $mappings[$parentType]->getRelationshipSelfUrl($propertyName))) {
108+
if (!empty($mappings[$parentType]) && !empty($selfLink = $mappings[$parentType]->getRelationshipSelfUrl($propertyName))) {
109109
$href = \str_replace($idProperties, $idValues, $selfLink);
110110
if ($selfLink != $href) {
111111
$propertyNameKey = DataAttributesHelper::transformToValidMemberName($propertyName);
@@ -126,7 +126,7 @@ public static function setResponseDataRelationship(array &$mappings, array &$arr
126126
$parentType = $parent[Serializer::CLASS_IDENTIFIER_KEY];
127127

128128
//Removes relationships related to the current resource if filtering include resources has been set.
129-
if (!empty($mappings[$parentType]->isFilteringIncludedResources())) {
129+
if (!empty($mappings[$parentType]) && !empty($mappings[$parentType]->isFilteringIncludedResources())) {
130130
foreach ($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName] as $position => $includedResource) {
131131
if (count($mappings[$parentType]->getIncludedResources()) > 0 &&
132132
false === in_array($type, $mappings[$parentType]->getIncludedResources(), true)
@@ -222,7 +222,7 @@ public static function setResponseDataRelationshipSelfLinks($propertyName, array
222222
$data = [];
223223
$parentType = $parent[Serializer::CLASS_IDENTIFIER_KEY];
224224

225-
if (\is_scalar($parentType)) {
225+
if (\is_scalar($parentType) && !empty($mappings[$parentType])) {
226226
$copy = $parent;
227227
RecursiveFormatterHelper::formatScalarValues($copy);
228228
if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) {

src/Helpers/PropertyHelper.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ class PropertyHelper
2828
*/
2929
public static function setResponseDataTypeAndId(array &$mappings, array &$value)
3030
{
31-
$type = $value[Serializer::CLASS_IDENTIFIER_KEY];
32-
33-
//Recursion base case
34-
if (empty($mappings[$type])) {
31+
if (empty($type = $value[Serializer::CLASS_IDENTIFIER_KEY]) || (empty($mappings[$type]))) {
3532
return [];
3633
}
3734

tests/Integrations/Doctrine/AbstractTestCase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
use Doctrine\DBAL\Schema\SchemaException;
1010
use Doctrine\ORM\Tools\SchemaTool;
11+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Customer;
12+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CustomerMapping;
13+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\PostMapping;
14+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Mappings\CommentMapping;
15+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Post;
16+
use NilPortugues\Tests\Api\JsonApi\Integrations\Doctrine\Entity\Comment;
1117

1218
require_once 'bootstrap.php';
1319

@@ -17,13 +23,44 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
1723
* @var \Doctrine\ORM\EntityManager
1824
*/
1925
protected static $entityManager;
26+
protected static $classConfig;
2027

2128
public static function setUpBeforeClass()
2229
{
2330
self::$entityManager = GetEntityManager();
2431
// Build the schema for sqlite
2532
self::generateSchema();
2633

34+
$newCustomer = new Customer();
35+
$newCustomer->setActive(true);
36+
$newCustomer->setName('Name 1');
37+
self::$entityManager->persist($newCustomer);
38+
39+
$newPost = new Post();
40+
$newPost->setCustomer($newCustomer);
41+
$newPost->setDate(new \DateTime('2016-07-12 16:30:12.000000'));
42+
$newPost->setDescription('Description test');
43+
self::$entityManager->persist($newPost);
44+
45+
$newComment = new Comment();
46+
$newComment->setPost($newPost);
47+
$newComment->setComment('Comment 1');
48+
self::$entityManager->persist($newComment);
49+
50+
$newComment2 = new Comment();
51+
$newComment2->setPost($newPost);
52+
$newComment2->setComment('Comment 2');
53+
$newComment2->setParentComment($newComment);
54+
self::$entityManager->persist($newComment2);
55+
56+
self::$entityManager->flush();
57+
58+
self::$classConfig = [
59+
CustomerMapping::class,
60+
PostMapping::class,
61+
CommentMapping::class,
62+
];
63+
2764
parent::setUpBeforeClass();
2865
}
2966

0 commit comments

Comments
 (0)