Skip to content

Add Behat test for PR 2183 #2188

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 1 commit into from
Apr 6, 2019
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
35 changes: 35 additions & 0 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

declare(strict_types=1);

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Address as AddressDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Answer as AnswerDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeItem as CompositeItemDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeLabel as CompositeLabelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositePrimitiveItem as CompositePrimitiveItemDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeRelation as CompositeRelationDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Customer as CustomerDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyAggregateOffer as DummyAggregateOfferDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyCar as DummyCarDocument;
Expand All @@ -37,6 +39,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\FourthLevel as FourthLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Greeting as GreetingDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\MaxDepthDummy as MaxDepthDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Order as OrderDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Person as PersonDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\PersonToPet as PersonToPetDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Pet as PetDocument;
Expand All @@ -49,12 +52,14 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SecuredDummy as SecuredDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositePrimitiveItem;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Container;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Customer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
Expand All @@ -78,6 +83,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
Expand Down Expand Up @@ -1505,4 +1511,33 @@ private function buildThirdLevel()
{
return $this->isOrm() ? new ThirdLevel() : new ThirdLevelDocument();
}

/**
* @Given there is a order with same customer and receiver
*/
public function testEagerLoadingNotDuplicateRelation()
{
$customer = $this->isOrm() ? new Customer() : new CustomerDocument();
$customer->name = 'customer_name';

$address1 = $this->isOrm() ? new Address() : new AddressDocument();
$address1->name = 'foo';
$address2 = $this->isOrm() ? new Address() : new AddressDocument();
$address2->name = 'bar';

$order = $this->isOrm() ? new Order() : new OrderDocument();
$order->recipient = $customer;
$order->customer = $customer;

$customer->addresses->add($address1);
$customer->addresses->add($address2);

$this->manager->persist($address1);
$this->manager->persist($address2);
$this->manager->persist($customer);
$this->manager->persist($order);

$this->manager->flush();
$this->manager->clear();
}
}
65 changes: 65 additions & 0 deletions features/main/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,71 @@ Feature: Relations support
}
"""

Scenario: Eager load relations should not be duplicated
Given there is a order with same customer and receiver
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/orders"
Then the response status code should be 200
And the response should be in JSON
And the JSON should be equal to:
"""
{
"@context": "/contexts/Order",
"@id": "/orders",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/orders/1",
"@type": "Order",
"id": 1,
"customer": {
"@id": "/customers/1",
"@type": "Customer",
"id": 1,
"name": "customer_name",
"addresses": [
{
"@id": "/addresses/1",
"@type": "Address",
"id": 1,
"name": "foo"
},
{
"@id": "/addresses/2",
"@type": "Address",
"id": 2,
"name": "bar"
}
]
},
"recipient": {
"@id": "/customers/1",
"@type": "Customer",
"id": 1,
"name": "customer_name",
"addresses": [
{
"@id": "/addresses/1",
"@type": "Address",
"id": 1,
"name": "foo"
},
{
"@id": "/addresses/2",
"@type": "Address",
"id": 2,
"name": "bar"
}
]
}
}
],
"hydra:totalItems": 1
}
"""


@dropSchema
Scenario: Passing an invalid IRI to a relation
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/relation_embedders" with body:
Expand Down
46 changes: 46 additions & 0 deletions tests/Fixtures/TestBundle/Document/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"address_read"}}}
* )
* @ODM\Document
*/
class Address
{
/**
* @var int
*
* @ODM\Id(strategy="INCREMENT", type="integer")
* @Groups({"address_read"})
*/
private $id;

/**
* @ODM\Field(type="string")
* @Groups({"address_read"})
*/
public $name;

public function getId()
{
return $this->id;
}
}
58 changes: 58 additions & 0 deletions tests/Fixtures/TestBundle/Document/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"customer_read"}}}
* )
* @ODM\Document
*/
class Customer
{
/**
* @var int
*
* @ODM\Id(strategy="INCREMENT", type="integer")
* @Groups({"customer_read"})
*/
private $id;

/**
* @ODM\Field(type="string")
* @Groups({"customer_read"})
*/
public $name;

/**
* @ODM\ReferenceMany(targetDocument=Address::class)
* @Groups({"customer_read"})
*/
public $addresses;

public function __construct()
{
$this->addresses = new ArrayCollection();
}

public function getId()
{
return $this->id;
}
}
55 changes: 55 additions & 0 deletions tests/Fixtures/TestBundle/Document/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"order_read", "customer_read", "address_read"}}},
* forceEager=false
* )
* @ODM\Document
*/
class Order
{
/**
* @var int
*
* @ODM\Id(strategy="INCREMENT", type="integer")
* @Groups({"order_read"})
*/
private $id;

/**
* @ODM\ReferenceOne(targetDocument=Customer::class)
* @Groups({"order_read"})
*/
public $customer;

/**
* @ODM\ReferenceOne(targetDocument=Customer::class)
* @Assert\NotNull
* @Groups({"order_read"})
*/
public $recipient;

public function getId()
{
return $this->id;
}
}
48 changes: 48 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"address_read"}}}
* )
* @ORM\Entity
*/
class Address
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"address_read"})
*/
private $id;

/**
* @ORM\Column(type="string")
* @Groups({"address_read"})
*/
public $name;

public function getId()
{
return $this->id;
}
}
Loading