Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c25889

Browse files
committed
Rewrite test to use EntityManager instead of UoW directly
1 parent fa3916c commit 1c25889

5 files changed

Lines changed: 158 additions & 69 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Issue9300;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Doctrine\Common\Collections\Collection;
9+
use Doctrine\ORM\Mapping\Column;
10+
use Doctrine\ORM\Mapping\Entity;
11+
use Doctrine\ORM\Mapping\GeneratedValue;
12+
use Doctrine\ORM\Mapping\Id;
13+
use Doctrine\ORM\Mapping\ManyToMany;
14+
15+
/**
16+
* @Entity
17+
*/
18+
class Issue9300Child
19+
{
20+
/**
21+
* @var int
22+
* @Id
23+
* @Column(type="integer")
24+
* @GeneratedValue
25+
*/
26+
public $id;
27+
28+
/**
29+
* @var Collection|Issue9300Parent
30+
* @ManyToMany(targetEntity="Issue9300Parent")
31+
*/
32+
public $parents;
33+
34+
/**
35+
* @var string
36+
* @Column(type="string")
37+
*/
38+
public $name;
39+
40+
public function __construct()
41+
{
42+
$this->parents = new ArrayCollection();
43+
}
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Issue9300;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
12+
/**
13+
* @Entity
14+
*/
15+
class Issue9300Parent
16+
{
17+
18+
/**
19+
* @var int
20+
* @Id
21+
* @Column(type="integer")
22+
* @GeneratedValue
23+
*/
24+
public $id;
25+
26+
/**
27+
* @var string
28+
* @Column(type="string")
29+
*/
30+
public $name;
31+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\Ticket;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Doctrine\Tests\Models\Issue9300\Issue9300Child;
9+
use Doctrine\Tests\Models\Issue9300\Issue9300Parent;
10+
use Doctrine\Tests\OrmFunctionalTestCase;
11+
12+
/**
13+
* @group 9300
14+
*/
15+
class Issue9300Test extends OrmFunctionalTestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
$this->useModelSet('issue9300');
20+
parent::setUp();
21+
}
22+
23+
/**
24+
* @group #9300
25+
*/
26+
public function testPersistedCollectionIsPresentInOriginalDataAfterFlush(): void
27+
{
28+
$parent = new Issue9300Parent();
29+
$child = new Issue9300Child();
30+
$child->parents->add($parent);
31+
32+
$parent->name = 'abc';
33+
$child->name = 'abc';
34+
35+
$this->_em->persist($parent);
36+
$this->_em->persist($child);
37+
$this->_em->flush();
38+
39+
$parent->name = 'abcd';
40+
$child->name = 'abcd';
41+
42+
$this->_em->flush();
43+
44+
self::assertArrayHasKey('parents', $this->_em->getUnitOfWork()->getOriginalEntityData($child));
45+
}
46+
47+
/**
48+
* @group #9300
49+
*/
50+
public function testPersistingCollectionAfterFlushWorksAsExpected(): void
51+
{
52+
$parentOne = new Issue9300Parent();
53+
$parentTwo = new Issue9300Parent();
54+
$childOne = new Issue9300Child();
55+
56+
$parentOne->name = 'abc';
57+
$parentTwo->name = 'abc';
58+
$childOne->name = 'abc';
59+
$childOne->parents = new ArrayCollection([$parentOne]);
60+
61+
$this->_em->persist($parentOne);
62+
$this->_em->persist($parentTwo);
63+
$this->_em->persist($childOne);
64+
$this->_em->flush();
65+
66+
// Recalculate change-set -> new original data
67+
$childOne->name = 'abcd';
68+
$this->_em->flush();
69+
70+
$childOne->parents = new ArrayCollection([$parentTwo]);
71+
72+
$this->_em->flush();
73+
$this->_em->clear();
74+
75+
$childOneFresh = $this->_em->find(Issue9300Child::class, $childOne->id);
76+
self::assertCount(1, $childOneFresh->parents);
77+
self::assertEquals($parentTwo->id, $childOneFresh->parents[0]->id);
78+
}
79+
}

tests/Doctrine/Tests/ORM/UnitOfWorkTest.php

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -828,75 +828,6 @@ public function testItThrowsWhenLookingUpIdentifierForUnknownEntity(): void
828828
$this->expectException(EntityNotFoundException::class);
829829
$this->_unitOfWork->getEntityIdentifier(new stdClass());
830830
}
831-
832-
/**
833-
* @group #9300
834-
*/
835-
public function testPersistedCollectionIsPresentInOriginalDataAfterComputeChangeset(): void
836-
{
837-
$entity = new EntityWithCollection();
838-
$entity->children->add(new ChildEntity());
839-
$entity->name = 'abc';
840-
841-
$this->_unitOfWork->persist($entity);
842-
$this->_unitOfWork->commit();
843-
844-
$entity->name = 'abcd';
845-
$this->_unitOfWork->computeChangeSets();
846-
847-
self::assertArrayHasKey('children', $this->_unitOfWork->getOriginalEntityData($entity));
848-
}
849-
}
850-
851-
/**
852-
* @Entity
853-
*/
854-
class EntityWithCollection
855-
{
856-
/**
857-
* @var Collection|ChildEntity[]
858-
* @OneToMany(targetEntity="ChildEntity", mappedBy="parent", cascade={"persist"})
859-
*/
860-
public $children;
861-
862-
/**
863-
* @var int
864-
* @Id
865-
* @Column(type="integer")
866-
* @GeneratedValue
867-
*/
868-
public $id;
869-
870-
/**
871-
* @var string
872-
* @Column(type="string")
873-
*/
874-
public $name;
875-
876-
public function __construct()
877-
{
878-
$this->children = new ArrayCollection();
879-
}
880-
}
881-
882-
/**
883-
* @Entity
884-
*/
885-
class ChildEntity
886-
{
887-
/**
888-
* @var int
889-
* @Id
890-
* @Column(type="integer")
891-
* @GeneratedValue
892-
*/
893-
public $id;
894-
895-
/**
896-
* @var int
897-
* @ManyToOne(targetEntity="EntityWithCollection", inversedBy="children")
898-
*/
899-
public $parent;
900831
}
901832

902833
/**

tests/Doctrine/Tests/OrmFunctionalTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
335335
Models\Issue5989\Issue5989Employee::class,
336336
Models\Issue5989\Issue5989Manager::class,
337337
],
338+
'issue9300' => [
339+
Models\Issue9300\Issue9300Child::class,
340+
Models\Issue9300\Issue9300Parent::class,
341+
],
338342
];
339343

340344
protected function useModelSet(string $setName): void

0 commit comments

Comments
 (0)