From f6fac72b189d146272f3a75c303c4a4e56bfd437 Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Sun, 9 Jun 2019 15:47:11 +0200 Subject: [PATCH 1/2] try to avoid event deprecation See issue #1006 --- .travis.yml | 1 + Event/Event.php | 88 ++++++++++++++++++++--------- Handler/UploadHandler.php | 7 ++- Tests/Handler/UploadHandlerTest.php | 41 +++++++++----- composer.json | 9 +-- 5 files changed, 100 insertions(+), 46 deletions(-) diff --git a/.travis.yml b/.travis.yml index 57310fda..6d486b56 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ before_install: - composer self-update - phpenv config-rm xdebug.ini || true - if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/symfony:${SYMFONY_VERSION}; fi + - if [ "$SYMFONY_VERSION" = "~3.4.0" ]; then composer remove symfony/event-dispatcher-contracts; fi - if [ "$WITH_LIIP_IMAGINE" = true ] ; then composer require --no-update liip/imagine-bundle:"^1.7|^2.0"; fi; - if [ "$VALIDATE_DOCS" = true ]; then composer require --dev --no-update kphoen/rusty dev-master; fi diff --git a/Event/Event.php b/Event/Event.php index bb5e3254..9e37a66b 100644 --- a/Event/Event.php +++ b/Event/Event.php @@ -3,42 +3,78 @@ namespace Vich\UploaderBundle\Event; use Symfony\Component\EventDispatcher\Event as BaseEvent; +use Symfony\Contracts\EventDispatcher\Event as ContractEvent; use Vich\UploaderBundle\Mapping\PropertyMapping; -/** +/* * Base class for upload events. * * @author Kévin Gomez */ -class Event extends BaseEvent -{ - protected $object; +if (class_exists(ContractEvent::class)) { + class Event extends ContractEvent + { + protected $object; - protected $mapping; + protected $mapping; - public function __construct($object, PropertyMapping $mapping) - { - $this->object = $object; - $this->mapping = $mapping; - } + public function __construct($object, PropertyMapping $mapping) + { + $this->object = $object; + $this->mapping = $mapping; + } - /** - * Accessor to the object being manipulated. - * - * @return object - */ - public function getObject() - { - return $this->object; - } + /** + * Accessor to the object being manipulated. + * + * @return object + */ + public function getObject() + { + return $this->object; + } - /** - * Accessor to the mapping used to manipulate the object. - * - * @return PropertyMapping - */ - public function getMapping(): PropertyMapping + /** + * Accessor to the mapping used to manipulate the object. + * + * @return PropertyMapping + */ + public function getMapping(): PropertyMapping + { + return $this->mapping; + } + } +} else { + class Event extends BaseEvent { - return $this->mapping; + protected $object; + + protected $mapping; + + public function __construct($object, PropertyMapping $mapping) + { + $this->object = $object; + $this->mapping = $mapping; + } + + /** + * Accessor to the object being manipulated. + * + * @return object + */ + public function getObject() + { + return $this->object; + } + + /** + * Accessor to the mapping used to manipulate the object. + * + * @return PropertyMapping + */ + public function getMapping(): PropertyMapping + { + return $this->mapping; + } } } diff --git a/Handler/UploadHandler.php b/Handler/UploadHandler.php index 9b51b445..2831e641 100644 --- a/Handler/UploadHandler.php +++ b/Handler/UploadHandler.php @@ -4,6 +4,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\HttpKernel\Kernel; use Vich\UploaderBundle\Event\Event; use Vich\UploaderBundle\Event\Events; use Vich\UploaderBundle\Injector\FileInjectorInterface; @@ -104,7 +105,11 @@ public function remove($obj, string $fieldName): void protected function dispatch(string $eventName, Event $event): void { - $this->dispatcher->dispatch($eventName, $event); + if (Kernel::VERSION_ID >= 40300) { + $this->dispatcher->dispatch($event, $eventName); + } else { + $this->dispatcher->dispatch($eventName, $event); + } } protected function hasUploadedFile($obj, PropertyMapping $mapping): bool diff --git a/Tests/Handler/UploadHandlerTest.php b/Tests/Handler/UploadHandlerTest.php index c0baddbc..7dedfaf6 100644 --- a/Tests/Handler/UploadHandlerTest.php +++ b/Tests/Handler/UploadHandlerTest.php @@ -3,9 +3,11 @@ namespace Vich\UploaderBundle\Tests\Handler; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\HttpKernel\Kernel; use Vich\TestBundle\Entity\Article; use Vich\UploaderBundle\Event\Event; use Vich\UploaderBundle\Event\Events; +use Vich\UploaderBundle\Exception\MappingNotFoundException; use Vich\UploaderBundle\Handler\UploadHandler; use Vich\UploaderBundle\Injector\FileInjectorInterface; use Vich\UploaderBundle\Storage\StorageInterface; @@ -14,7 +16,7 @@ /** * @author Kévin Gomez */ -class UploadHandlerTest extends TestCase +final class UploadHandlerTest extends TestCase { protected $factory; @@ -49,7 +51,7 @@ protected function setUp(): void ->expects($this->any()) ->method('fromField') ->with($this->object, self::FILE_FIELD) - ->will($this->returnValue($this->mapping)); + ->willReturn($this->mapping); } public function testUpload(): void @@ -60,7 +62,7 @@ public function testUpload(): void ->expects($this->once()) ->method('getFile') ->with($this->object) - ->will($this->returnValue($this->getUploadedFileMock())); + ->willReturn($this->getUploadedFileMock()); $this->storage ->expects($this->once()) @@ -80,12 +82,12 @@ public function testUpload(): void */ public function testAnExceptionIsThrownIfMappingIsntFound($method): void { - $this->expectException(\Vich\UploaderBundle\Exception\MappingNotFoundException::class); + $this->expectException(MappingNotFoundException::class); $this->factory = $this->getPropertyMappingFactoryMock(); $handler = new UploadHandler($this->factory, $this->storage, $this->injector, $this->dispatcher); - \call_user_func([$handler, $method], $this->object, self::FILE_FIELD); + $handler->$method($this->object, self::FILE_FIELD); } public function methodProvider(): array @@ -135,13 +137,13 @@ public function testClean(): void ->expects($this->once()) ->method('getFile') ->with($this->object) - ->will($this->returnValue($this->getUploadedFileMock())); + ->willReturn($this->getUploadedFileMock()); $this->mapping ->expects($this->once()) ->method('getFileName') ->with($this->object) - ->will($this->returnValue('something not null')); + ->willReturn('something not null'); $this->storage ->expects($this->once()) @@ -157,7 +159,7 @@ public function testCleanSkipsEmptyObjects(): void ->expects($this->any()) ->method('getFileName') ->with($this->object) - ->will($this->returnValue('something not null')); + ->willReturn('something not null'); $this->dispatcher ->expects($this->never()) @@ -178,13 +180,13 @@ public function testRemove(): void ->expects($this->once()) ->method('getFileName') ->with($this->object) - ->will($this->returnValue('something not null')); + ->willReturn('something not null'); $this->mapping ->expects($this->once()) ->method('erase') ->with($this->object) - ->will($this->returnValue(null)); + ->willReturn(null); $this->storage ->expects($this->once()) @@ -228,7 +230,7 @@ protected function validEvent() $object = $this->object; $mapping = $this->mapping; - return $this->callback(function ($event) use ($object, $mapping) { + return $this->callback(static function ($event) use ($object, $mapping) { return $event instanceof Event && $event->getObject() === $object && $event->getMapping() === $mapping; }); } @@ -236,10 +238,19 @@ protected function validEvent() protected function expectEvents(array $events): void { foreach ($events as $i => $event) { - $this->dispatcher - ->expects($this->at($i)) - ->method('dispatch') - ->with($event, $this->validEvent()); + if (Kernel::VERSION_ID >= 40300) { + $this->dispatcher + ->expects($this->at($i)) + ->method('dispatch') + ->with($this->validEvent(), $event) + ; + } else { + $this->dispatcher + ->expects($this->at($i)) + ->method('dispatch') + ->with($event, $this->validEvent()) + ; + } } } } diff --git a/composer.json b/composer.json index e94cc8d6..ece16aad 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "symfony/config": "^3.4 || ^4.0", "symfony/dependency-injection": "^3.4 || ^4.0", "symfony/event-dispatcher": "^3.4 || ^4.0", - "symfony/form": "^3.4 || ^4.0", + "symfony/event-dispatcher-contracts": "^1.1", + "symfony/form": "^3.4.19 || ^4.0", "symfony/http-foundation": "^3.4 || ^4.0", "symfony/http-kernel": "^3.4 || ^4.0", "symfony/property-access": "^3.4 || ^4.0", @@ -38,13 +39,13 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.0", "mikey179/vfsstream": "^1.6", "oneup/flysystem-bundle": "^3.0", - "phpunit/phpunit": "^7.0 || ^8.0", + "phpunit/phpunit": "^8.0", "symfony/browser-kit": "^3.4 || ^4.0", "symfony/css-selector": "^3.4 || ^4.0", "symfony/doctrine-bridge": "^3.4 || ^4.0", "symfony/dom-crawler": "^3.4 || ^4.0", - "symfony/framework-bundle": "^3.4 || ^4.0", - "symfony/phpunit-bridge": "^3.3", + "symfony/framework-bundle": "^3.4.23 || ^4.2.4", + "symfony/phpunit-bridge": "^4.3", "symfony/security-csrf": "^3.4 || ^4.0", "symfony/translation": "^3.4 || ^4.0", "symfony/twig-bridge": "^3.4 || ^4.0", From 5465e0e3ae1353322e3aebce4c76966e01723eeb Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Wed, 12 Jun 2019 13:18:34 +0200 Subject: [PATCH 2/2] make it working with all Symfony versions --- .travis.yml | 3 +-- Event/Event.php | 3 ++- Handler/UploadHandler.php | 5 +++-- Tests/Handler/UploadHandlerTest.php | 3 ++- composer.json | 1 - 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d486b56..1e22edf4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ matrix: - php: 7.2 env: SYMFONY_VERSION="~4.2.0" WITH_LIIP_IMAGINE=true - php: 7.3 - env: SYMFONY_VERSION="~4.2.0" WITH_LIIP_IMAGINE=true + env: SYMFONY_VERSION="~4.3.0" WITH_LIIP_IMAGINE=true allow_failures: - php: nightly @@ -33,7 +33,6 @@ before_install: - composer self-update - phpenv config-rm xdebug.ini || true - if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/symfony:${SYMFONY_VERSION}; fi - - if [ "$SYMFONY_VERSION" = "~3.4.0" ]; then composer remove symfony/event-dispatcher-contracts; fi - if [ "$WITH_LIIP_IMAGINE" = true ] ; then composer require --no-update liip/imagine-bundle:"^1.7|^2.0"; fi; - if [ "$VALIDATE_DOCS" = true ]; then composer require --dev --no-update kphoen/rusty dev-master; fi diff --git a/Event/Event.php b/Event/Event.php index 9e37a66b..4eb937d5 100644 --- a/Event/Event.php +++ b/Event/Event.php @@ -3,6 +3,7 @@ namespace Vich\UploaderBundle\Event; use Symfony\Component\EventDispatcher\Event as BaseEvent; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Contracts\EventDispatcher\Event as ContractEvent; use Vich\UploaderBundle\Mapping\PropertyMapping; @@ -11,7 +12,7 @@ * * @author Kévin Gomez */ -if (class_exists(ContractEvent::class)) { +if ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && class_exists(ContractEvent::class)) { class Event extends ContractEvent { protected $object; diff --git a/Handler/UploadHandler.php b/Handler/UploadHandler.php index 2831e641..c56e6430 100644 --- a/Handler/UploadHandler.php +++ b/Handler/UploadHandler.php @@ -3,8 +3,9 @@ namespace Vich\UploaderBundle\Handler; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Contracts\EventDispatcher\Event as ContractEvent; +use Symfony\Component\HttpFoundation\File\UploadedFile; use Vich\UploaderBundle\Event\Event; use Vich\UploaderBundle\Event\Events; use Vich\UploaderBundle\Injector\FileInjectorInterface; @@ -105,7 +106,7 @@ public function remove($obj, string $fieldName): void protected function dispatch(string $eventName, Event $event): void { - if (Kernel::VERSION_ID >= 40300) { + if ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && class_exists(ContractEvent::class)) { $this->dispatcher->dispatch($event, $eventName); } else { $this->dispatcher->dispatch($eventName, $event); diff --git a/Tests/Handler/UploadHandlerTest.php b/Tests/Handler/UploadHandlerTest.php index 7dedfaf6..5f2e5d97 100644 --- a/Tests/Handler/UploadHandlerTest.php +++ b/Tests/Handler/UploadHandlerTest.php @@ -4,6 +4,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Contracts\EventDispatcher\Event as ContractEvent; use Vich\TestBundle\Entity\Article; use Vich\UploaderBundle\Event\Event; use Vich\UploaderBundle\Event\Events; @@ -238,7 +239,7 @@ protected function validEvent() protected function expectEvents(array $events): void { foreach ($events as $i => $event) { - if (Kernel::VERSION_ID >= 40300) { + if ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && class_exists(ContractEvent::class)) { $this->dispatcher ->expects($this->at($i)) ->method('dispatch') diff --git a/composer.json b/composer.json index ece16aad..b59996e4 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,6 @@ "symfony/config": "^3.4 || ^4.0", "symfony/dependency-injection": "^3.4 || ^4.0", "symfony/event-dispatcher": "^3.4 || ^4.0", - "symfony/event-dispatcher-contracts": "^1.1", "symfony/form": "^3.4.19 || ^4.0", "symfony/http-foundation": "^3.4 || ^4.0", "symfony/http-kernel": "^3.4 || ^4.0",