Skip to content

Commit

Permalink
refactor #790 Init respond processor (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT

Based on #789 

Commits
-------

4e89297 Phpunit tests for Symfony flash listener
ed1b756 Fix coding standard
4740788 Fix PHPUnit tests
d497e2e Init flash processor
28876a4 Fix coding standard
1a7dcaf Phpunit tests for Symfony respond listener
829cd5b Init respond processor
  • Loading branch information
lchrusciel authored Nov 22, 2023
2 parents dae17d4 + 829cd5b commit 48f8af3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Component/Tests/State/Processor/RespondProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace State\Processor;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Component\Resource\src\State\Processor\RespondProcessor;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Context\Initiator\RequestContextInitiatorInterface;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\State\ProcessorInterface;
use Sylius\Resource\State\ResponderInterface;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

final class RespondProcessorTest extends TestCase
{
use ProphecyTrait;

private ProcessorInterface|ObjectProphecy $decorated;

private RequestContextInitiatorInterface|ObjectProphecy $contextInitiator;

private ResponderInterface|ObjectProphecy $responder;

private RespondProcessor $respondProcessor;

protected function setUp(): void
{
$this->decorated = $this->prophesize(ProcessorInterface::class);
$this->responder = $this->prophesize(ResponderInterface::class);

$this->respondProcessor = new RespondProcessor(
$this->decorated->reveal(),
$this->responder->reveal(),
);
}

/** @test */
public function it_returns_a_response(): void
{
$response = $this->prophesize(Response::class);
$operation = $this->prophesize(HttpOperation::class);

$context = new Context();

$this->responder->respond(['foo' => 'fighters'], $operation, $context)
->willReturn($response)
->shouldBeCalled()
;

$this->decorated->process(['foo' => 'fighters'], $operation, $context)->willReturn(['foo' => 'fighters'])->shouldBeCalled();

$data = $this->respondProcessor->process(['foo' => 'fighters'], $operation->reveal(), $context);
Assert::eq($data, $response->reveal());
}

/** @test */
public function it_does_nothing_when_data_is_a_response(): void
{
$response = $this->prophesize(Response::class);
$operation = $this->prophesize(HttpOperation::class);

$context = new Context();

$this->decorated->process($response, $operation, $context)->willReturn($response)->shouldBeCalled();

$this->responder->respond($response, $operation, $context)
->willReturn($response)
->shouldNotBeCalled()
;

$data = $this->respondProcessor->process($response, $operation->reveal(), $context);
Assert::eq($data, $response->reveal());
}
}
44 changes: 44 additions & 0 deletions src/Component/src/State/Processor/RespondProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\src\State\Processor;

use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\State\ProcessorInterface;
use Sylius\Resource\State\ResponderInterface;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

final class RespondProcessor implements ProcessorInterface
{
public function __construct(
private ProcessorInterface $decorated,
private ResponderInterface $responder,
) {
}

public function process(mixed $data, Operation $operation, Context $context): mixed
{
$data = $this->decorated->process($data, $operation, $context);

if ($data instanceof Response) {
return $data;
}

$response = $this->responder->respond($data, $operation, $context);
Assert::isInstanceOf($response, Response::class);

return $response;
}
}

0 comments on commit 48f8af3

Please sign in to comment.