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

Skip to content

Commit d3f8ecc

Browse files
bilouwanalexgurrola
authored andcommitted
Cherry pick unit test from PR doctrine#5570 (Fix PrePersist EventListener when using merge instead of persist)
1 parent ef6a53e commit d3f8ecc

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

tests/Doctrine/Tests/Models/Company/CompanyContractListener.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

55
class CompanyContractListener
66
{
7+
const PRE_PERSIST = 0;
8+
79
public $postPersistCalls;
810
public $prePersistCalls;
9-
11+
1012
public $postUpdateCalls;
1113
public $preUpdateCalls;
12-
14+
1315
public $postRemoveCalls;
1416
public $preRemoveCalls;
1517

1618
public $preFlushCalls;
17-
19+
1820
public $postLoadCalls;
19-
21+
22+
public $snapshots = [];
23+
2024
/**
2125
* @PostPersist
2226
*/
@@ -30,6 +34,7 @@ public function postPersistHandler(CompanyContract $contract)
3034
*/
3135
public function prePersistHandler(CompanyContract $contract)
3236
{
37+
$this->snapshots[self::PRE_PERSIST][] = $this->takeSnapshot($contract);
3338
$this->prePersistCalls[] = func_get_args();
3439
}
3540

@@ -81,4 +86,20 @@ public function postLoadHandler(CompanyContract $contract)
8186
$this->postLoadCalls[] = func_get_args();
8287
}
8388

89+
public function takeSnapshot(CompanyContract $contract)
90+
{
91+
$snapshot = [];
92+
$reflexion = new \ReflectionClass($contract);
93+
foreach ($reflexion->getProperties() as $property) {
94+
$property->setAccessible(true);
95+
$value = $property->getValue($contract);
96+
if (is_object($value) || is_array($value)) {
97+
continue;
98+
}
99+
$snapshot[$property->getName()] = $property->getValue($contract);
100+
}
101+
102+
return $snapshot;
103+
}
104+
84105
}

tests/Doctrine/Tests/ORM/Functional/EntityListenersOnMergeTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,39 @@
22

33
namespace Doctrine\Tests\ORM\Functional;
44

5+
use Doctrine\Tests\Models\Company\CompanyContractListener;
6+
use Doctrine\Tests\Models\Company\CompanyFixContract;
57
use Doctrine\Tests\Models\DDC3597\DDC3597Image;
68
use Doctrine\Tests\Models\DDC3597\DDC3597Media;
79
use Doctrine\Tests\Models\DDC3597\DDC3597Root;
810

911
/**
12+
* @group DDC-1955
1013
*/
1114
class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
1215
{
16+
17+
/**
18+
* @var \Doctrine\Tests\Models\Company\CompanyContractListener
19+
*/
20+
private $listener;
21+
1322
protected function setUp()
1423
{
24+
$this->useModelSet('company');
1525
parent::setUp();
26+
1627
$this->_schemaTool->createSchema(
1728
[
1829
$this->_em->getClassMetadata(DDC3597Root::class),
1930
$this->_em->getClassMetadata(DDC3597Media::class),
2031
$this->_em->getClassMetadata(DDC3597Image::class),
2132
]
2233
);
34+
35+
$this->listener = $this->_em->getConfiguration()
36+
->getEntityListenerResolver()
37+
->resolve('Doctrine\Tests\Models\Company\CompanyContractListener');
2338
}
2439

2540
protected function tearDown()
@@ -47,4 +62,35 @@ public function testMergeNewEntityLifecyleEventsModificationsShouldBeKept()
4762
$this->assertNotNull($imageEntity->getCreatedAt());
4863
$this->assertNotNull($imageEntity->getUpdatedAt());
4964
}
65+
66+
public function testPrePersistListeners()
67+
{
68+
$fix = new CompanyFixContract();
69+
$fix->setFixPrice(2000);
70+
71+
$this->listener->prePersistCalls = [];
72+
73+
$fix = $this->_em->merge($fix);
74+
$this->_em->flush();
75+
76+
$this->assertCount(1, $this->listener->prePersistCalls);
77+
78+
$this->assertSame($fix, $this->listener->prePersistCalls[0][0]);
79+
80+
$this->assertInstanceOf(
81+
'Doctrine\Tests\Models\Company\CompanyFixContract',
82+
$this->listener->prePersistCalls[0][0]
83+
);
84+
85+
$this->assertInstanceOf(
86+
'Doctrine\ORM\Event\LifecycleEventArgs',
87+
$this->listener->prePersistCalls[0][1]
88+
);
89+
90+
$this->assertArrayHasKey('fixPrice', $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]);
91+
$this->assertEquals(
92+
$fix->getFixPrice(),
93+
$this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]['fixPrice']
94+
);
95+
}
5096
}

0 commit comments

Comments
 (0)