Skip to content
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

Test case for GH-6776: unique constraint violation on OneToOne relationship #7450

Open
wants to merge 3 commits into
base: 2.6
Choose a base branch
from
Open
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
120 changes: 120 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH6776Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group 6776
*/
class GH6776Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp() : void
{
parent::setUp();

$this->setUpEntitySchema([GH6776WheelDimension::class, GH6776Vehicle::class]);
}

/**
* Verifies that removing and then inserting an element in a collection using a unique constraint does not
* make this constraint fail.
*/
public function testIssue() : void
{
// create some vehicle with dimensions
$vehicle = new GH6776Vehicle();
$vehicle->name = 'SuperCar';

$dimension1 = new GH6776WheelDimension();
$dimension1->width = 6;
$dimension1->diameter = 17;
$dimension1->offset = 32;
$dimension1->tirePressure = 2.75;
$dimension1->vehicle = $vehicle;

$dimension1bis = clone $dimension1;
$dimension1bis->tirePressure = 3.10;

$dimension2 = new GH6776WheelDimension();
$dimension2->type = 'rear';
$dimension2->width = 6;
$dimension2->diameter = 17;
$dimension2->offset = 39;
$dimension2->tirePressure = 2.75;
$dimension2->vehicle = $vehicle;

$vehicle->compatibleDimensions = new ArrayCollection();
$vehicle->compatibleDimensions->add($dimension1);
$vehicle->compatibleDimensions->add($dimension2);

// persist and flush vehicle and the 2 original dimensions
$this->_em->persist($vehicle);
$this->_em->persist($dimension1);
$this->_em->persist($dimension2);
$this->_em->flush();

self::assertCount(2, $vehicle->compatibleDimensions);

// remove dimension1 and add its clone; when flushing it should crash because of unique constraint violation
$vehicle->compatibleDimensions->removeElement($dimension1);
$this->_em->remove($dimension1);

self::assertCount(1, $vehicle->compatibleDimensions);

$this->_em->persist($dimension1bis);
$vehicle->compatibleDimensions->add($dimension1bis);

$this->_em->flush();

// still the same count
self::assertCount(2, $vehicle->compatibleDimensions);
}
}

/**
* @Entity
*/
class GH6776Vehicle
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;

/** @Column(type="string") */
public $name;

/** @OneToMany(targetEntity="GH6776WheelDimension", mappedBy="vehicle") */
public $compatibleDimensions;
}

/**
* @Entity
* @Table(uniqueConstraints={@UniqueConstraint(name="wheel_size", columns={"offset", "width", "diameter"})})
*/
class GH6776WheelDimension
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;

/** @ManyToOne(targetEntity="GH6776Vehicle", inversedBy="compatibleDimensions") */
public $vehicle;

/** @Column(type="integer") */
public $offset;

/** @Column(type="integer") */
public $width;

/** @Column(type="integer") */
public $diameter;

/** @Column(type="float") */
public $tirePressure;

/** @Column(type="string") */
public $type = 'front';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Doctrine\Tests\Functional\Ticket;

use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group 6776
*/
class GH6776Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([GH6776Cost::class, GH6776Vehicle::class]);
}


/**
* Verifies that removing and then inserting an element in a collection using a unique constraint does not
* make this constraint fail.
*/
public function testIssue(): void
{
// create some vehicle with dimensions
$vehicle = new GH6776Vehicle();
$vehicle->name = 'SuperCar';

$cost1 = new GH6776Cost();
$cost1->currency = 'GBP';
$cost1->amount = 12000000;
$cost1->vehicle = $vehicle;

$vehicle->cost = $cost1;

// persist and flush vehicle and the 2 original dimensions
$this->_em->persist($vehicle);
$this->_em->persist($cost1);
$this->_em->flush();

self::assertEquals($cost1, $vehicle->cost);

// remove cost1 and add its clone; when flushing it should crash because of unique constraint violation
$vehicle->cost = null;
$this->_em->remove($cost1);

self::assertEquals(null, $vehicle->cost);

$cost2 = clone $cost1;
$cost2->amount = 15000000;

$this->_em->persist($cost2);
$vehicle->cost = $cost2;

$this->_em->flush();

// still the same count
self::assertEquals($cost2, $vehicle->cost);
}
}

/**
* @Entity
*/
class GH6776Vehicle
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;

/** @Column(type="string") */
public $name;

/** @OneToOne(targetEntity="GH6776Cost", mappedBy="vehicle") */
public $cost;
}

/**
* @Entity()
* @Table(uniqueConstraints={@UniqueConstraint(name="cost", columns={"vehicle_id"})})
*/
class GH6776Cost
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;

/** @OneToOne(targetEntity="GH6776Vehicle", inversedBy="code") */
public $vehicle;

/** @Column(type="string", length=3) */
public $currency;

/** @Column(type="integer") */
public $amount;
}