Skip to content

Commit fdae73c

Browse files
committed
Add Behat test for PR #2183
1 parent b64da31 commit fdae73c

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
declare(strict_types=1);
1313

14+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
1415
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
1516
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
1617
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
1718
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositePrimitiveItem;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
1920
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Container;
21+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Customer;
2022
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2123
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
2224
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
@@ -36,6 +38,7 @@
3638
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FourthLevel;
3739
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
3840
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
41+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
3942
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
4043
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
4144
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
@@ -1024,4 +1027,33 @@ public function thereIsAPersonWithAGreeting(string $name, string $message)
10241027
$this->manager->flush();
10251028
$this->manager->clear();
10261029
}
1030+
1031+
/**
1032+
* @Given there is a order with same customer and receiver
1033+
*/
1034+
public function testEagerLoadingNotDuplicateRelation()
1035+
{
1036+
$customer = new Customer();
1037+
$customer->name = 'customer_name';
1038+
1039+
$address1 = new Address();
1040+
$address1->name = 'foo';
1041+
$address2 = new Address();
1042+
$address2->name = 'bar';
1043+
1044+
$order = new Order();
1045+
$order->recipient = $customer;
1046+
$order->customer = $customer;
1047+
1048+
$customer->addresses->add($address1);
1049+
$customer->addresses->add($address2);
1050+
1051+
$this->manager->persist($address1);
1052+
$this->manager->persist($address2);
1053+
$this->manager->persist($customer);
1054+
$this->manager->persist($order);
1055+
1056+
$this->manager->flush();
1057+
$this->manager->clear();
1058+
}
10271059
}

features/main/relation.feature

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,70 @@ Feature: Relations support
555555
}
556556
"""
557557

558+
Scenario: Eager load relations should not be duplicated, related to PR #2183
559+
Given there is a order with same customer and receiver
560+
When I add "Content-Type" header equal to "application/ld+json"
561+
And I send a "GET" request to "/orders"
562+
Then the response status code should be 200
563+
And the response should be in JSON
564+
And the JSON should be equal to:
565+
"""
566+
{
567+
"@context": "/contexts/Order",
568+
"@id": "/orders",
569+
"@type": "hydra:Collection",
570+
"hydra:member": [
571+
{
572+
"@id": "/orders/1",
573+
"@type": "Order",
574+
"id": 1,
575+
"customer": {
576+
"@id": "/customers/1",
577+
"@type": "Customer",
578+
"id": 1,
579+
"name": "customer_name",
580+
"addresses": [
581+
{
582+
"@id": "/addresses/1",
583+
"@type": "Address",
584+
"id": 1,
585+
"name": "foo"
586+
},
587+
{
588+
"@id": "/addresses/2",
589+
"@type": "Address",
590+
"id": 2,
591+
"name": "bar"
592+
}
593+
]
594+
},
595+
"recipient": {
596+
"@id": "/customers/1",
597+
"@type": "Customer",
598+
"id": 1,
599+
"name": "customer_name",
600+
"addresses": [
601+
{
602+
"@id": "/addresses/1",
603+
"@type": "Address",
604+
"id": 1,
605+
"name": "foo"
606+
},
607+
{
608+
"@id": "/addresses/2",
609+
"@type": "Address",
610+
"id": 2,
611+
"name": "bar"
612+
}
613+
]
614+
}
615+
}
616+
],
617+
"hydra:totalItems": 1
618+
}
619+
"""
620+
621+
558622
@dropSchema
559623
Scenario: Passing an invalid IRI to a relation
560624
When I add "Content-Type" header equal to "application/ld+json"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
19+
20+
/**
21+
* @ApiResource(
22+
* attributes={"normalization_context"={"groups"={"address_read"}}}
23+
* )
24+
* @ORM\Entity
25+
*/
26+
class Address
27+
{
28+
/**
29+
* @var int
30+
*
31+
* @ORM\Id
32+
* @ORM\Column(type="integer")
33+
* @ORM\GeneratedValue(strategy="AUTO")
34+
* @Groups({"address_read"})
35+
*/
36+
private $id;
37+
38+
/**
39+
* @ORM\Column(type="string")
40+
* @Groups({"address_read"})
41+
*/
42+
public $name;
43+
44+
public function getId()
45+
{
46+
return $this->id;
47+
}
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\Common\Collections\ArrayCollection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\Serializer\Annotation\Groups;
20+
21+
/**
22+
* @ApiResource(
23+
* attributes={"normalization_context"={"groups"={"customer_read"}}}
24+
* )
25+
* @ORM\Entity
26+
*/
27+
class Customer
28+
{
29+
/**
30+
* @var int
31+
*
32+
* @ORM\Id
33+
* @ORM\Column(type="integer")
34+
* @ORM\GeneratedValue(strategy="AUTO")
35+
* @Groups({"customer_read"})
36+
*/
37+
private $id;
38+
39+
/**
40+
* @ORM\Column(type="string")
41+
* @Groups({"customer_read"})
42+
*/
43+
public $name;
44+
45+
/**
46+
* @ORM\ManyToMany(targetEntity="Address")
47+
* @ORM\JoinColumn(nullable=false)
48+
* @Groups({"customer_read"})
49+
*/
50+
public $addresses;
51+
52+
public function __construct()
53+
{
54+
$this->addresses = new ArrayCollection();
55+
}
56+
57+
public function getId()
58+
{
59+
return $this->id;
60+
}
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
19+
use Symfony\Component\Validator\Constraints as Assert;
20+
21+
/**
22+
* @ApiResource(
23+
* attributes={"normalization_context"={"groups"={"order_read", "customer_read", "address_read"}}}
24+
* )
25+
* @ORM\Entity
26+
* @ORM\Table(name="`order`")
27+
*/
28+
class Order
29+
{
30+
/**
31+
* @var int
32+
*
33+
* @ORM\Id
34+
* @ORM\Column(type="integer")
35+
* @ORM\GeneratedValue(strategy="AUTO")
36+
* @Groups({"order_read"})
37+
*/
38+
private $id;
39+
40+
/**
41+
* @ORM\ManyToOne(targetEntity="Customer")
42+
* @ORM\JoinColumn(nullable=false)
43+
* @Groups({"order_read"})
44+
*/
45+
public $customer;
46+
47+
/**
48+
* @ORM\ManyToOne(targetEntity="Customer")
49+
* @ORM\JoinColumn(nullable=false)
50+
* @Assert\NotNull
51+
* @Groups({"order_read"})
52+
*/
53+
public $recipient;
54+
55+
public function getId()
56+
{
57+
return $this->id;
58+
}
59+
}

0 commit comments

Comments
 (0)