1
0
mirror of synced 2025-02-02 13:31:45 +03:00

Cherry pick unit test from PR #5570 (Fix PrePersist EventListener when using merge instead of persist)

This commit is contained in:
bilouwan 2016-12-15 13:03:53 +01:00 committed by Marco Pivetta
parent 25efabdb74
commit 295523cdca
2 changed files with 71 additions and 4 deletions

View File

@ -4,19 +4,23 @@ namespace Doctrine\Tests\Models\Company;
class CompanyContractListener
{
const PRE_PERSIST = 0;
public $postPersistCalls;
public $prePersistCalls;
public $postUpdateCalls;
public $preUpdateCalls;
public $postRemoveCalls;
public $preRemoveCalls;
public $preFlushCalls;
public $postLoadCalls;
public $snapshots = [];
/**
* @PostPersist
*/
@ -30,6 +34,7 @@ class CompanyContractListener
*/
public function prePersistHandler(CompanyContract $contract)
{
$this->snapshots[self::PRE_PERSIST][] = $this->takeSnapshot($contract);
$this->prePersistCalls[] = func_get_args();
}
@ -81,4 +86,20 @@ class CompanyContractListener
$this->postLoadCalls[] = func_get_args();
}
public function takeSnapshot(CompanyContract $contract)
{
$snapshot = [];
$reflexion = new \ReflectionClass($contract);
foreach ($reflexion->getProperties() as $property) {
$property->setAccessible(true);
$value = $property->getValue($contract);
if (is_object($value) || is_array($value)) {
continue;
}
$snapshot[$property->getName()] = $property->getValue($contract);
}
return $snapshot;
}
}

View File

@ -2,17 +2,28 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\Company\CompanyContractListener;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\DDC3597\DDC3597Image;
use Doctrine\Tests\Models\DDC3597\DDC3597Media;
use Doctrine\Tests\Models\DDC3597\DDC3597Root;
/**
* @group DDC-1955
*/
class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
/**
* @var \Doctrine\Tests\Models\Company\CompanyContractListener
*/
private $listener;
protected function setUp()
{
$this->useModelSet('company');
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC3597Root::class),
@ -20,6 +31,10 @@ class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->getClassMetadata(DDC3597Image::class),
]
);
$this->listener = $this->_em->getConfiguration()
->getEntityListenerResolver()
->resolve('Doctrine\Tests\Models\Company\CompanyContractListener');
}
protected function tearDown()
@ -47,4 +62,35 @@ class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertNotNull($imageEntity->getCreatedAt());
$this->assertNotNull($imageEntity->getUpdatedAt());
}
public function testPrePersistListeners()
{
$fix = new CompanyFixContract();
$fix->setFixPrice(2000);
$this->listener->prePersistCalls = [];
$fix = $this->_em->merge($fix);
$this->_em->flush();
$this->assertCount(1, $this->listener->prePersistCalls);
$this->assertSame($fix, $this->listener->prePersistCalls[0][0]);
$this->assertInstanceOf(
'Doctrine\Tests\Models\Company\CompanyFixContract',
$this->listener->prePersistCalls[0][0]
);
$this->assertInstanceOf(
'Doctrine\ORM\Event\LifecycleEventArgs',
$this->listener->prePersistCalls[0][1]
);
$this->assertArrayHasKey('fixPrice', $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]);
$this->assertEquals(
$fix->getFixPrice(),
$this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]['fixPrice']
);
}
}