Skip to content

Commit

Permalink
Discard Classification's Entities are discarded after $om->clear
Browse files Browse the repository at this point in the history
Fix sonata-project/SonataClassificationBundle#60 (sonata-project/SonataClassificationBundle#60)

MediaEventSubscriber caches the root instances of Category.
If the EntityManager is cleared, the instanced are detached
and need to be re-fetched or merged before being persisted again.

I went the easy way and simply discarded the cache.
  • Loading branch information
lemoinem committed May 30, 2016
1 parent 9293008 commit 5de40d5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Listener/ORM/MediaEventSubscriber.php
Expand Up @@ -36,9 +36,15 @@ public function getSubscribedEvents()
Events::postUpdate,
Events::postRemove,
Events::postPersist,
Events::onClear,
);
}

public function onClear()
{
$this->rootCategories = null;
}

/**
* {@inheritdoc}
*/
Expand Down
79 changes: 79 additions & 0 deletions Tests/Listener/ORM/MediaEventSubscriberTest.php
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\Listener\ORM;

use Doctrine\ORM\Events;
use Sonata\MediaBundle\Listener\ORM\MediaEventSubscriber;
use Sonata\MediaBundle\Model\Media;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* @author Mathieu Lemoine <mlemoine@mlemoine.name>
*/
class MediaEventSubscriberTest extends \PHPUnit_Framework_TestCase
{
/**
* @see https://github.com/sonata-project/SonataClassificationBundle/issues/60
* @see https://github.com/sonata-project/SonataMediaBundle/pull/780
*/
public function testRefetchCategoriesAfterClear()
{
$provider = $this->getMock('Sonata\\MediaBundle\\Provider\\MediaProviderInterface');

$pool = $this->getMock('Sonata\\MediaBundle\\Provider\\Pool', array(), array('default'));

$pool->method('getProvider')->will($this->returnValueMap(array(array('provider', $provider))));

$category = $this->getMock('Sonata\\ClassificationBundle\\Model\\CategoryInterface');

$catManager = $this->getMockBuilder('Sonata\\ClassificationBundle\\Entity\\CategoryManager', array('getRootCategories'))
->disableOriginalConstructor()
->getMock();

$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');

$container->method('get')->will($this->returnValueMap(array(
array('sonata.media.pool', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $pool),
array('sonata.classification.manager.category', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $catManager),
)));

$catManager->expects($this->exactly(2))
->method('getRootCategories')
->willReturn(array('context' => $category))
;

$subscriber = new MediaEventSubscriber($container);

$this->assertContains(Events::onClear, $subscriber->getSubscribedEvents());

$media1 = $this->getMock('Sonata\\MediaBundle\\Model\\Media', array('getId', 'getCategory', 'getProvider', 'getContext'));
$media1->method('getProvider')->willReturn('provider');
$media1->method('getContext')->willReturn('context');

$args1 = $this->getMock('Doctrine\\Common\\EventArgs', array('getEntity'));
$args1->method('getEntity')->willReturn($media1);

$subscriber->prePersist($args1);

$subscriber->onClear();

$media2 = $this->getMock('Sonata\\MediaBundle\\Model\\Media', array('getId', 'getCategory', 'getProvider', 'getContext'));
$media2->method('getProvider')->willReturn('provider');
$media2->method('getContext')->willReturn('context');

$args2 = $this->getMock('Doctrine\\Common\\EventArgs', array('getEntity'));
$args2->method('getEntity')->willReturn($media2);

$subscriber->prePersist($args2);
}
}

0 comments on commit 5de40d5

Please sign in to comment.