Skip to content

Commit

Permalink
move context to productReviewContext
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Mar 30, 2021
1 parent 8e74ab7 commit d71210c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 65 deletions.
63 changes: 1 addition & 62 deletions src/Sylius/Behat/Context/Api/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@

namespace Sylius\Behat\Context\Api\Shop;

use ApiPlatform\Core\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\Request;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
Expand All @@ -34,27 +32,12 @@ final class ProductContext implements Context
/** @var ResponseCheckerInterface */
private $responseChecker;

/** @var SharedStorageInterface */
private $sharedStorage;

/** @var ApiClientInterface */
private $productReviewClient;

/** @var IriConverterInterface */
private $iriConverter;

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

/**
Expand Down Expand Up @@ -94,39 +77,6 @@ public function iSearchForProductsWithName(string $name)
$this->client->filter();
}

/**
* @Then I should see :amount product reviews
*/
public function iShouldSeeProductReviews(int $amount): void
{
/** @var ProductInterface $product */
$product = $this->sharedStorage->get('product');

$this->productReviewClient->index();
$this->productReviewClient->addFilter('reviewSubject', $this->iriConverter->getIriFromItem($product));
$this->productReviewClient->addFilter('itemsPerPage', 3);
$this->productReviewClient->addFilter('order[createdAt]', 'desc');
$this->productReviewClient->filter();

Assert::same($this->responseChecker->countCollectionItems($this->productReviewClient->getLastResponse()), $amount);
}

/**
* @Then I should see reviews titled :titleOne, :titleTwo and :titleThree
*/
public function iShouldSeeReviewsTitledAnd(string ...$titles): void
{
Assert::true($this->hasReviewsWithTitles($titles));
}

/**
* @Then I should not see review titled :title
*/
public function iShouldNotSeeReviewTitled(string $title): void
{
Assert::false($this->hasReviewsWithTitles([$title]));
}

/**
* @Then I should see the product :name
*/
Expand Down Expand Up @@ -248,15 +198,4 @@ private function hasProductWithName(array $products, string $name): bool

return false;
}

private function hasReviewsWithTitles(array $titles): bool
{
foreach ($titles as $title) {
if (!$this->responseChecker->hasItemWithValue($this->productReviewClient->getLastResponse(), 'title', $title)) {
return false;
}
}

return true;
}
}
44 changes: 44 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/ProductReviewContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ public function iLeaveACommentTitled(string $comment, string $title, ?string $em
$this->client->addRequestData('email', $email);
}

/**
* @Then I should see :amount product reviews
*/
public function iShouldSeeProductReviews(int $amount = 0): void
{
/** @var ProductInterface $product */
$product = $this->sharedStorage->get('product');

$this->client->index();
$this->client->addFilter('reviewSubject', $this->iriConverter->getIriFromItem($product));
$this->client->addFilter('itemsPerPage', 3);
$this->client->addFilter('order[createdAt]', 'desc');
$this->client->filter();

Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $amount);
}

/**
* @Then I should see reviews titled :titleOne, :titleTwo and :titleThree
*/
public function iShouldSeeReviewsTitledAnd(string ...$titles): void
{
Assert::true($this->hasReviewsWithTitles($titles));
}

/**
* @Then I should not see review titled :title
*/
public function iShouldNotSeeReviewTitled(string $title): void
{
Assert::false($this->hasReviewsWithTitles([$title]));
}

/**
* @When I rate it with :rating point(s)
*/
Expand Down Expand Up @@ -126,4 +159,15 @@ public function iShouldNotSeeReviewTitledInTheList(string $title): void
{
Assert::isEmpty($this->responseChecker->getCollectionItemsWithValue($this->client->getLastResponse(), 'title', $title));
}

private function hasReviewsWithTitles(array $titles): bool
{
foreach ($titles as $title) {
if (!$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'title', $title)) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@
<service id="sylius.behat.context.api.shop.product" class="Sylius\Behat\Context\Api\Shop\ProductContext">
<argument type="service" id="sylius.behat.api_platform_client.shop.product" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.behat.api_platform_client.shop.product_review" />
<argument type="service" id="api_platform.iri_converter" />
</service>

<service id="sylius.behat.context.api.shop.product_review" class="Sylius\Behat\Context\Api\Shop\ProductReviewContext">
Expand Down

0 comments on commit d71210c

Please sign in to comment.