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

[phpspec-2-phpunit] migration of tests (Twig) #953

Open
wants to merge 2 commits into
base: 1.13
Choose a base branch
from
Open
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
97 changes: 0 additions & 97 deletions src/Component/spec/Twig/Context/Factory/ContextFactorySpec.php

This file was deleted.

95 changes: 95 additions & 0 deletions src/Component/tests/Twig/Context/Factory/ContextFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\Resource\Tests\Twig\Context\Factory;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Twig\Context\Factory\ContextFactory;
use Sylius\Resource\Twig\Context\Factory\ContextFactoryInterface;

final class ContextFactoryTest extends TestCase
{
use ProphecyTrait;

private ContextFactory $contextFactory;

private ContainerInterface|ObjectProphecy $locator;

protected function setUp(): void
{
$this->locator = $this->prophesize(ContainerInterface::class);
$this->contextFactory = new ContextFactory($this->locator->reveal());
}

public function testItIsInitializable(): void
{
$this->assertInstanceOf(ContextFactory::class, $this->contextFactory);
}

public function testItCreatesTwigContextFromOperationTwigContextFactoryAsCallable(): void
{
$data = new \stdClass();
$twigContextFactory = [TwigContextFactoryCallable::class, 'create'];
$operation = new Show(twigContextFactory: $twigContextFactory);
$context = new Context();

$result = $this->contextFactory->create($data, $operation, $context);

$this->assertSame(['foo' => 'bar'], $result);
}

public function testItCreatesTwigContextFromOperationTwigContextFactoryAsString(): void
{
$data = new \stdClass();
$twigContextFactory = $this->prophesize(ContextFactoryInterface::class);
$operation = new Show(twigContextFactory: $twigContextFactory::class);
$context = new Context();

$this->locator->has($twigContextFactory::class)->willReturn(true);
$this->locator->get($twigContextFactory::class)->willReturn($twigContextFactory->reveal());

$twigContextFactory->create($data, $operation, $context)->willReturn(['foo' => 'bar']);

$result = $this->contextFactory->create($data, $operation, $context);

$this->assertSame(['foo' => 'bar'], $result);
}

public function testItThrowsExceptionWhenTwigContextFactoryNotFoundOnLocator(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(sprintf('Twig context factory "%s" not found on operation "app_dummy_show"', ContextFactoryInterface::class));

$data = new \stdClass();
$operation = new Show(name: 'app_dummy_show', twigContextFactory: ContextFactoryInterface::class);
$context = new Context();

$this->locator->has(ContextFactoryInterface::class)->willReturn(false);

$this->contextFactory->create($data, $operation, $context);
}
}

final class TwigContextFactoryCallable
{
public static function create(mixed $data, Operation $operation, Context $context): array
{
return ['foo' => 'bar'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,60 @@

declare(strict_types=1);

namespace spec\Sylius\Resource\Twig\Context\Factory;
namespace Sylius\Resource\Tests\Twig\Context\Factory;

use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\TestCase;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Twig\Context\Factory\DefaultContextFactory;

final class DefaultContextFactorySpec extends ObjectBehavior
final class DefaultContextFactoryTest extends TestCase
{
function it_is_initializable(): void
private DefaultContextFactory $defaultContextFactory;

protected function setUp(): void
{
$this->defaultContextFactory = new DefaultContextFactory();
}

public function testItIsInitializable(): void
{
$this->shouldHaveType(DefaultContextFactory::class);
$this->assertInstanceOf(DefaultContextFactory::class, $this->defaultContextFactory);
}

function it_creates_twig_context_for_resource(
\stdClass $data,
): void {
public function testItCreatesTwigContextForResource(): void
{
$data = new \stdClass();
$resourceMetadata = new ResourceMetadata(alias: 'app.dummy', name: 'dummy');
$operation = (new Show())->withResource($resourceMetadata);
$context = new Context();

$result = $this->defaultContextFactory->create($data, $operation, $context);

$this->create($data, $operation, new Context())->shouldReturn([
$this->assertSame([
'operation' => $operation,
'resource_metadata' => $resourceMetadata,
'resource' => $data,
'dummy' => $data,
]);
], $result);
}

function it_creates_twig_context_for_resource_collection(
\stdClass $data,
): void {
public function testItCreatesTwigContextForResourceCollection(): void
{
$data = new \stdClass();
$resourceMetadata = new ResourceMetadata(alias: 'app.dummy', pluralName: 'dummies');
$operation = (new Index())->withResource($resourceMetadata);
$context = new Context();

$result = $this->defaultContextFactory->create($data, $operation, $context);

$this->create($data, $operation, new Context())->shouldReturn([
$this->assertSame([
'operation' => $operation,
'resource_metadata' => $resourceMetadata,
'resources' => $data,
'dummies' => $data,
]);
], $result);
}
}
Loading