diff --git a/.travis.yml b/.travis.yml index 57310fda..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 diff --git a/Event/Event.php b/Event/Event.php index bb5e3254..4eb937d5 100644 --- a/Event/Event.php +++ b/Event/Event.php @@ -3,42 +3,79 @@ 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; -/** +/* * Base class for upload events. * * @author Kévin Gomez */ -class Event extends BaseEvent -{ - protected $object; +if ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && 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..c56e6430 100644 --- a/Handler/UploadHandler.php +++ b/Handler/UploadHandler.php @@ -3,6 +3,8 @@ namespace Vich\UploaderBundle\Handler; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +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; @@ -104,7 +106,11 @@ public function remove($obj, string $fieldName): void protected function dispatch(string $eventName, Event $event): void { - $this->dispatcher->dispatch($eventName, $event); + if ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && class_exists(ContractEvent::class)) { + $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 3d81c88c..5f2e5d97 100644 --- a/Tests/Handler/UploadHandlerTest.php +++ b/Tests/Handler/UploadHandlerTest.php @@ -3,9 +3,12 @@ namespace Vich\UploaderBundle\Tests\Handler; 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; +use Vich\UploaderBundle\Exception\MappingNotFoundException; use Vich\UploaderBundle\Handler\UploadHandler; use Vich\UploaderBundle\Injector\FileInjectorInterface; use Vich\UploaderBundle\Storage\StorageInterface; @@ -14,7 +17,7 @@ /** * @author Kévin Gomez */ -class UploadHandlerTest extends TestCase +final class UploadHandlerTest extends TestCase { protected $factory; @@ -80,12 +83,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 @@ -228,7 +231,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 +239,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 ('42' !== Kernel::MAJOR_VERSION.Kernel::MINOR_VERSION && class_exists(ContractEvent::class)) { + $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..b59996e4 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "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/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 +38,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",