Skip to content

Commit

Permalink
Merge pull request #12 from peter-gribanov/doctrine_2.7
Browse files Browse the repository at this point in the history
Compatible with Doctrine ORM >= 2.7
  • Loading branch information
peter-gribanov authored May 24, 2021
2 parents 619cabb + 0dcc771 commit 056e9e8
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/http-kernel": "~2.3|~3.0|~4.0|~5.0",
"symfony/dependency-injection": "~2.3|~3.0|~4.0|~5.0",
"symfony/expression-language": "~2.3|~3.0|~4.0|~5.0",
"doctrine/orm": "~2.4|~2.5|~2.6"
"doctrine/orm": "~2.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8.36"
Expand Down
13 changes: 11 additions & 2 deletions src/Service/EventPuller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace GpsLab\Bundle\DomainEvent\Service;

use Doctrine\Common\Persistence\Proxy;
use Doctrine\Common\Persistence\Proxy as CommonProxy;
use Doctrine\Persistence\Proxy;
use Doctrine\ORM\UnitOfWork;
use GpsLab\Domain\Event\Aggregator\AggregateEvents;
use GpsLab\Domain\Event\Event;
Expand Down Expand Up @@ -45,10 +46,18 @@ public function pull(UnitOfWork $uow)
private function pullFromEntities(array $entities)
{
$events = [];

foreach ($entities as $entity) {
// ignore Doctrine not initialized proxy classes
// proxy class can't have a domain events
if ((!($entity instanceof Proxy) || $entity->__isInitialized()) && $entity instanceof AggregateEvents) {
if (
($entity instanceof Proxy && !$entity->__isInitialized()) ||
($entity instanceof CommonProxy && !$entity->__isInitialized())
) {
continue;
}

if ($entity instanceof AggregateEvents) {
$events = array_merge($events, $entity->pullEvents());
}
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Fixtures/SimpleObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* GpsLab component.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/MIT
*/

namespace GpsLab\Bundle\DomainEvent\Tests\Fixtures;

class SimpleObject
{
/**
* @var string
*/
private $foo;

/**
* @var string
*/
protected $camelCase = 'boo';

/**
* @return string
*/
public function getFoo()
{
return $this->foo;
}

/**
* @param string $foo
*/
public function setFoo($foo)
{
$this->foo = $foo;
}

/**
* @return string
*/
public function getCamelCase()
{
return $this->camelCase;
}

/**
* @param string $camelCase
*/
public function setCamelCase($camelCase)
{
$this->camelCase = $camelCase;
}
}
64 changes: 64 additions & 0 deletions tests/Fixtures/SimpleObjectProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* GpsLab component.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/MIT
*/

namespace GpsLab\Bundle\DomainEvent\Tests\Fixtures;

use Doctrine\Common\Persistence\Proxy as CommonProxy;
use Doctrine\Persistence\Proxy;

if (interface_exists(CommonProxy::class)) {
class SimpleObjectProxy extends SimpleObject implements CommonProxy
{
/**
* @var bool
*/
public $__isInitialized__ = false;

public function __load()
{
if (!$this->__isInitialized__) {
$this->camelCase = 'proxy-boo';
$this->__isInitialized__ = true;
}
}

/**
* @return bool
*/
public function __isInitialized()
{
return $this->__isInitialized__;
}
}
} else {
class SimpleObjectProxy extends SimpleObject implements Proxy
{
/**
* @var bool
*/
public $__isInitialized__ = false;

public function __load()
{
if (!$this->__isInitialized__) {
$this->camelCase = 'proxy-boo';
$this->__isInitialized__ = true;
}
}

/**
* @return bool
*/
public function __isInitialized()
{
return $this->__isInitialized__;
}
}
}
15 changes: 8 additions & 7 deletions tests/Service/EventPullerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

namespace GpsLab\Bundle\DomainEvent\Tests\Service;

use Doctrine\Common\Persistence\Proxy;
use Doctrine\ORM\UnitOfWork;
use GpsLab\Bundle\DomainEvent\Service\EventPuller;
use GpsLab\Bundle\DomainEvent\Tests\Fixtures\SimpleObject;
use GpsLab\Bundle\DomainEvent\Tests\Fixtures\SimpleObjectProxy;
use GpsLab\Domain\Event\Aggregator\AggregateEvents;
use GpsLab\Domain\Event\Event;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -115,16 +116,16 @@ public function testPull(

$map = [
[
$this->getMockBuilder(Proxy::class)->getMock(),
$this->getMockBuilder(SimpleObjectProxy::class)->getMock(),
$aggregator1,
],
[
$aggregator2,
new \stdClass(),
new SimpleObject(),
],
[
new \stdClass(),
$this->getMockBuilder(Proxy::class)->getMock(),
new SimpleObject(),
$this->getMockBuilder(SimpleObjectProxy::class)->getMock(),
],
];
} else {
Expand Down Expand Up @@ -188,8 +189,8 @@ private function getEntitiesFroEvents(array $events)
;

return [
$this->getMockBuilder(Proxy::class)->getMock(),
new \stdClass(),
$this->getMockBuilder(SimpleObjectProxy::class)->getMock(),
new SimpleObject(),
$aggregator1,
$aggregator2,
];
Expand Down

0 comments on commit 056e9e8

Please sign in to comment.