Skip to content

Commit

Permalink
[PriceHistory][API] Add behat context
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Mar 17, 2023
1 parent f774da1 commit 23d616c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\Api\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webmozart\Assert\Assert;

final class ChannelPricingLogEntryContext implements Context
{
public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private SharedStorageInterface $sharedStorage,
) {
}

/**
* @When /^I go to the price history of a (variant with code "[^"]+")$/
*/
public function iGoToThePriceHistoryOfAVariant(ProductVariantInterface $productVariant): void
{
$channel = $this->sharedStorage->get('channel');
Assert::notNull($channel);

$this->sharedStorage->set('variant', $productVariant);

$this->client->index(Resources::CHANNEL_PRICING_LOG_ENTRY);
$this->client->addFilter('channelPricing.channelCode', $channel->getCode());
$this->client->addFilter('channelPricing.productVariant.code', $productVariant->getCode());
$this->client->filter();
}

/**
* @Then I should see :count log entries in the catalog price history
* @Then I should see a single log entry in the catalog price history
*/
public function iShouldSeeLogEntriesInTheCatalogPriceHistoryForTheVariant(int $count = 1): void
{
Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count);
}

/**
* @Then /^there should be a log entry on the (\d+)(?:|st|nd|rd|th) position with the ("[^"]+") selling price, (no|"[^"]+") original price and datetime of the price change$/
*/
public function thereShouldBeALogEntryOnThePositionWithTheSellingPriceOriginalPriceAndDatetimeOfThePriceChange(
int $position,
int $price,
int|string $originalPrice,
): void {
if ('no' === $originalPrice) {
$originalPrice = null;
}

$logEntry = $this->responseChecker->getCollection($this->client->getLastResponse())[$position - 1];

Assert::same($logEntry['price'], $price);
Assert::same($logEntry['originalPrice'], $originalPrice);
Assert::keyExists($logEntry, 'loggedAt');
}

/**
* @Then /^there should be a log entry with the ("[^"]+") selling price, (no|"[^"]+") original price and datetime of the price change$/
*/
public function thereShouldBeALogEntryWithTheSellingPriceOriginalPriceAndDatetimeOfThePriceChange(
int $price,
int|string $originalPrice,
): void {
$this->thereShouldBeALogEntryOnThePositionWithTheSellingPriceOriginalPriceAndDatetimeOfThePriceChange(
1,
$price,
$originalPrice,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public function iSetItsPriceToForChannel(int $price, ChannelInterface $channel):
]);
}

/**
* @When /^I set its original price to ("[^"]+") for ("[^"]+" channel)$/
*/
public function iSetItsOriginalPriceToForChannel(int $originalPrice, ChannelInterface $channel): void
{
$this->client->addRequestData('channelPricings', [
$channel->getCode() => [
'originalPrice' => $originalPrice,
'channelCode' => $channel->getCode(),
],
]);
}

/**
* @When /^I set its minimum price to ("[^"]+") for ("[^"]+" channel)$/
*/
Expand Down Expand Up @@ -126,6 +139,27 @@ public function iCreateANewVariantPricedAtForProductInTheChannel(
$this->createNewVariantWithPrice($name, $price, $product, $channel);
}

/**
* @When I want to modify the :variant product variant
*/
public function iWantToModifyProductVariant(ProductVariantInterface $variant): void
{
$this->client->buildUpdateRequest(Resources::PRODUCT_VARIANTS, $variant->getCode());
}

/**
* @When /^I change its price to ("[^"]+") for ("[^"]+" channel)$/
*/
public function iChangeItsPriceToForChannel(int $originalPrice, ChannelInterface $channel): void
{
$this->client->addRequestData('channelPricings', [
$channel->getCode() => [
'price' => $originalPrice,
'channelCode' => $channel->getCode(),
],
]);
}

/**
* @Then I should be notified that it has been successfully created
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Context/Api/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class Resources

public const CATALOG_PROMOTIONS = 'catalog-promotions';

public const CHANNEL_PRICING_LOG_ENTRY = 'channel-pricing-log-entries';

public const CHANNELS = 'channels';

public const CONTACT_REQUESTS = 'contact-requests';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,11 @@
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument>%sylius.security.new_api_route%</argument>
</service>

<service id="Sylius\Behat\Context\Api\Admin\ChannelPricingLogEntryContext" public="true">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ default:
- sylius.behat.context.setup.product
- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.taxonomy
- Sylius\Behat\Context\Setup\CatalogPromotionContext

- sylius.behat.context.api.admin.managing_product_variants
- Sylius\Behat\Context\Api\Admin\ChannelPricingLogEntryContext
filters:
tags: "@managing_product_variants&&@api"
javascript: false

0 comments on commit 23d616c

Please sign in to comment.