Skip to content

Commit 44af515

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

File tree

8 files changed

+429
-0
lines changed

8 files changed

+429
-0
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
declare(strict_types=1);
1313

14+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Address as AddressDocument;
1415
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Answer as AnswerDocument;
1516
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeItem as CompositeItemDocument;
1617
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeLabel as CompositeLabelDocument;
1718
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositePrimitiveItem as CompositePrimitiveItemDocument;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeRelation as CompositeRelationDocument;
20+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Customer as CustomerDocument;
1921
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
2022
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyAggregateOffer as DummyAggregateOfferDocument;
2123
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyCar as DummyCarDocument;
@@ -37,6 +39,7 @@
3739
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\FourthLevel as FourthLevelDocument;
3840
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Greeting as GreetingDocument;
3941
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\MaxDepthDummy as MaxDepthDummyDocument;
42+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Order as OrderDocument;
4043
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Person as PersonDocument;
4144
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\PersonToPet as PersonToPetDocument;
4245
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Pet as PetDocument;
@@ -49,12 +52,14 @@
4952
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SecuredDummy as SecuredDummyDocument;
5053
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
5154
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
55+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
5256
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
5357
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
5458
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
5559
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositePrimitiveItem;
5660
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
5761
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Container;
62+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Customer;
5863
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
5964
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
6065
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
@@ -78,6 +83,7 @@
7883
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
7984
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
8085
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
86+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
8187
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
8288
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
8389
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
@@ -1505,4 +1511,34 @@ private function buildThirdLevel()
15051511
{
15061512
return $this->isOrm() ? new ThirdLevel() : new ThirdLevelDocument();
15071513
}
1514+
1515+
/**
1516+
* @Given there is a order with same customer and receiver
1517+
*/
1518+
public function testEagerLoadingNotDuplicateRelation()
1519+
{
1520+
1521+
$customer = $this->isORM() ? new Customer() : new CustomerDocument();
1522+
$customer->name = 'customer_name';
1523+
1524+
$address1 = $this->isORM() ? new Address() : new AddressDocument();
1525+
$address1->name = 'foo';
1526+
$address2 = $this->isORM() ? new Address() : new AddressDocument();
1527+
$address2->name = 'bar';
1528+
1529+
$order = $this->isOrm() ? new Order() : new OrderDocument();
1530+
$order->recipient = $customer;
1531+
$order->customer = $customer;
1532+
1533+
$customer->addresses->add($address1);
1534+
$customer->addresses->add($address2);
1535+
1536+
$this->manager->persist($address1);
1537+
$this->manager->persist($address2);
1538+
$this->manager->persist($customer);
1539+
$this->manager->persist($order);
1540+
1541+
$this->manager->flush();
1542+
$this->manager->clear();
1543+
}
15081544
}

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
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Document;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
19+
20+
/**
21+
* @ApiResource(
22+
* attributes={"normalization_context"={"groups"={"address_read"}}}
23+
* )
24+
* @ODM\Document
25+
*/
26+
class Address
27+
{
28+
/**
29+
* @var int
30+
*
31+
* @ODM\Id(strategy="INCREMENT", type="integer")
32+
* @Groups({"address_read"})
33+
*/
34+
private $id;
35+
36+
/**
37+
* @ODM\Field(type="string")
38+
* @Groups({"address_read"})
39+
*/
40+
public $name;
41+
42+
public function getId()
43+
{
44+
return $this->id;
45+
}
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Document;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\Common\Collections\ArrayCollection;
18+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
19+
use Symfony\Component\Serializer\Annotation\Groups;
20+
21+
/**
22+
* @ApiResource(
23+
* attributes={"normalization_context"={"groups"={"customer_read"}}}
24+
* )
25+
* @ODM\Document
26+
*/
27+
class Customer
28+
{
29+
/**
30+
* @var int
31+
*
32+
* @ODM\Id(strategy="INCREMENT", type="integer")
33+
* @Groups({"customer_read"})
34+
*/
35+
private $id;
36+
37+
/**
38+
* @ODM\Field(type="string")
39+
* @Groups({"customer_read"})
40+
*/
41+
public $name;
42+
43+
/**
44+
* @ODM\ReferenceMany(targetDocument=Address::class)
45+
* @Groups({"customer_read"})
46+
*/
47+
public $addresses;
48+
49+
public function __construct()
50+
{
51+
$this->addresses = new ArrayCollection();
52+
}
53+
54+
public function getId()
55+
{
56+
return $this->id;
57+
}
58+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Document;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
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+
* @ODM\Document
27+
*/
28+
class Order
29+
{
30+
/**
31+
* @var int
32+
*
33+
* @ODM\Id(strategy="INCREMENT", type="integer")
34+
* @Groups({"order_read"})
35+
*/
36+
private $id;
37+
38+
/**
39+
* @ODM\ReferenceMany(targetDocument=Customer::class)
40+
* @Groups({"order_read"})
41+
*/
42+
public $customer;
43+
44+
/**
45+
* @ODM\ReferenceMany(targetDocument=Customer::class)
46+
* @Assert\NotNull
47+
* @Groups({"order_read"})
48+
*/
49+
public $recipient;
50+
51+
public function getId()
52+
{
53+
return $this->id;
54+
}
55+
}
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+
}

0 commit comments

Comments
 (0)