Skip to content

Commit b327848

Browse files
committed
Add tests
1 parent 478dab2 commit b327848

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"require-dev": {
2121
"infection/infection": "^0.27.11",
2222
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.1",
23+
"phpspec/prophecy-phpunit": "^2.3",
2324
"phpunit/phpunit": "^9.6",
2425
"psalm/plugin-phpunit": "^0.19",
2526
"psalm/plugin-symfony": "^5.2",
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusStaticContextsBundle\Tests\Context;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Prophecy\Prophecy\ObjectProphecy;
10+
use Setono\SyliusStaticContextsBundle\Context\StaticChannelContext;
11+
use Sylius\Component\Channel\Context\ChannelNotFoundException;
12+
use Sylius\Component\Channel\Model\ChannelInterface;
13+
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
14+
15+
final class StaticChannelContextTest extends TestCase
16+
{
17+
use ProphecyTrait;
18+
19+
/** @var ObjectProphecy<ChannelRepositoryInterface> */
20+
private ObjectProphecy $channelRepository;
21+
22+
private StaticChannelContext $context;
23+
24+
protected function setUp(): void
25+
{
26+
$this->channelRepository = $this->prophesize(ChannelRepositoryInterface::class);
27+
$this->context = new StaticChannelContext($this->channelRepository->reveal());
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function get_channel_throws_exception_when_channel_is_not_set(): void
34+
{
35+
$this->expectException(ChannelNotFoundException::class);
36+
$this->expectExceptionMessage('Static channel is not set');
37+
38+
$this->context->getChannel();
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function set_channel_and_get_channel(): void
45+
{
46+
$channel = $this->prophesize(ChannelInterface::class)->reveal();
47+
48+
$this->context->setChannel($channel);
49+
50+
$this->assertSame($channel, $this->context->getChannel());
51+
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function set_channel_code_throws_exception_if_channel_does_not_exist(): void
57+
{
58+
$this->channelRepository->findOneByCode('non_existing_code')->willReturn(null);
59+
60+
$this->expectException(ChannelNotFoundException::class);
61+
$this->expectExceptionMessage('Channel with code "non_existing_code" does not exist');
62+
63+
$this->context->setChannelCode('non_existing_code');
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function set_channel_code_successfully_sets_channel(): void
70+
{
71+
$channel = $this->prophesize(ChannelInterface::class)->reveal();
72+
73+
$this->channelRepository->findOneByCode('existing_code')->willReturn($channel);
74+
75+
$this->context->setChannelCode('existing_code');
76+
77+
$this->assertSame($channel, $this->context->getChannel());
78+
}
79+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusStaticContextsBundle\Tests\Context;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusStaticContextsBundle\Context\StaticLocaleContext;
10+
use Sylius\Component\Locale\Context\LocaleNotFoundException;
11+
12+
final class StaticLocaleContextTest extends TestCase
13+
{
14+
use ProphecyTrait;
15+
16+
/**
17+
* @test
18+
*/
19+
public function get_locale_code_throws_exception_when_locale_is_not_set(): void
20+
{
21+
$context = new StaticLocaleContext();
22+
23+
$this->expectException(LocaleNotFoundException::class);
24+
$this->expectExceptionMessage('Static locale code is not set');
25+
26+
$context->getLocaleCode();
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function set_locale_code_and_get_locale_code(): void
33+
{
34+
$localeCode = 'en_US';
35+
36+
$context = new StaticLocaleContext();
37+
$context->setLocaleCode($localeCode);
38+
39+
$this->assertSame($localeCode, $context->getLocaleCode());
40+
}
41+
}

0 commit comments

Comments
 (0)