Skip to content

Commit

Permalink
Added testcase as I actually experience this behavior, adding ramsey …
Browse files Browse the repository at this point in the history
…third party requirements to test this ofcourse isn't ideal, could use some help with another simple route to validate this.
  • Loading branch information
wtfzdotnet committed Jun 3, 2023
1 parent fd2a834 commit 00de8f5
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"phpstan/phpstan": "~1.4.10 || 1.10.14",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.6",
"psr/log": "^1 || ^2 || ^3",
"ramsey/uuid": "^4.7",
"ramsey/uuid-doctrine": "^2.0",
"squizlabs/php_codesniffer": "3.7.2",
"symfony/cache": "^4.4 || ^5.4 || ^6.0",
"symfony/var-exporter": "^4.4 || ^5.4 || ^6.2",
Expand Down
156 changes: 156 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/GH10747Test.php
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;
}
}

0 comments on commit 00de8f5

Please sign in to comment.