Skip to content

Commit

Permalink
PHPUnit tests for context
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Dec 6, 2023
1 parent fa9c888 commit dd163ca
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 154 deletions.
80 changes: 80 additions & 0 deletions src/Component/tests/Context/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\Context;

use PHPUnit\Framework\TestCase;
use Sylius\Component\Resource\Tests\Dummy\DummyClassOne;
use Sylius\Component\Resource\Tests\Dummy\DummyClassTwo;
use Sylius\Resource\Context\Context;

final class ContextTest extends TestCase
{
/** @test */
public function it_is_an_iterator(): void
{
$context = new Context();
$this->assertInstanceOf(\IteratorAggregate::class, $context);
}

/** @test */
public function it_can_be_constructed_with_options(): void
{
$optionOne = new DummyClassOne();
$optionTwo = new DummyClassTwo();

$context = new Context($optionOne, $optionTwo);

$this->assertEquals($optionOne, $context->get(DummyClassOne::class));
$this->assertEquals($optionTwo, $context->get(DummyClassTwo::class));
}

/** @test */
public function it_can_be_with_options(): void
{
$optionOne = new DummyClassOne();
$optionTwo = new DummyClassTwo();

$context = new Context();
$context = $context->with($optionOne, $optionTwo);

$this->assertEquals($optionOne, $context->get(DummyClassOne::class));
$this->assertEquals($optionTwo, $context->get(DummyClassTwo::class));
}

/** @test */
public function it_can_be_without_options(): void
{
$optionOne = new DummyClassOne();
$optionTwo = new DummyClassTwo();

$context = new Context();
$context = $context->with($optionOne, $optionTwo);
$context = $context->without(DummyClassOne::class, DummyClassTwo::class);

$this->assertNull($context->get(DummyClassOne::class));
$this->assertNull($context->get(DummyClassTwo::class));
}

/** @test */
public function it_can_be_iterated(): void
{
$optionOne = new DummyClassOne();
$optionTwo = new DummyClassTwo();

$context = new Context($optionOne, $optionTwo);

$this->assertEquals($optionOne, $context->getIterator()->current());
$this->assertEquals(2, iterator_count($context->getIterator()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Context\Initiator;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Context\Initiator\RequestContextInitiator;
use Sylius\Resource\Context\Option\RequestOption;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

final class RequestContextInitiatorTest extends TestCase
{
use ProphecyTrait;

private RequestContextInitiator $requestContextInitiator;

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

/** @test */
public function it_initializes_context(): void
{
$request = $this->prophesize(Request::class);

$request->attributes = new ParameterBag(['_sylius' => ['resource_class' => 'App\Resource']]);

$context = $this->requestContextInitiator->initializeContext($request->reveal());

$this->assertInstanceOf(Context::class, $context);
$this->assertEquals($request->reveal(), $context->get(RequestOption::class)?->request());
}
}
42 changes: 42 additions & 0 deletions src/Component/tests/Context/Option/MetadataOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Context\Option;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Context\Option\MetadataOption;

final class MetadataOptionTest extends TestCase
{
use ProphecyTrait;

private MetadataInterface|ObjectProphecy $metadata;

private MetadataOption $metadataOption;

protected function setUp(): void
{
$this->metadata = $this->prophesize(MetadataInterface::class);

$this->metadataOption = new MetadataOption($this->metadata->reveal());
}

/** @test */
public function it_returns_request_configuration(): void
{
$this->assertEquals($this->metadata->reveal(), $this->metadataOption->metadata());
}
}
42 changes: 42 additions & 0 deletions src/Component/tests/Context/Option/RequestOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Context\Option;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Resource\Context\Option\RequestOption;
use Symfony\Component\HttpFoundation\Request;

final class RequestOptionTest extends TestCase
{
use ProphecyTrait;

private Request|ObjectProphecy $request;

private RequestOption $requestOption;

protected function setUp(): void
{
$this->request = $this->prophesize(Request::class);

$this->requestOption = new RequestOption($this->request->reveal());
}

/** @test */
public function it_contains_request(): void
{
$this->assertEquals($this->request->reveal(), $this->requestOption->request());
}
}
79 changes: 0 additions & 79 deletions src/Component/tests/spec/Context/ContextSpec.php

This file was deleted.

This file was deleted.

36 changes: 0 additions & 36 deletions src/Component/tests/spec/Context/Option/RequestOptionSpec.php

This file was deleted.

0 comments on commit dd163ca

Please sign in to comment.