diff --git a/DependencyInjection/Compiler/RegisterPropelModelsPass.php b/DependencyInjection/Compiler/RegisterPropelModelsPass.php index 7dc95f37..43e96fd0 100644 --- a/DependencyInjection/Compiler/RegisterPropelModelsPass.php +++ b/DependencyInjection/Compiler/RegisterPropelModelsPass.php @@ -5,12 +5,14 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\DefinitionDecorator; use Vich\UploaderBundle\Exception\MappingNotFoundException; /** * Register the uploadable models in BazingaPropelEventDispatcherBundle * * @author Kévin Gomez + * @author Konstantin Myakshin */ class RegisterPropelModelsPass implements CompilerPassInterface { @@ -23,9 +25,7 @@ public function process(ContainerBuilder $container) return; } - $serviceTypes = array( - 'inject', 'clean', 'remove', 'upload', - ); + $serviceTypes = array('inject', 'clean', 'remove', 'upload',); $metadata = $container->get('vich_uploader.metadata_reader'); $mappings = $container->getParameter('vich_uploader.mappings'); @@ -43,11 +43,12 @@ public function process(ContainerBuilder $container) } foreach ($serviceTypes as $type) { - if (!$container->has(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']))) { - continue; - } + $listenerId = sprintf('vich_uploader.listener.%s.propel.%s', $type, $field['mapping']); + + $definition = $container + ->setDefinition($listenerId, new DefinitionDecorator(sprintf('vich_uploader.listener.%s.propel', $type))) + ->replaceArgument(1, new Reference('vich_uploader.adapter.propel')); - $definition = $container->getDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping'])); $definition->setClass($container->getDefinition($definition->getParent())->getClass()); $definition->setPublic(true); $definition->addTag('propel.event_subscriber', array('class' => $class)); diff --git a/DependencyInjection/VichUploaderExtension.php b/DependencyInjection/VichUploaderExtension.php index 9eb6502d..fc21ead6 100644 --- a/DependencyInjection/VichUploaderExtension.php +++ b/DependencyInjection/VichUploaderExtension.php @@ -24,7 +24,7 @@ class VichUploaderExtension extends Extension protected $tagMap = array( 'orm' => 'doctrine.event_subscriber', 'mongodb' => 'doctrine_mongodb.odm.event_subscriber', - 'phpcr' => 'doctrine_phpcr.event_subscriber' + 'phpcr' => 'doctrine_phpcr.event_subscriber', ); /** @@ -153,26 +153,24 @@ protected function fixDbDriverConfig(array $config) protected function registerListeners(ContainerBuilder $container, array $config) { - $servicesMap = array( - 'inject_on_load' => array('name' => 'inject', 'priority' => 0), - 'delete_on_update' => array('name' => 'clean', 'priority' => 50), - 'delete_on_remove' => array('name' => 'remove', 'priority' => 0) + $dbDrivers = array(); + foreach ($config['mappings'] as $mapping) { + $dbDrivers[$mapping['db_driver']] = true; + } + // register propel via compiler pass + unset($dbDrivers['propel']); + + $serviceTypes = array( + 'upload' => 0, + 'inject' => 0, + 'clean' => 50, + 'remove' => 0, ); - foreach ($config['mappings'] as $name => $mapping) { - $driver = $mapping['db_driver']; - - // create optionnal listeners - foreach ($servicesMap as $configOption => $service) { - if (!$mapping[$configOption]) { - continue; - } - - $this->createListener($container, $name, $service['name'], $driver, $service['priority']); + foreach (array_keys($dbDrivers) as $driver) { + foreach ($serviceTypes as $serviceType => $priority) { + $this->createListener($container, $serviceType, $driver, $priority); } - - // the upload listener is mandatory - $this->createListener($container, $name, 'upload', $driver); } } @@ -199,16 +197,12 @@ protected function createNamerService(ContainerBuilder $container, $mappingName, return $mapping; } - protected function createListener(ContainerBuilder $container, $name, $type, $driver, $priority = 0) + protected function createListener(ContainerBuilder $container, $type, $driver, $priority = 0) { $definition = $container - ->setDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $name), new DefinitionDecorator(sprintf('vich_uploader.listener.%s.%s', $type, $driver))) - ->replaceArgument(0, $name) + ->getDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $driver)) ->replaceArgument(1, new Reference('vich_uploader.adapter.'.$driver)); - // propel does not require tags to work - if (isset($this->tagMap[$driver])) { - $definition->addTag($this->tagMap[$driver], array('priority' => $priority)); - } + $definition->addTag($this->tagMap[$driver], array('priority' => $priority)); } } diff --git a/EventListener/BaseListener.php b/EventListener/BaseListener.php new file mode 100644 index 00000000..65ac5d18 --- /dev/null +++ b/EventListener/BaseListener.php @@ -0,0 +1,61 @@ + + */ +abstract class BaseListener +{ + /** + * @var array + */ + protected $mappings; + + /** + * @var AdapterInterface $adapter + */ + protected $adapter; + + /** + * @var MetadataReader $metadata + */ + protected $metadata; + + /** + * @var UploadHandler $handler + */ + protected $handler; + + /** + * Constructs a new instance of listener. + * + * @param array $mappings The mappings configuration. + * @param AdapterInterface $adapter The adapter. + * @param MetadataReader $metadata The metadata reader. + * @param UploadHandler $handler The upload handler. + */ + public function __construct($mappings, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) + { + $this->mappings = $mappings; + $this->adapter = $adapter; + $this->metadata = $metadata; + $this->handler = $handler; + } + + protected function isFlagEnabledForField($flag, array $field) + { + return true === $this->mappings[$field['mapping']][$flag]; + } + + public function getUploadableFields($object) + { + return $this->metadata->getUploadableFields(ClassUtils::getClass($object)); + } +} diff --git a/EventListener/Doctrine/BaseListener.php b/EventListener/Doctrine/BaseListener.php index f5f655f8..7a88c960 100644 --- a/EventListener/Doctrine/BaseListener.php +++ b/EventListener/Doctrine/BaseListener.php @@ -3,80 +3,14 @@ namespace Vich\UploaderBundle\EventListener\Doctrine; use Doctrine\Common\EventSubscriber; - -use Vich\UploaderBundle\Adapter\AdapterInterface; -use Vich\UploaderBundle\Handler\UploadHandler; -use Vich\UploaderBundle\Metadata\MetadataReader; -use Vich\UploaderBundle\Util\ClassUtils; +use Vich\UploaderBundle\EventListener\BaseListener as CommonListener; /** * BaseListener * - * @author Kévin Gomez + * @author Konstantin Myakshin */ -abstract class BaseListener implements EventSubscriber +abstract class BaseListener extends CommonListener implements EventSubscriber { - /** - * @var string - */ - protected $mapping; - - /** - * @var AdapterInterface $adapter - */ - protected $adapter; - - /** - * @var MetadataReader $metadata - */ - protected $metadata; - - /** - * @var UploadHandler $handler - */ - protected $handler; - - /** - * Constructs a new instance of UploaderListener. - * - * @param string $mapping The mapping name. - * @param AdapterInterface $adapter The adapter. - * @param MetadataReader $metadata The metadata reader. - * @param UploadHandler $handler The upload handler. - */ - public function __construct($mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) - { - $this->mapping = $mapping; - $this->adapter = $adapter; - $this->metadata = $metadata; - $this->handler = $handler; - } - - /** - * Checks if the given object is uploadable using the current mapping. - * - * @param mixed $object The object to test. - * - * @return bool - */ - protected function isUploadable($object) - { - return $this->metadata->isUploadable(ClassUtils::getClass($object), $this->mapping); - } - - /** - * Returns a list of uploadable fields for the given object and mapping. - * - * @param mixed $object The object to use. - * - * @return array A list of field names. - */ - protected function getUploadableFields($object) - { - $fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping); - return array_map(function($data) { - return $data['propertyName']; - }, $fields); - } } diff --git a/EventListener/Doctrine/CleanListener.php b/EventListener/Doctrine/CleanListener.php index f3a33014..07d9754d 100644 --- a/EventListener/Doctrine/CleanListener.php +++ b/EventListener/Doctrine/CleanListener.php @@ -2,43 +2,47 @@ namespace Vich\UploaderBundle\EventListener\Doctrine; -use Doctrine\Common\EventArgs; +use Doctrine\ORM\Event\PreFlushEventArgs; +use Doctrine\ORM\Events; /** * CleanListener * * Listen to the update event to delete old files accordingly. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class CleanListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public function getSubscribedEvents() { - return array( - 'preUpdate', - ); + return array(Events::preFlush); } /** - * @param EventArgs $event The event. + * @param PreFlushEventArgs $event */ - public function preUpdate(EventArgs $event) + public function preFlush(PreFlushEventArgs $event) { - $object = $this->adapter->getObjectFromArgs($event); + $em = $event->getEntityManager(); + $identityMap = $em->getUnitOfWork()->getIdentityMap(); - if (!$this->isUploadable($object)) { - return; - } + foreach ($identityMap as $class => $entities) { + $fields = $this->metadata->getUploadableFields($class); + + foreach ($fields as $field) { + if (!$this->isFlagEnabledForField('delete_on_update', $field)) { + continue; + } - foreach ($this->getUploadableFields($object) as $field) { - $this->handler->clean($object, $field); - $this->adapter->recomputeChangeSet($event); + foreach ($entities as $entity) { + $this->handler->clean($entity, $field['propertyName']); + } + } } } } diff --git a/EventListener/Doctrine/InjectListener.php b/EventListener/Doctrine/InjectListener.php index 766c771d..69e51ac7 100644 --- a/EventListener/Doctrine/InjectListener.php +++ b/EventListener/Doctrine/InjectListener.php @@ -3,41 +3,39 @@ namespace Vich\UploaderBundle\EventListener\Doctrine; use Doctrine\Common\EventArgs; +use Doctrine\ORM\Events; /** * InjectListener * * Listen to the load event in order to inject File objects. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class InjectListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public function getSubscribedEvents() { - return array( - 'postLoad', - ); + return array(Events::postLoad); } /** - * @param EventArgs $event The event. + * @param LifecycleEventArgs $event */ - public function postLoad(EventArgs $event) + public function postLoad(LifecycleEventArgs $event) { $object = $this->adapter->getObjectFromArgs($event); - if (!$this->isUploadable($object)) { - return; - } - foreach ($this->getUploadableFields($object) as $field) { - $this->handler->inject($object, $field); + if (!$this->isFlagEnabledForField('inject_on_load', $field)) { + continue; + } + + $this->handler->inject($object, $field['propertyName']); } } } diff --git a/EventListener/Doctrine/RemoveListener.php b/EventListener/Doctrine/RemoveListener.php index 0912e1ab..6a3bdef6 100644 --- a/EventListener/Doctrine/RemoveListener.php +++ b/EventListener/Doctrine/RemoveListener.php @@ -2,58 +2,59 @@ namespace Vich\UploaderBundle\EventListener\Doctrine; -use Doctrine\Common\EventArgs; use Doctrine\Common\Persistence\Proxy; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Events; /** * RemoveListener * * Listen to the remove event to delete files accordingly. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class RemoveListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public function getSubscribedEvents() { return array( - 'preRemove', - 'postRemove', + Events::preRemove, + Events::postRemove, ); } /** - * Ensures a proxy will be usable in the postRemove. - * - * @param EventArgs $event The event. + * {@inheritdoc} */ - public function preRemove(EventArgs $event) + public function preRemove(LifecycleEventArgs $event) { - $object = $this->adapter->getObjectFromArgs($event); - - if ($this->isUploadable($object) && $object instanceof Proxy) { - $object->__load(); - } + $object = $this->adapter->getObjectFromArgs($event); + // Ensures a proxy will be usable in the postRemove. + if ($object instanceof Proxy) { + $fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object)); + if ($fields) { + $object->__load(); + } + } } /** - * @param EventArgs $event The event. + * {@inheritdoc} */ - public function postRemove(EventArgs $event) + public function postRemove(LifecycleEventArgs $event) { $object = $this->adapter->getObjectFromArgs($event); - if (!$this->isUploadable($object)) { - return; - } - foreach ($this->getUploadableFields($object) as $field) { - $this->handler->remove($object, $field); + if (!$this->isFlagEnabledForField('delete_on_remove', $field)) { + continue; + } + + $this->handler->remove($object, $field['propertyName']); } } } diff --git a/EventListener/Doctrine/UploadListener.php b/EventListener/Doctrine/UploadListener.php index b8f393ef..d7d65cae 100644 --- a/EventListener/Doctrine/UploadListener.php +++ b/EventListener/Doctrine/UploadListener.php @@ -2,61 +2,43 @@ namespace Vich\UploaderBundle\EventListener\Doctrine; -use Doctrine\Common\EventArgs; +use Doctrine\ORM\Event\PreFlushEventArgs; +use Doctrine\ORM\Events; /** * UploadListener * * Handles file uploads. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class UploadListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public function getSubscribedEvents() { - return array( - 'prePersist', - 'preUpdate', - ); + return array(Events::preFlush); } /** - * @param EventArgs $event The event. + * @param PreFlushEventArgs $event */ - public function prePersist(EventArgs $event) + public function preFlush(PreFlushEventArgs $event) { - $object = $this->adapter->getObjectFromArgs($event); + $em = $event->getEntityManager(); + $identityMap = $em->getUnitOfWork()->getIdentityMap(); - if (!$this->isUploadable($object)) { - return; - } + foreach ($identityMap as $class => $entities) { + $fields = $this->metadata->getUploadableFields($class); - foreach ($this->getUploadableFields($object) as $field) { - $this->handler->upload($object, $field); + foreach ($fields as $field) { + foreach ($entities as $entity) { + $this->handler->upload($entity, $field['propertyName']); + } + } } } - - /** - * @param EventArgs $event The event. - */ - public function preUpdate(EventArgs $event) - { - $object = $this->adapter->getObjectFromArgs($event); - - if (!$this->isUploadable($object)) { - return; - } - - foreach ($this->getUploadableFields($object) as $field) { - $this->handler->upload($object, $field); - } - - $this->adapter->recomputeChangeSet($event); - } } diff --git a/EventListener/Propel/BaseListener.php b/EventListener/Propel/BaseListener.php index 0b8c0deb..2ca8c960 100644 --- a/EventListener/Propel/BaseListener.php +++ b/EventListener/Propel/BaseListener.php @@ -3,68 +3,13 @@ namespace Vich\UploaderBundle\EventListener\Propel; use Symfony\Component\EventDispatcher\EventSubscriberInterface; - -use Vich\UploaderBundle\Adapter\AdapterInterface; -use Vich\UploaderBundle\Handler\UploadHandler; -use Vich\UploaderBundle\Metadata\MetadataReader; -use Vich\UploaderBundle\Util\ClassUtils; +use Vich\UploaderBundle\EventListener\BaseListener as CommonListener; /** * BaseListener * - * @author Kévin Gomez + * @author Konstantin Myakshin */ -abstract class BaseListener implements EventSubscriberInterface +abstract class BaseListener extends CommonListener implements EventSubscriberInterface { - /** - * @var string - */ - protected $mapping; - - /** - * @var AdapterInterface $adapter - */ - protected $adapter; - - /** - * @var MetadataReader $metadata - */ - protected $metadata; - - /** - * @var UploaderHandler $handler - */ - protected $handler; - - /** - * Constructs a new instance of BaseListener. - * - * @param string $mapping The mapping name. - * @param AdapterInterface $adapter The adapter. - * @param MetadataReader $metadata The metadata reader. - * @param UploaderHandler $handler The upload handler. - */ - public function __construct($mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) - { - $this->mapping = $mapping; - $this->adapter = $adapter; - $this->metadata = $metadata; - $this->handler = $handler; - } - - /** - * Returns a list of uploadable fields for the given object and mapping. - * - * @param mixed $object The object to use. - * - * @return array A list of field names. - */ - protected function getUploadableFields($object) - { - $fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping); - - return array_map(function($data) { - return $data['propertyName']; - }, $fields); - } } diff --git a/EventListener/Propel/CleanListener.php b/EventListener/Propel/CleanListener.php index 816017ae..39532975 100644 --- a/EventListener/Propel/CleanListener.php +++ b/EventListener/Propel/CleanListener.php @@ -9,14 +9,13 @@ * * Listen to the update event to delete old files accordingly. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class CleanListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public static function getSubscribedEvents() { @@ -33,7 +32,11 @@ public function onUpload(GenericEvent $event) $object = $this->adapter->getObjectFromArgs($event); foreach ($this->getUploadableFields($object) as $field) { - $this->handler->clean($object, $field); + if (!$this->isFlagEnabledForField('delete_on_update', $field)) { + continue; + } + + $this->handler->clean($object, $field['propertyName']); } } } diff --git a/EventListener/Propel/InjectListener.php b/EventListener/Propel/InjectListener.php index df6e1387..677cb936 100644 --- a/EventListener/Propel/InjectListener.php +++ b/EventListener/Propel/InjectListener.php @@ -9,14 +9,13 @@ * * Listen to the load event in order to inject File objects. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class InjectListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public static function getSubscribedEvents() { @@ -33,7 +32,11 @@ public function onHydrate(GenericEvent $event) $object = $this->adapter->getObjectFromArgs($event); foreach ($this->getUploadableFields($object) as $field) { - $this->handler->inject($object, $field); + if (!$this->isFlagEnabledForField('inject_on_load', $field)) { + continue; + } + + $this->handler->inject($object, $field['propertyName']); } } } diff --git a/EventListener/Propel/RemoveListener.php b/EventListener/Propel/RemoveListener.php index 8016516c..2c4f5f33 100644 --- a/EventListener/Propel/RemoveListener.php +++ b/EventListener/Propel/RemoveListener.php @@ -9,6 +9,7 @@ * * Listen to the remove event to delete files accordingly. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class RemoveListener extends BaseListener @@ -33,7 +34,11 @@ public function onDelete(GenericEvent $event) $object = $this->adapter->getObjectFromArgs($event); foreach ($this->getUploadableFields($object) as $field) { - $this->handler->remove($object, $field); + if (!$this->isFlagEnabledForField('delete_on_remove', $field)) { + continue; + } + + $this->handler->remove($object, $field['propertyName']); } } } diff --git a/EventListener/Propel/UploadListener.php b/EventListener/Propel/UploadListener.php index 0474ef19..db24d3e7 100644 --- a/EventListener/Propel/UploadListener.php +++ b/EventListener/Propel/UploadListener.php @@ -9,14 +9,13 @@ * * Handles file uploads. * + * @author Konstantin Myakshin * @author Kévin Gomez */ class UploadListener extends BaseListener { /** - * The events the listener is subscribed to. - * - * @return array The array of events. + * {@inheritdoc} */ public static function getSubscribedEvents() { @@ -34,7 +33,7 @@ public function onUpload(GenericEvent $event) $object = $this->adapter->getObjectFromArgs($event); foreach ($this->getUploadableFields($object) as $field) { - $this->handler->upload($object, $field); + $this->handler->upload($object, $field['propertyName']); } } } diff --git a/Metadata/MetadataReader.php b/Metadata/MetadataReader.php index 0cb01641..c860adb0 100644 --- a/Metadata/MetadataReader.php +++ b/Metadata/MetadataReader.php @@ -42,7 +42,9 @@ public function isUploadable($class, $mapping = null) if ($metadata === null) { return false; - } elseif ($mapping === null) { + } + + if ($mapping === null) { return true; } @@ -75,8 +77,11 @@ public function getUploadableClasses() public function getUploadableFields($class) { $metadata = $this->reader->getMetadataForClass($class); - $uploadableFields = array(); + if (null === $metadata) { + return array(); + } + $uploadableFields = array(); foreach ($metadata->classMetadata as $classMetadata) { $uploadableFields = array_merge($uploadableFields, $classMetadata->fields); } diff --git a/Resources/config/listener.xml b/Resources/config/listener.xml index 00175fe1..abf742bb 100644 --- a/Resources/config/listener.xml +++ b/Resources/config/listener.xml @@ -6,18 +6,18 @@ - - - + + %vich_uploader.mappings% - - - - + + + + + @@ -30,17 +30,10 @@ - - - - - - - - - - - + + + + diff --git a/Resources/doc/known_issues.md b/Resources/doc/known_issues.md index 071c7e26..e65adda2 100644 --- a/Resources/doc/known_issues.md +++ b/Resources/doc/known_issues.md @@ -1,45 +1,6 @@ Known issues ============ -## The file is not updated if there are not other changes in the entity - -As the bundle is listening to Doctrine `prePersist` and `preUpdate` events, which are not fired -when there is no change on field mapped by Doctrine, the file upload is not handled if the image field -is the only updated. - -A workaround to solve this issue is to manually generate a change: - -```php -use Symfony\Component\HttpFoundation\File\File; -use Symfony\Component\HttpFoundation\File\UploadedFile; - -class Product -{ - // ... - - /** - * @ORM\Column(type="datetime") - * - * @var \DateTime - */ - private $updatedAt; - - // ... - - public function setImage(File $image = null) - { - $this->image = $image; - - // Only change the updated af if the file is really uploaded to avoid database updates. - // This is needed when the file should be set when loading the entity. - if ($this->image instanceof UploadedFile) { - $this->updatedAt = new \DateTime('now'); - } - } -} -``` -See issue [GH-123](https://github.com/dustin10/VichUploaderBundle/issues/123) - ## Catchable Fatal Error When you get the following error:`Catchable Fatal Error: ... must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, ...` diff --git a/Tests/EventListener/Doctrine/CleanListenerTest.php b/Tests/EventListener/Doctrine/CleanListenerTest.php index 79309aa1..2f767bc9 100644 --- a/Tests/EventListener/Doctrine/CleanListenerTest.php +++ b/Tests/EventListener/Doctrine/CleanListenerTest.php @@ -2,6 +2,7 @@ namespace Vich\UploaderBundle\Tests\EventListener\Doctrine; +use Doctrine\ORM\Events; use Vich\UploaderBundle\EventListener\Doctrine\CleanListener; /** @@ -28,11 +29,11 @@ public function testGetSubscribedEvents() { $events = $this->listener->getSubscribedEvents(); - $this->assertSame(array('preUpdate'), $events); + $this->assertSame(array(Events::preFlush), $events); } /** - * Test the preUpdate method. + * Test the preFlush method. */ public function testPreUpdate() { @@ -55,16 +56,11 @@ public function testPreUpdate() ->method('clean') ->with($this->object, 'field_name'); - $this->adapter - ->expects($this->once()) - ->method('recomputeChangeSet') - ->with($this->event); - - $this->listener->preUpdate($this->event); + $this->listener->preFlush($this->event); } /** - * Test that preUpdate skips non uploadable entity. + * Test that preFlush skips non uploadable entity. */ public function testPreUpdateSkipsNonUploadable() { @@ -78,10 +74,6 @@ public function testPreUpdateSkipsNonUploadable() ->expects($this->never()) ->method('clean'); - $this->adapter - ->expects($this->never()) - ->method('recomputeChangeSet'); - - $this->listener->preUpdate($this->event); + $this->listener->preFlush($this->event); } } diff --git a/Tests/EventListener/Doctrine/InjectListenerTest.php b/Tests/EventListener/Doctrine/InjectListenerTest.php index a5e4c0ef..748ed61a 100644 --- a/Tests/EventListener/Doctrine/InjectListenerTest.php +++ b/Tests/EventListener/Doctrine/InjectListenerTest.php @@ -2,6 +2,7 @@ namespace Vich\UploaderBundle\Tests\EventListener\Doctrine; +use Doctrine\ORM\Events; use Vich\UploaderBundle\EventListener\Doctrine\InjectListener; /** @@ -28,7 +29,7 @@ public function testGetSubscribedEvents() { $events = $this->listener->getSubscribedEvents(); - $this->assertSame(array('postLoad'), $events); + $this->assertSame(array(Events::postLoad), $events); } /** diff --git a/Tests/EventListener/Doctrine/RemoveListenerTest.php b/Tests/EventListener/Doctrine/RemoveListenerTest.php index b610145a..1ed47199 100644 --- a/Tests/EventListener/Doctrine/RemoveListenerTest.php +++ b/Tests/EventListener/Doctrine/RemoveListenerTest.php @@ -2,6 +2,7 @@ namespace Vich\UploaderBundle\Tests\EventListener\Doctrine; +use Doctrine\ORM\Events; use Vich\UploaderBundle\EventListener\Doctrine\RemoveListener; /** @@ -28,7 +29,7 @@ public function testGetSubscribedEvents() { $events = $this->listener->getSubscribedEvents(); - $this->assertSame(array('preRemove', 'postRemove'), $events); + $this->assertSame(array(Events::preRemove, Events::postRemove), $events); } public function testPreRemove() diff --git a/Tests/EventListener/Doctrine/UploadListenerTest.php b/Tests/EventListener/Doctrine/UploadListenerTest.php index cf1f0bf0..345f1c52 100644 --- a/Tests/EventListener/Doctrine/UploadListenerTest.php +++ b/Tests/EventListener/Doctrine/UploadListenerTest.php @@ -2,6 +2,7 @@ namespace Vich\UploaderBundle\Tests\EventListener\Doctrine; +use Doctrine\ORM\Events; use Vich\UploaderBundle\EventListener\Doctrine\UploadListener; /** @@ -28,13 +29,13 @@ public function testGetSubscribedEvents() { $events = $this->listener->getSubscribedEvents(); - $this->assertSame(array('prePersist', 'preUpdate'), $events); + $this->assertSame(array(Events::preFlush), $events); } /** - * Tests the prePersist method. + * Tests the preFlush method. */ - public function testPrePersist() + public function testPreFlush() { $this->metadata ->expects($this->once()) @@ -55,11 +56,11 @@ public function testPrePersist() ->method('upload') ->with($this->object, 'field_name'); - $this->listener->prePersist($this->event); + $this->listener->preFlush($this->event); } /** - * Tests that prePersist skips non-uploadable entity. + * Tests that preFlush skips non-uploadable entity. */ public function testPrePersistSkipsNonUploadable() { @@ -73,60 +74,6 @@ public function testPrePersistSkipsNonUploadable() ->expects($this->never()) ->method('upload'); - $this->listener->prePersist($this->event); - } - - /** - * Test the preUpdate method. - */ - public function testPreUpdate() - { - $this->adapter - ->expects($this->once()) - ->method('recomputeChangeSet') - ->with($this->event); - - $this->metadata - ->expects($this->once()) - ->method('isUploadable') - ->with('Vich\UploaderBundle\Tests\DummyEntity') - ->will($this->returnValue(true)); - - $this->metadata - ->expects($this->once()) - ->method('getUploadableFields') - ->with('Vich\UploaderBundle\Tests\DummyEntity', self::MAPPING_NAME) - ->will($this->returnValue(array( - array('propertyName' => 'field_name') - ))); - - $this->handler - ->expects($this->once()) - ->method('upload') - ->with($this->object, 'field_name'); - - $this->listener->preUpdate($this->event); - } - - /** - * Test that preUpdate skips non uploadable entity. - */ - public function testPreUpdateSkipsNonUploadable() - { - $this->metadata - ->expects($this->once()) - ->method('isUploadable') - ->with('Vich\UploaderBundle\Tests\DummyEntity') - ->will($this->returnValue(false)); - - $this->adapter - ->expects($this->never()) - ->method('recomputeChangeSet'); - - $this->handler - ->expects($this->never()) - ->method('upload'); - - $this->listener->preUpdate($this->event); + $this->listener->preFlush($this->event); } }