Skip to content

Commit

Permalink
Merge pull request dustin10#1009 from dustin10/new-event-class
Browse files Browse the repository at this point in the history
New event class
  • Loading branch information
garak authored Jun 12, 2019
2 parents d862fe9 + 5465e0e commit 1c3c14a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 63 additions & 26 deletions Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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;
}
}
}
8 changes: 7 additions & 1 deletion Handler/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions Tests/Handler/UploadHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +17,7 @@
/**
* @author Kévin Gomez <[email protected]>
*/
class UploadHandlerTest extends TestCase
final class UploadHandlerTest extends TestCase
{
protected $factory;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,18 +231,27 @@ 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;
});
}

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())
;
}
}
}
}
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 1c3c14a

Please sign in to comment.