Skip to content

Commit

Permalink
incorrect unit of work after flush
Browse files Browse the repository at this point in the history
- added unit test to illustrate the problem of proxy classes being
  present in the unit of work after deletion of parent child construction
  usian an association class
  • Loading branch information
wickedOne committed Jan 28, 2024
1 parent ccfc97c commit 7395639
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/Doctrine/Tests/Models/AssociationClass/AssociationClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AssociationClass;

use Doctrine\ORM\Mapping as ORM;

/**
* @author wicliff <[email protected]>
*
* @ORM\Entity()
* @ORM\Table(name="association_class_association")
*/
class AssociationClass
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue()
*/
private $id;

/**
* @var \Doctrine\Tests\Models\AssociationClass\ParentClass
*
* @ORM\ManyToOne(targetEntity="Doctrine\Tests\Models\AssociationClass\ParentClass", inversedBy="associations")
* @ORM\JoinColumn(referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $parent;

/**
* @var \Doctrine\Tests\Models\AssociationClass\ChildClass
*
* @ORM\OneToOne(targetEntity="Doctrine\Tests\Models\AssociationClass\ChildClass")
* @ORM\JoinColumn(referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $child;

public function __construct(ParentClass $parent, ChildClass $child)
{
$this->parent = $parent;
$this->child = $child;
}
}
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/Models/AssociationClass/ChildClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AssociationClass;

use Doctrine\ORM\Mapping as ORM;

/**
* @author wicliff <[email protected]>
*
* @ORM\Entity()
* @ORM\Table(name="association_class_children")
*/
class ChildClass
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue()
*/
private $id;

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
}
53 changes: 53 additions & 0 deletions tests/Doctrine/Tests/Models/AssociationClass/ParentClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AssociationClass;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* @author wicliff <[email protected]>
*
* @ORM\Entity()
* @ORM\Table(name="association_class_parent")
*/
class ParentClass
{
/**
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue()
*
* @var int
*/
private $id;

/**
* @ORM\OneToMany(targetEntity="Doctrine\Tests\Models\AssociationClass\AssociationClass", mappedBy="parent", cascade={"persist"}, orphanRemoval=true)
*
* @var Collection<int, AssociationClass>
*/
private $associations;

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

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

public function addChild(ChildClass $child): self
{
$this->associations->add(new AssociationClass($this, $child));

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\AssociationClass;

Check failure on line 5 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 line after namespace statement, found 2.


use Doctrine\Tests\Models\AssociationClass\AssociationClass;

Check failure on line 8 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 line before first use statement, found 2.
use Doctrine\Tests\Models\AssociationClass\ChildClass;
use Doctrine\Tests\Models\AssociationClass\ParentClass;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @author wicliff <[email protected]>

Check failure on line 14 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Use of annotation @author is forbidden.
*/
class AssociationClassTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
ParentClass::class,
ChildClass::class,
AssociationClass::class
);
}

public function testUnitOfWorkWithRemoveAfterClear(): void
{
$parent = new ParentClass();

Check failure on line 31 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$firstChild = new ChildClass();

Check failure on line 32 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$secondChild = new ChildClass();

$parent
->addChild($firstChild)
->addChild($secondChild)
;

Check failure on line 38 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Space found before semicolon; expected ");" but found ")\n ;"

$this->_em->persist($firstChild);
$this->_em->persist($secondChild);
$this->_em->persist($parent);

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

$parent = $this->_em->find(ParentClass::class, $parent->getId());

Check failure on line 47 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$firstChild = $this->_em->find(ChildClass::class, $firstChild->getId());

Check failure on line 48 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$secondChild = $this->_em->find(ChildClass::class, $secondChild->getId());

$this->_em->remove($firstChild);
$this->_em->remove($secondChild);
$this->_em->remove($parent);

$this->_em->flush();

foreach ($this->_em->getUnitOfWork()->getIdentityMap() as $classString => $object) {
self::assertCount(0, $object, sprintf('%s contains %d objects: %s', $classString, count($object), var_export($object)));

Check failure on line 58 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Function sprintf() should not be referenced via a fallback global name, but via a use statement.

Check failure on line 58 in tests/Doctrine/Tests/ORM/Functional/Ticket/AssociationClass/AssociationClassTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Function count() should not be referenced via a fallback global name, but via a use statement.
}
}
}

0 comments on commit 7395639

Please sign in to comment.