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

[CatalogPromotion][API] Add channels to catalog promotion #13009

Merged
merged 3 commits into from
Aug 26, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ Feature: Creating a catalog promotion
Then there should be 1 new catalog promotion on the list
And it should have "winter_sale" code and "Winter sale" name
And this catalog promotion should be usable

@api
Scenario: Creating a catalog promotion for channel
When I want to create a new catalog promotion
And I specify its code as "winter_sale"
And I name it "Winter sale"
And I make it available in channel "United States"
And I add it
Then there should be 1 new catalog promotion on the list
And it should have "winter_sale" code and "Winter sale" name
And the catalog promotion "Winter sale" should be available in channel "United States"
And this catalog promotion should be usable
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace Sylius\Behat\Context\Api\Admin;

use ApiPlatform\Core\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Promotion\Event\CatalogPromotionUpdated;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;
Expand All @@ -25,15 +28,18 @@ final class ManagingCatalogPromotionsContext implements Context
private ApiClientInterface $client;
private ResponseCheckerInterface $responseChecker;
private MessageBusInterface $messageBus;
private IriConverterInterface $iriConverter;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
MessageBusInterface $messageBus
MessageBusInterface $messageBus,
IriConverterInterface $iriConverter
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->messageBus = $messageBus;
$this->iriConverter = $iriConverter;
}

/**
Expand Down Expand Up @@ -86,6 +92,14 @@ public function iDescribeItAsIn(string $description, string $localeCode): void
$this->client->updateRequestData($data);
}

/**
* @When I make it available in channel :channel
*/
public function iMakeItAvailableInChannel(ChannelInterface $channel): void
{
$this->client->addRequestData('channels', [$this->iriConverter->getIriFromItem($channel)]);
}

/**
* @When I add it
*/
Expand Down Expand Up @@ -120,4 +134,21 @@ public function thisCatalogPromotionShouldBeUsable(): void
{
Assert::isInstanceOf($this->messageBus->getDispatchedMessages()[0]['message'], CatalogPromotionUpdated::class);
}

/**
* @Then the catalog promotion :catalogPromotion should be available in channel :channel
*/
public function itShouldBeAvailableInChannel(
CatalogPromotionInterface $catalogPromotion,
ChannelInterface $channel
): void {
Assert::true(
$this->responseChecker->hasValueInCollection(
$this->client->show($catalogPromotion->getCode()),
'channels',
$this->iriConverter->getIriFromItem($channel)
),
sprintf('Catalog promotion is not assigned to %s channel', $channel->getName())
);
}
}
40 changes: 40 additions & 0 deletions src/Sylius/Behat/Context/Transform/CatalogPromotionContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Behat\Context\Transform;

use Behat\Behat\Context\Context;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;

final class CatalogPromotionContext implements Context
{
private RepositoryInterface $catalogPromotionRepository;

public function __construct(RepositoryInterface $catalogPromotionRepository)
{
$this->catalogPromotionRepository = $catalogPromotionRepository;
}

/**
* @Transform :catalogPromotion
*/
public function getCatalogPromotionByName(string $name): CatalogPromotionInterface
{
$catalogPromotion = $this->catalogPromotionRepository->findOneBy(['name' => $name]);
Assert::notNull($catalogPromotion);

return $catalogPromotion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<argument type="service" id="sylius.behat.api_platform_client.admin.catalog_promotion" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.event_bus" />
<argument type="service" id="api_platform.iri_converter" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,9 @@
<argument type="service" id="sylius.repository.zone" />
<argument type="service" id="sylius.repository.zone_member" />
</service>

<service id="Sylius\Behat\Context\Transform\CatalogPromotionContext">
<argument type="service" id="sylius.repository.catalog_promotion" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ default:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.channel
- sylius.behat.context.transform.shared_storage
- Sylius\Behat\Context\Transform\CatalogPromotionContext

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.admin_api_security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<property name="id" identifier="false" writable="false" />
<property name="code" identifier="true" />
<property name="name" writable="true" />
<property name="channels" writable="true" />
<property name="translations" readable="true" writable="true">
<attribute name="openapi_context">
<attribute name="type">object</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<group>admin:catalog_promotion:update</group>
</attribute>

<attribute name="channels">
<group>admin:catalog_promotion:read</group>
<group>admin:catalog_promotion:create</group>
</attribute>

<attribute name="translations">
<group>admin:catalog_promotion:read</group>
<group>admin:catalog_promotion:create</group>
Expand Down
28 changes: 28 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Migrations/Version20210826054355.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Sylius\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210826054355 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add channels to catalog promotion';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE sylius_catalog_promotion_channels (catalog_promotion_id INT NOT NULL, channel_id INT NOT NULL, INDEX IDX_48E9AE7622E2CB5A (catalog_promotion_id), INDEX IDX_48E9AE7672F5A1AA (channel_id), PRIMARY KEY(catalog_promotion_id, channel_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE sylius_catalog_promotion_channels ADD CONSTRAINT FK_48E9AE7622E2CB5A FOREIGN KEY (catalog_promotion_id) REFERENCES sylius_catalog_promotion (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE sylius_catalog_promotion_channels ADD CONSTRAINT FK_48E9AE7672F5A1AA FOREIGN KEY (channel_id) REFERENCES sylius_channel (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE sylius_catalog_promotion_channels');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

sylius_promotion:
resources:
catalog_promotion:
classes:
model: Sylius\Component\Core\Model\CatalogPromotion
promotion_subject:
classes:
model: "%sylius.model.order.class%"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<mapped-superclass name="Sylius\Component\Core\Model\CatalogPromotion" table="sylius_catalog_promotion">
<many-to-many field="channels" target-entity="Sylius\Component\Channel\Model\ChannelInterface">
<join-table name="sylius_catalog_promotion_channels">
<join-columns>
<join-column name="catalog_promotion_id" nullable="false" on-delete="CASCADE" />
</join-columns>
<inverse-join-columns>
<join-column name="channel_id" nullable="false" on-delete="CASCADE" />
</inverse-join-columns>
</join-table>
</many-to-many>
</mapped-superclass>
</doctrine-mapping>
70 changes: 70 additions & 0 deletions src/Sylius/Component/Core/Model/CatalogPromotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Component\Core\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface;
use Sylius\Component\Promotion\Model\CatalogPromotion as BaseCatalogPromotion;
use Sylius\Component\Promotion\Model\CatalogPromotionTranslation;

class CatalogPromotion extends BaseCatalogPromotion implements CatalogPromotionInterface
{
/**
* @var Collection<array-key, ChannelInterface>
*
* @psalm-var Collection<array-key, ChannelInterface>
*/
protected Collection $channels;

public function __construct()
{
parent::__construct();

$this->channels = new ArrayCollection();
}

/**
* @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220
* @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220
*/
public function getChannels(): Collection
{
return $this->channels;
}

public function hasChannel(BaseChannelInterface $channel): bool
{
return $this->channels->contains($channel);
}

public function addChannel(BaseChannelInterface $channel): void
{
if (!$this->hasChannel($channel)) {
$this->channels->add($channel);
}
}

public function removeChannel(BaseChannelInterface $channel): void
{
if ($this->hasChannel($channel)) {
$this->channels->removeElement($channel);
}
}

public static function getTranslationClass(): string
{
return CatalogPromotionTranslation::class;
}
}
21 changes: 21 additions & 0 deletions src/Sylius/Component/Core/Model/CatalogPromotionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Component\Core\Model;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionInterface as BaseCatalogPromotionInterface;

interface CatalogPromotionInterface extends BaseCatalogPromotionInterface, ChannelsAwareInterface
{
}
43 changes: 43 additions & 0 deletions src/Sylius/Component/Core/spec/Model/CatalogPromotionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Component\Core\Model;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelInterface;

final class CatalogPromotionSpec extends ObjectBehavior
{
function it_implements_a_catalog_promotion_interface(): void
{
$this->shouldImplement(CatalogPromotionInterface::class);
}

function it_has_channels_collection(ChannelInterface $firstChannel, ChannelInterface $secondChannel): void
{
$this->addChannel($firstChannel);
$this->addChannel($secondChannel);

$this->getChannels()->shouldIterateAs([$firstChannel, $secondChannel]);
}

function it_can_add_and_remove_channels(ChannelInterface $channel): void
{
$this->addChannel($channel);
$this->hasChannel($channel)->shouldReturn(true);

$this->removeChannel($channel);
$this->hasChannel($channel)->shouldReturn(false);
}
}
Loading