forked from dustin10/VichUploaderBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dustin10#1009 from dustin10/new-event-class
New event class
- Loading branch information
Showing
5 changed files
with
95 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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,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()) | ||
; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters