-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added testcase as I actually experience this behavior, adding ramsey …
…third party requirements to test this ofcourse isn't ideal, could use some help with another simple route to validate this.
- Loading branch information
1 parent
fd2a834
commit 00de8f5
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\DBAL\Types\Type as DBALType; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Table; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
use Ramsey\Uuid\Doctrine\UuidBinaryType; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
/** | ||
* Functional tests for asserting that orphaned children in a OneToMany relationship get removed with a custom identifier | ||
* | ||
* @group GH10747 | ||
*/ | ||
final class GH10747Test extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (! DBALType::hasType(UuidBinaryType::class)) { | ||
DBALType::addType(UuidBinaryType::class, UuidBinaryType::class); | ||
} | ||
|
||
$this->setUpEntitySchema([GH10747Article::class, GH10747Credit::class]); | ||
} | ||
|
||
public function testOrphanedOneToManyDeletesCollection(): void | ||
{ | ||
$object = new GH10747Article( | ||
Uuid::uuid4() | ||
); | ||
|
||
$creditOne = new GH10747Credit( | ||
Uuid::uuid4(), | ||
$object, | ||
'credit1' | ||
); | ||
|
||
$creditTwo = new GH10747Credit( | ||
Uuid::uuid4(), | ||
$object, | ||
'credit2' | ||
); | ||
|
||
$object->setCredits(new ArrayCollection([$creditOne, $creditTwo])); | ||
|
||
$this->_em->persist($object); | ||
$this->_em->persist($creditOne); | ||
$this->_em->persist($creditTwo); | ||
$this->_em->flush(); | ||
|
||
$id = $object->id; | ||
|
||
$object2 = $this->_em->find(GH10747Article::class, $id); | ||
|
||
$creditThree = new GH10747Credit( | ||
Uuid::uuid4(), | ||
$object2, | ||
'credit3' | ||
); | ||
|
||
$object2->setCredits(new ArrayCollection([$creditThree])); | ||
|
||
$this->_em->persist($object2); | ||
$this->_em->persist($creditThree); | ||
$this->_em->flush(); | ||
|
||
$currentDatabaseCredits = $this->_em->createQueryBuilder() | ||
->select('c.id') | ||
->from(GH10747Credit::class, 'c') | ||
->getQuery() | ||
->execute(); | ||
|
||
self::assertCount(1, $currentDatabaseCredits); | ||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
* @Table | ||
*/ | ||
class GH10747Article | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="Ramsey\Uuid\Doctrine\UuidBinaryType") | ||
* @var UuidInterface | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\OneToMany(targetEntity="GH10747Credit", mappedBy="article", orphanRemoval=true) | ||
* | ||
* @var Collection | ||
*/ | ||
public $credits; | ||
|
||
public function __construct(UuidInterface $id) | ||
{ | ||
$this->id = $id; | ||
$this->credits = new ArrayCollection(); | ||
} | ||
|
||
public function setCredits(Collection $credits): void | ||
{ | ||
$this->credits = $credits; | ||
} | ||
|
||
/** @return Collection<int, GH10747Credit> */ | ||
public function getCredits(): Collection | ||
{ | ||
return $this->credits; | ||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
* @Table | ||
*/ | ||
class GH10747Credit | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="Ramsey\Uuid\Doctrine\UuidBinaryType") | ||
* @var UuidInterface | ||
*/ | ||
public $id; | ||
|
||
/** @var string */ | ||
public $name; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity="GH10747Article", inversedBy="credits") | ||
* | ||
* @var GH10747Article | ||
*/ | ||
public $article; | ||
|
||
public function __construct(UuidInterface $id, GH10747Article $article, string $name) | ||
{ | ||
$this->id = $id; | ||
$this->article = $article; | ||
$this->name = $name; | ||
} | ||
} |