Skip to content

Commit

Permalink
Init respond processor
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Nov 21, 2023
1 parent a72ced6 commit 8bb9845
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 8bb9845

Please sign in to comment.