-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching ArgumentValueResolverInterface into ValueResolverInterface (#…
…15) Co-authored-by: Witold Karaś <wk@k4g.com>
- Loading branch information
Showing
4 changed files
with
127 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace DualMedia\DtoRequestBundle\ValueResolver; | ||
|
||
use DualMedia\DtoRequestBundle\Event\DtoResolvedEvent; | ||
use DualMedia\DtoRequestBundle\Interfaces\DtoInterface; | ||
use DualMedia\DtoRequestBundle\Interfaces\Resolver\DtoResolverInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** @psalm-suppress UndefinedClass */ | ||
if (interface_exists(\Symfony\Component\HttpKernel\Controller\ValueResolverInterface::class)) { | ||
class DtoValueResolver implements \Symfony\Component\HttpKernel\Controller\ValueResolverInterface | ||
{ | ||
public function __construct( | ||
private DtoResolverInterface $dtoResolver, | ||
private EventDispatcherInterface $eventDispatcher | ||
) { | ||
} | ||
|
||
/** | ||
* @return iterable<DtoInterface> | ||
*/ | ||
public function resolve( | ||
Request $request, | ||
ArgumentMetadata $argument | ||
): iterable { | ||
$class = $argument->getType(); | ||
|
||
if (null === $class || !is_subclass_of($class, DtoInterface::class)) { | ||
return []; | ||
} | ||
|
||
$this->eventDispatcher->dispatch( | ||
new DtoResolvedEvent( | ||
$object = $this->dtoResolver->resolve($request, $class) | ||
) | ||
); | ||
|
||
$object->setOptional($argument->isNullable()); | ||
|
||
yield $object; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace DualMedia\DtoRequestBundle\Tests\Unit\ValueResolver; | ||
|
||
use DualMedia\DtoRequestBundle\Event\DtoResolvedEvent; | ||
use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\ResolveDto\SubDto; | ||
use DualMedia\DtoRequestBundle\Tests\PHPUnit\KernelTestCase; | ||
use DualMedia\DtoRequestBundle\ValueResolver\DtoValueResolver; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
class DtoValueResolverTest extends KernelTestCase | ||
{ | ||
private ValueResolverInterface $service; | ||
private MockObject $eventMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
$this->eventMock = $this->createMock(EventDispatcherInterface::class); | ||
self::$container->set('event_dispatcher', $this->eventMock); | ||
|
||
if (interface_exists(\Symfony\Component\HttpKernel\Controller\ValueResolverInterface::class)) { | ||
$this->service = $this->getService(DtoValueResolver::class); | ||
} | ||
} | ||
|
||
public function testResolve(): void | ||
{ | ||
if (!interface_exists(\Symfony\Component\HttpKernel\Controller\ValueResolverInterface::class)) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
$event = $this->deferCallable(function ($event): void { | ||
$this->assertInstanceOf(DtoResolvedEvent::class, $event); | ||
$this->assertInstanceOf(SubDto::class, $event->getDto()); | ||
}); | ||
|
||
$this->eventMock->expects($this->once()) | ||
->method('dispatch') | ||
->willReturnCallback(function (...$args) use ($event) { | ||
$event->set($args); | ||
|
||
return $args[0]; | ||
}); | ||
|
||
$request = new Request([], [ | ||
'value' => 155, | ||
'floaty_boy' => 22.5, | ||
]); | ||
|
||
$mock = $this->createMock(ArgumentMetadata::class); | ||
$mock->method('getType') | ||
->willReturn(SubDto::class); | ||
$mock->method('isNullable') | ||
->willReturn(false); | ||
|
||
/** | ||
* @var SubDto $dto | ||
* | ||
* @psalm-suppress InvalidArgument | ||
*/ | ||
$dto = iterator_to_array($this->service->resolve($request, $mock))[0]; | ||
|
||
$this->assertInstanceOf(SubDto::class, $dto); | ||
$this->assertTrue($dto->isValid()); | ||
$this->assertEquals(155, $dto->value); | ||
$this->assertEquals(22.5, $dto->floatVal); | ||
$this->assertTrue($dto->visited('value')); | ||
$this->assertTrue($dto->visited('floatVal')); | ||
} | ||
} |