Skip to content

Commit bf2ce57

Browse files
ilicmsretenantograssiot
authored andcommitted
Add Behat test for PR #2183
1 parent b7afbf8 commit bf2ce57

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SecuredDummy as SecuredDummyDocument;
5050
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
5151
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
52+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
5253
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
5354
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
5455
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
5556
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositePrimitiveItem;
5657
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
5758
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Container;
59+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Customer;
5860
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
5961
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
6062
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
@@ -78,6 +80,7 @@
7880
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
7981
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
8082
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
83+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
8184
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
8285
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
8386
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
@@ -1505,4 +1508,33 @@ private function buildThirdLevel()
15051508
{
15061509
return $this->isOrm() ? new ThirdLevel() : new ThirdLevelDocument();
15071510
}
1511+
1512+
/**
1513+
* @Given there is a order with same customer and receiver
1514+
*/
1515+
public function testEagerLoadingNotDuplicateRelation()
1516+
{
1517+
$customer = new Customer();
1518+
$customer->name = 'customer_name';
1519+
1520+
$address1 = new Address();
1521+
$address1->name = 'foo';
1522+
$address2 = new Address();
1523+
$address2->name = 'bar';
1524+
1525+
$order = new Order();
1526+
$order->recipient = $customer;
1527+
$order->customer = $customer;
1528+
1529+
$customer->addresses->add($address1);
1530+
$customer->addresses->add($address2);
1531+
1532+
$this->manager->persist($address1);
1533+
$this->manager->persist($address2);
1534+
$this->manager->persist($customer);
1535+
$this->manager->persist($order);
1536+
1537+
$this->manager->flush();
1538+
$this->manager->clear();
1539+
}
15081540
}

features/main/relation.feature

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,71 @@ Feature: Relations support
558558
}
559559
"""
560560

561+
Scenario: Eager load relations should not be duplicated, related to PR #2183
562+
Given there is a order with same customer and receiver
563+
When I add "Content-Type" header equal to "application/ld+json"
564+
And I send a "GET" request to "/orders"
565+
Then the response status code should be 200
566+
And the response should be in JSON
567+
And the JSON should be equal to:
568+
"""
569+
{
570+
"@context": "/contexts/Order",
571+
"@id": "/orders",
572+
"@type": "hydra:Collection",
573+
"hydra:member": [
574+
{
575+
"@id": "/orders/1",
576+
"@type": "Order",
577+
"id": 1,
578+
"customer": {
579+
"@id": "/customers/1",
580+
"@type": "Customer",
581+
"id": 1,
582+
"name": "customer_name",
583+
"addresses": [
584+
{
585+
"@id": "/addresses/1",
586+
"@type": "Address",
587+
"id": 1,
588+
"name": "foo"
589+
},
590+
{
591+
"@id": "/addresses/2",
592+
"@type": "Address",
593+
"id": 2,
594+
"name": "bar"
595+
}
596+
]
597+
},
598+
"recipient": {
599+
"@id": "/customers/1",
600+
"@type": "Customer",
601+
"id": 1,
602+
"name": "customer_name",
603+
"addresses": [
604+
{
605+
"@id": "/addresses/1",
606+
"@type": "Address",
607+
"id": 1,
608+
"name": "foo"
609+
},
610+
{
611+
"@id": "/addresses/2",
612+
"@type": "Address",
613+
"id": 2,
614+
"name": "bar"
615+
}
616+
]
617+
}
618+
}
619+
],
620+
"hydra:totalItems": 1
621+
}
622+
"""
623+
624+
625+
@dropSchema
561626
Scenario: Passing an invalid IRI to a relation
562627
When I add "Content-Type" header equal to "application/ld+json"
563628
And I send a "POST" request to "/relation_embedders" with body:
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
* forceEager=false
25+
* )
26+
* @ORM\Entity
27+
* @ORM\Table(name="`order`")
28+
*/
29+
class Order
30+
{
31+
/**
32+
* @var int
33+
*
34+
* @ORM\Id
35+
* @ORM\Column(type="integer")
36+
* @ORM\GeneratedValue(strategy="AUTO")
37+
* @Groups({"order_read"})
38+
*/
39+
private $id;
40+
41+
/**
42+
* @ORM\ManyToOne(targetEntity="Customer")
43+
* @ORM\JoinColumn(nullable=false)
44+
* @Groups({"order_read"})
45+
*/
46+
public $customer;
47+
48+
/**
49+
* @ORM\ManyToOne(targetEntity="Customer")
50+
* @ORM\JoinColumn(nullable=false)
51+
* @Assert\NotNull
52+
* @Groups({"order_read"})
53+
*/
54+
public $recipient;
55+
56+
public function getId()
57+
{
58+
return $this->id;
59+
}
60+
}

0 commit comments

Comments
 (0)