Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpunit tests for Symfony form listener #791

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/Component/Tests/State/Processor/FlashProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?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\State\Processor;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Context\Option\RequestOption;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\State\Processor\FlashProcessor;
use Sylius\Resource\State\ProcessorInterface;
use Sylius\Resource\Symfony\Session\Flash\FlashHelperInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class FlashProcessorTest extends TestCase
{
use ProphecyTrait;

private ProcessorInterface|ObjectProphecy $decorated;

private FlashHelperInterface|ObjectProphecy $flashHelper;

private FlashProcessor $flashProcessor;

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

$this->flashProcessor = new FlashProcessor(
$this->decorated->reveal(),
$this->flashHelper->reveal(),
);
}

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

$request->isMethodSafe()->willReturn(false);

$request->attributes = $attributes;

$attributes->getBoolean('is_valid', true)->willReturn(true);

$context = new Context(new RequestOption($request->reveal()));

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

$this->flashHelper->addSuccessFlash($operation, $context)->shouldBeCalled();

$this->flashProcessor->process(['foo' => 'fighters'], $operation->reveal(), $context);
}

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

$request->isMethodSafe()->willReturn(false)->shouldNotBeCalled();

$request->attributes = $attributes;

$attributes->getBoolean('is_valid', true)->willReturn(true)->shouldNotBeCalled();

$context = new Context(new RequestOption($request->reveal()));

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

$this->flashHelper->addSuccessFlash($operation, $context)->shouldNotBeCalled();

$this->flashProcessor->process($response->reveal(), $operation->reveal(), $context);
}

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

$request->isMethodSafe()->willReturn(true)->shouldBeCalled();

$request->attributes = $attributes;

$attributes->getBoolean('is_valid', true)->willReturn(true);

$context = new Context(new RequestOption($request->reveal()));

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

$this->flashHelper->addSuccessFlash($operation, $context)->shouldNotBeCalled();

$this->flashProcessor->process(['foo' => 'fighters'], $operation->reveal(), $context);
}

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

$request->isMethodSafe()->willReturn(false);

$request->attributes = $attributes;

$attributes->getBoolean('is_valid', true)->willReturn(false)->shouldBeCalled();

$context = new Context(new RequestOption($request->reveal()));

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

$this->flashHelper->addSuccessFlash($operation, $context)->shouldNotBeCalled();

$this->flashProcessor->process(['foo' => 'fighters'], $operation->reveal(), $context);
}
}
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());
}
}
Loading
Loading