|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\SyliusLoyaltyPlugin\Tests\Functional; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Doctrine\Persistence\ObjectRepository; |
| 9 | +use Setono\SyliusLoyaltyPlugin\Earning\TriggerChannelResolverInterface; |
| 10 | +use Setono\SyliusLoyaltyPlugin\Model\LoyaltyAccount; |
| 11 | +use Sylius\Component\Core\Model\ChannelInterface; |
| 12 | +use Sylius\Component\Core\Model\CustomerInterface; |
| 13 | +use Sylius\Component\Currency\Model\CurrencyInterface; |
| 14 | +use Sylius\Component\Locale\Model\LocaleInterface; |
| 15 | +use Sylius\Component\Resource\Factory\FactoryInterface; |
| 16 | +use Sylius\Component\Resource\Model\ResourceInterface; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 18 | + |
| 19 | +final class DefaultTriggerChannelResolverTest extends KernelTestCase |
| 20 | +{ |
| 21 | + private EntityManagerInterface $manager; |
| 22 | + |
| 23 | + private TriggerChannelResolverInterface $resolver; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + self::bootKernel(); |
| 28 | + |
| 29 | + $manager = self::getContainer()->get('doctrine.orm.entity_manager'); |
| 30 | + \assert($manager instanceof EntityManagerInterface); |
| 31 | + $this->manager = $manager; |
| 32 | + |
| 33 | + $resolver = self::getContainer()->get('Setono\SyliusLoyaltyPlugin\Earning\DefaultTriggerChannelResolver'); |
| 34 | + \assert($resolver instanceof TriggerChannelResolverInterface); |
| 35 | + $this->resolver = $resolver; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @test |
| 40 | + */ |
| 41 | + public function it_resolves_the_channels_where_the_customer_has_an_account(): void |
| 42 | + { |
| 43 | + $customer = $this->createCustomer('resolver@example.com'); |
| 44 | + $this->createAccount($customer, $this->createChannel('resolver-a')); |
| 45 | + $this->createAccount($customer, $this->createChannel('resolver-b')); |
| 46 | + |
| 47 | + $codes = []; |
| 48 | + foreach ($this->resolver->resolve($customer) as $channel) { |
| 49 | + $codes[] = $channel->getCode(); |
| 50 | + } |
| 51 | + |
| 52 | + self::assertCount(2, $codes); |
| 53 | + self::assertContains('resolver-a', $codes); |
| 54 | + self::assertContains('resolver-b', $codes); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + */ |
| 60 | + public function it_resolves_nothing_for_a_customer_without_an_account(): void |
| 61 | + { |
| 62 | + $customer = $this->createCustomer('resolver-empty@example.com'); |
| 63 | + |
| 64 | + $channels = []; |
| 65 | + foreach ($this->resolver->resolve($customer) as $channel) { |
| 66 | + $channels[] = $channel; |
| 67 | + } |
| 68 | + |
| 69 | + self::assertCount(0, $channels); |
| 70 | + } |
| 71 | + |
| 72 | + private function createAccount(CustomerInterface $customer, ChannelInterface $channel): void |
| 73 | + { |
| 74 | + $account = new LoyaltyAccount(); |
| 75 | + $account->setCustomer($customer); |
| 76 | + $account->setChannel($channel); |
| 77 | + $this->manager->persist($account); |
| 78 | + $this->manager->flush(); |
| 79 | + } |
| 80 | + |
| 81 | + private function createChannel(string $code): ChannelInterface |
| 82 | + { |
| 83 | + $currency = $this->getOrCreateByCode('sylius.repository.currency', 'sylius.factory.currency', 'USD', CurrencyInterface::class); |
| 84 | + \assert($currency instanceof CurrencyInterface); |
| 85 | + $locale = $this->getOrCreateByCode('sylius.repository.locale', 'sylius.factory.locale', 'en_US', LocaleInterface::class); |
| 86 | + \assert($locale instanceof LocaleInterface); |
| 87 | + |
| 88 | + $channel = $this->factory('sylius.factory.channel')->createNew(); |
| 89 | + \assert($channel instanceof ChannelInterface); |
| 90 | + $channel->setCode($code); |
| 91 | + $channel->setName('Web'); |
| 92 | + $channel->setBaseCurrency($currency); |
| 93 | + $channel->setDefaultLocale($locale); |
| 94 | + $channel->addCurrency($currency); |
| 95 | + $channel->addLocale($locale); |
| 96 | + $this->manager->persist($channel); |
| 97 | + $this->manager->flush(); |
| 98 | + |
| 99 | + return $channel; |
| 100 | + } |
| 101 | + |
| 102 | + private function createCustomer(string $email): CustomerInterface |
| 103 | + { |
| 104 | + $customer = $this->factory('sylius.factory.customer')->createNew(); |
| 105 | + \assert($customer instanceof CustomerInterface); |
| 106 | + $customer->setEmail($email); |
| 107 | + $this->manager->persist($customer); |
| 108 | + $this->manager->flush(); |
| 109 | + |
| 110 | + return $customer; |
| 111 | + } |
| 112 | + |
| 113 | + private function getOrCreateByCode(string $repositoryId, string $factoryId, string $code, string $type): ResourceInterface |
| 114 | + { |
| 115 | + $repository = self::getContainer()->get($repositoryId); |
| 116 | + \assert($repository instanceof ObjectRepository); |
| 117 | + |
| 118 | + $resource = $repository->findOneBy(['code' => $code]); |
| 119 | + if ($resource instanceof $type) { |
| 120 | + \assert($resource instanceof ResourceInterface); |
| 121 | + |
| 122 | + return $resource; |
| 123 | + } |
| 124 | + |
| 125 | + $resource = $this->factory($factoryId)->createNew(); |
| 126 | + \assert(method_exists($resource, 'setCode')); |
| 127 | + $resource->setCode($code); |
| 128 | + $this->manager->persist($resource); |
| 129 | + |
| 130 | + return $resource; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return FactoryInterface<ResourceInterface> |
| 135 | + */ |
| 136 | + private function factory(string $id): FactoryInterface |
| 137 | + { |
| 138 | + $factory = self::getContainer()->get($id); |
| 139 | + \assert($factory instanceof FactoryInterface); |
| 140 | + |
| 141 | + return $factory; |
| 142 | + } |
| 143 | +} |
0 commit comments