|
| 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 | +} |
0 commit comments