Skip to content

Commit

Permalink
Test 0-index bug with ManyToMany relations
Browse files Browse the repository at this point in the history
  • Loading branch information
abluchet committed Dec 1, 2017
1 parent a30ac33 commit 2d1288d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
19 changes: 18 additions & 1 deletion features/bootstrap/FeatureContext.php
Expand Up @@ -12,6 +12,7 @@
declare(strict_types=1);

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Brand;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
Expand Down Expand Up @@ -594,7 +595,7 @@ public function thereIsAFileConfigDummyObject()
/**
* @Given there is a DummyCar entity with related colors
*/
public function thereIsAFooEntityWithRelatedBars()
public function thereIsADummyCarEntityWithRelatedColors()
{
$foo = new DummyCar();
$this->manager->persist($foo);
Expand Down Expand Up @@ -766,4 +767,20 @@ public function thereIsDummyDateObjectsWithDummyDate(int $nb)

$this->manager->flush();
}

/**
* @Given there is a brand and :nb DummyCar
*/
public function thereAreSomeBrands(int $nb)
{
$brand = new Brand();
$this->manager->persist($brand);

for ($i = 1; $i <= $nb; ++$i) {
$foo = new DummyCar();
$this->manager->persist($foo);
}

$this->manager->flush();
}
}
75 changes: 75 additions & 0 deletions features/main/many_to_many.feature
@@ -0,0 +1,75 @@
Feature: Add/remove on a ManytoMany relation
In order to use a hypermedia API
As a client software developer
I need to be able to update ManytoMany relations between resources

@createSchema
Scenario: Add a DummyCar to the Brand
Given there is a brand and 5 DummyCar
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/brands/1" with body:
"""
{"car": ["/dummy_cars/1"]}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Brand",
"@id": "/brands/1",
"@type": "Brand",
"car": [
"/dummy_cars/1"
],
"id": 1
}
"""

Scenario: Add another DummyCar to the Brand
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/brands/1" with body:
"""
{"car": ["/dummy_cars/1", "/dummy_cars/2"]}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Brand",
"@id": "/brands/1",
"@type": "Brand",
"car": [
"/dummy_cars/1",
"/dummy_cars/2"
],
"id": 1
}
"""

@dropSchema
Scenario: Remove the first car of the relation
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/brands/1" with body:
"""
{"car": ["/dummy_cars/2"]}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Brand",
"@id": "/brands/1",
"@type": "Brand",
"car": [
"/dummy_cars/2"
],
"id": 1
}
"""

67 changes: 67 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Brand.php
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ApiResource
* @ORM\Entity
*/
class Brand
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\ManyToMany(targetEntity="ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar", inversedBy="brand")
* @ORM\JoinTable(
* name="CarToBrand",
* joinColumns={@ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="car_id", referencedColumnName="id", nullable=false)}
* )
*/
private $car;

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

public function addCar(DummyCar $car)
{
$this->car[] = $car;
}

public function removeCar(DummyCar $car)
{
$this->car->removeElement($car);
}

public function getCar()
{
return $this->car->getValues();
}

public function getId()
{
return $this->id;
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DummyCar.php
Expand Up @@ -47,6 +47,11 @@ class DummyCar
*/
private $colors;

/**
* @ORM\ManyToMany(targetEntity="ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Brand", mappedBy="car")
*/
public $brand;

public function __construct()
{
$this->colors = new ArrayCollection();
Expand Down

0 comments on commit 2d1288d

Please sign in to comment.