Skip to content

Commit

Permalink
Phpunit tests for Symfony respond listener
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Nov 21, 2023
1 parent 28876a4 commit 1a7dcaf
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 112 deletions.
122 changes: 122 additions & 0 deletions src/Component/Tests/Symfony/EventListener/RespondListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?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\Tests\Symfony\EventListener;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Context\Initiator\RequestContextInitiatorInterface;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\Operation\HttpOperationInitiatorInterface;
use Sylius\Resource\State\ResponderInterface;
use Sylius\Resource\Symfony\EventListener\RespondListener;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Webmozart\Assert\Assert;

final class RespondListenerTest extends TestCase
{
use ProphecyTrait;

private HttpOperationInitiatorInterface|ObjectProphecy $operationInitiator;

private RequestContextInitiatorInterface|ObjectProphecy $contextInitiator;

private ResponderInterface|ObjectProphecy $responder;

private RespondListener $respondListener;

protected function setUp(): void
{
$this->operationInitiator = $this->prophesize(HttpOperationInitiatorInterface::class);
$this->contextInitiator = $this->prophesize(RequestContextInitiatorInterface::class);
$this->responder = $this->prophesize(ResponderInterface::class);

$this->respondListener = new RespondListener(
$this->operationInitiator->reveal(),
$this->contextInitiator->reveal(),
$this->responder->reveal(),
);
}

/** @test */
public function it_sets_a_response_on_event(): void
{
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = $this->prophesize(Request::class);
$response = $this->prophesize(Response::class);
$attributes = $this->prophesize(ParameterBag::class);
$operation = $this->prophesize(HttpOperation::class);

$event = new ViewEvent(
$kernel->reveal(),
$request->reveal(),
HttpKernelInterface::MAIN_REQUEST,
['foo' => 'fighters'],
);

$context = new Context();

$this->contextInitiator->initializeContext($request)->willReturn($context);
$this->operationInitiator->initializeOperation($request)->willReturn($operation);

$request->attributes = $attributes;
$request->getMethod()->willReturn('POST');

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

$this->respondListener->onKernelView($event);

Assert::eq($event->getResponse(), $response->reveal());
}

/** @test */
public function it_does_nothing_when_controller_result_is_a_response(): void
{
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = $this->prophesize(Request::class);
$response = $this->prophesize(Response::class);
$attributes = $this->prophesize(ParameterBag::class);
$operation = $this->prophesize(HttpOperation::class);

$event = new ViewEvent(
$kernel->reveal(),
$request->reveal(),
HttpKernelInterface::MAIN_REQUEST,
$response->reveal(),
);

$context = new Context();

$this->contextInitiator->initializeContext($request)->willReturn($context);
$this->operationInitiator->initializeOperation($request)->willReturn($operation);

$request->attributes = $attributes;
$request->getMethod()->willReturn('POST');

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

$this->respondListener->onKernelView($event);
}
}
112 changes: 0 additions & 112 deletions src/Component/tests/spec/Symfony/EventListener/RespondListenerSpec.php

This file was deleted.

0 comments on commit 1a7dcaf

Please sign in to comment.