Skip to content

Commit

Permalink
Add tests with the examples given in the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jan 3, 2024
1 parent b95529d commit 84a76a9
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH10913Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Internal\TopologicalSort\CycleDetectedException;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

use function array_filter;
use function array_values;
use function strpos;

class GH10913Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

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

public function testExample1(): void
{
[$a, $b, $c] = $this->createEntities(3);

$c->ref = $b;
$b->odc = $a;

$this->_em->persist($a);
$this->_em->persist($b);
$this->_em->persist($c);
$this->_em->flush();

$this->_em->remove($a);
$this->_em->remove($b);
$this->_em->remove($c);

$this->flushAndAssertNumberOfDeleteQueries(3);
}

public function testExample2(): void
{
[$a, $b, $c] = $this->createEntities(3);

$a->odc = $b;
$b->odc = $a;
$c->ref = $b;

$this->_em->persist($a);
$this->_em->persist($b);
$this->_em->persist($c);
$this->_em->flush();

$this->_em->remove($a);
$this->_em->remove($b);
$this->_em->remove($c);

$this->flushAndAssertNumberOfDeleteQueries(3);
}

public function testExample3(): void
{
[$a, $b, $c] = $this->createEntities(3);

$a->odc = $b;
$a->ref = $c;
$c->ref = $b;
$b->odc = $a;

$this->_em->persist($a);
$this->_em->persist($b);
$this->_em->persist($c);
$this->_em->flush();

$this->_em->remove($a);
$this->_em->remove($b);
$this->_em->remove($c);

self::expectException(CycleDetectedException::class);

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

public function testExample4(): void
{
[$a, $b, $c, $d] = $this->createEntities(4);

$a->ref = $b;
$b->odc = $c;
$c->odc = $b;
$d->ref = $c;

$this->_em->persist($a);
$this->_em->persist($b);
$this->_em->persist($c);
$this->_em->persist($d);
$this->_em->flush();

$this->_em->remove($b);
$this->_em->remove($c);
$this->_em->remove($d);
$this->_em->remove($a);

$this->flushAndAssertNumberOfDeleteQueries(4);
}

private function flushAndAssertNumberOfDeleteQueries(int $expectedCount): void
{
$queryLog = $this->getQueryLog();
$queryLog->reset()->enable();

$this->_em->flush();

$queries = array_values(array_filter($queryLog->queries, static function ($entry) {
return strpos($entry['sql'], 'DELETE') === 0;
}));

self::assertCount($expectedCount, $queries);
}

/**
* @return list<GH10913Entity>
*/
private function createEntities(int $count = 1): array
{
$result = [];

for ($i = 0; $i < $count; $i++) {
$result[] = new GH10913Entity();
}

return $result;
}
}

/** @ORM\Entity */
class GH10913Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity=GH10913Entity::class)
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
*
* @var GH10913Entity
*/
public $odc;

/**
* @ORM\ManyToOne(targetEntity=GH10913Entity::class)
* @ORM\JoinColumn(nullable=true)
*
* @var GH10913Entity
*/
public $ref;
}

0 comments on commit 84a76a9

Please sign in to comment.