Skip to content

Commit

Permalink
Add search tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Apr 11, 2024
1 parent 0a08055 commit ee52f56
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 2 deletions.
71 changes: 71 additions & 0 deletions features/product/search_products/search_products.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@search_products
Feature: Search products
In order to find products that fits my needs
As a Visitor
I want to be able to search products from a keyword

Background:
Given the store operates on a channel named "US Channel" with hostname "127.0.0.1"
And that channel allows to shop using "English (United States)" and "Italian (Italy)" locales
And it uses the "English (United States)" locale by default
And the store operates on another channel named "IT Channel" with hostname "shop.it"
And that channel allows to shop using "English (United States)" and "Italian (Italy)" locales
And it uses the "Italian (Italy)" locale by default

And the store has a product "T-Shirt Banana" with code "T_SHIRT_BANANA"
And this product is available in "US Channel" channel
And this product is also available in "IT Channel" channel
And this product is named "Maglietta Banana" in the "Italian (Italy)" locale

And the store has a product "T-Shirt Apple" available in "US Channel" channel
And this product is also available in "IT Channel" channel
And this product is named "Maglietta Mela" in the "Italian (Italy)" locale

And the store has a "T-Shirt Orange" configurable product
And this product has a "T-Shirt Orange - Medium size" variant with code "T_SHIRT_ORANGE_M"
And this product is available in "US Channel" channel
And this product is also available in "IT Channel" channel
And this product is named "Maglietta Arancia" in the "Italian (Italy)" locale

And the store is indexed on Elasticsearch
And I am browsing the channel "US Channel"

@ui
Scenario: Search products by code
When I search for "T_SHIRT_BANANA"
Then I should be redirected to the product "T-Shirt Banana" page

@ui
Scenario: Search products by variant code
When I search for "T_SHIRT_ORANGE_M"
Then I should be redirected to the product "T-Shirt Orange" page

@ui
Scenario: Search products by name
When I search for "shirt"
Then I should see "3" results
And I should see the product "T-Shirt Banana"
And I should see the product "T-Shirt Apple"
And I should see the product "T-Shirt Orange"

@ui
Scenario: Search products by name in different locale
Given I am browsing the channel "IT Channel"
When I search for "maglietta"
Then I should see "3" results
And I should see the product "Maglietta Banana"
And I should see the product "Maglietta Mela"
And I should see the product "Maglietta Arancia"

@ui
Scenario: Search products by variant name
When I search for "medium"
Then I should be redirected to the product "T-Shirt Orange" page

@ui
Scenario: Search only enabled products
Given the store has a product "T-Shirt disabled" available in "US Channel" channel
And this product has been disabled
And the store is indexed on Elasticsearch
When I search for "shirt"
Then I should see "3" results
2 changes: 1 addition & 1 deletion src/DocumentType/ProductDocumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private function variantProperties(): array
'on-hold' => $this->integer(false),
'is-tracked' => $this->boolean(false),
'shipping-required' => $this->boolean(false),
'name' => $this->nestedTranslationKeywords(),
'name' => $this->nestedTranslationTexts(),
'price' => [
'type' => 'object',
'dynamic' => false,
Expand Down
4 changes: 3 additions & 1 deletion templates/Search/Result/_main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
{% if paginator|length > 0 %}
<div class="ui three cards" id="products" {{ sylius_test_html_attribute('results') }}>
{% for result in paginator %}
{% include '@SyliusShop/Product/_box.html.twig' with {product: result} %}
<div {{ sylius_test_html_attribute('result') }}>
{% include '@SyliusShop/Product/_box.html.twig' with {product: result} %}
</div>
{% endfor %}
</div>
<div class="ui hidden divider"></div>
Expand Down
16 changes: 16 additions & 0 deletions tests/Behat/Context/Ui/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
namespace Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Context\Ui\Shop;

use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Shop\Product\ShowPageInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Product\IndexPageInterface;
use Webmozart\Assert\Assert;

final readonly class ProductContext implements Context
{
public function __construct(
private IndexPageInterface $indexPage,
private ShowPageInterface $showPage,
private SharedStorageInterface $sharedStorage,
) {
}

Expand Down Expand Up @@ -40,4 +45,15 @@ public function iFilterProductsByWithValue(string $filterName, string $filterVal
{
$this->indexPage->filterBy($filterName, $filterValue);
}

/**
* @Then /^I should be redirected to the (product "[^"]+") page$/
*/
public function iShouldBeRedirectedToTheProductPage(ProductInterface $product): void
{
$currentLocaleCode = $this->sharedStorage->get('current_locale_code');
$productTranslation = $product->getTranslation($currentLocaleCode);

Assert::true($this->showPage->isOpen(['_locale' => $currentLocaleCode, 'slug' => $productTranslation->getSlug()]));
}
}
37 changes: 37 additions & 0 deletions tests/Behat/Context/Ui/Shop/SearchContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Context\Ui\Shop;

use Behat\Behat\Context\Context;
use Sylius\Behat\Service\SharedStorageInterface;
use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Search\ResultsPageInterface;
use Webmozart\Assert\Assert;

final readonly class SearchContext implements Context
{
public function __construct(
private ResultsPageInterface $searchResultPage,
private SharedStorageInterface $sharedStorage,
) {
}

/**
* @When /^I search for "([^"]*)"$/
*/
public function iSearchFor(string $searchTerm): void
{
$currentLocaleCode = $this->sharedStorage->get('current_locale_code');

$this->searchResultPage->tryToOpen(['_locale' => $currentLocaleCode, 'query' => $searchTerm]);
}

/**
* @Then /^I should see "([^"]*)" results$/
*/
public function iShouldSeeResults(int $count): void
{
Assert::eq($this->searchResultPage->countResults(), $count);
}
}
20 changes: 20 additions & 0 deletions tests/Behat/Page/Shop/Search/ResultsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Search;

use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage;

final class ResultsPage extends SymfonyPage implements ResultsPageInterface
{
public function getRouteName(): string
{
return 'sylius_shop_search';
}

public function countResults(): int
{
return count($this->getDocument()->findAll('css', '[data-test-result]'));
}
}
12 changes: 12 additions & 0 deletions tests/Behat/Page/Shop/Search/ResultsPageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Search;

use FriendsOfBehat\PageObjectExtension\Page\PageInterface;

interface ResultsPageInterface extends PageInterface
{
public function countResults(): int;
}
5 changes: 5 additions & 0 deletions tests/Behat/Resources/services/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Product\IndexPage;
use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Page\Shop\Search\ResultsPage;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe.sylius_elasticsearch_plugin.behat.page.shop.product.index', IndexPage::class)
->parent('sylius.behat.page.shop.product.index')
;

$services->set('webgriffe.sylius_elasticsearch_plugin.behat.page.shop.search.results', ResultsPage::class)
->parent('sylius.behat.symfony_page')
;
};
11 changes: 11 additions & 0 deletions tests/Behat/Resources/services/ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Context\Ui\Shop\ProductContext;
use Tests\Webgriffe\SyliusElasticsearchPlugin\Behat\Context\Ui\Shop\SearchContext;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();
Expand All @@ -13,6 +14,16 @@
->public()
->args([
service('webgriffe.sylius_elasticsearch_plugin.behat.page.shop.product.index'),
service('sylius.behat.page.shop.product.show'),
service('sylius.behat.shared_storage'),
])
;

$services->set('webgriffe.sylius_elasticsearch_plugin.behat.context.ui.shop.search', SearchContext::class)
->public()
->args([
service('webgriffe.sylius_elasticsearch_plugin.behat.page.shop.search.results'),
service('sylius.behat.shared_storage'),
])
;
};
1 change: 1 addition & 0 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
imports:
- suites/viewing_products.yaml
- suites/filter_products.yaml
- suites/search_products.yaml
53 changes: 53 additions & 0 deletions tests/Behat/Resources/suites/search_products.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
default:
suites:
ui_search_products:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.hook.session

- sylius.behat.context.transform.channel
- sylius.behat.context.transform.currency
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.locale
- sylius.behat.context.transform.product
- sylius.behat.context.transform.product_association_type
- sylius.behat.context.transform.product_option
- sylius.behat.context.transform.product_variant
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_category
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.taxon
- Sylius\Behat\Context\Transform\CatalogPromotionContext

- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.admin_user
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.locale
- sylius.behat.context.setup.product
- sylius.behat.context.setup.product_association
- sylius.behat.context.setup.product_attribute
- sylius.behat.context.setup.product_review
- sylius.behat.context.setup.product_taxon
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.shipping_category
- sylius.behat.context.setup.taxonomy
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.product_option
- Sylius\Behat\Context\Setup\CatalogPromotionContext
- webgriffe.sylius_elasticsearch_plugin.behat.context.setup.elasticsearch
- webgriffe.sylius_elasticsearch_plugin.behat.context.setup.product_attribute

- sylius.behat.context.ui.admin.managing_product_attributes
- sylius.behat.context.ui.admin.product_showpage
- sylius.behat.context.ui.channel
- sylius.behat.context.ui.shop.locale
- sylius.behat.context.ui.shop.product
- sylius.behat.context.ui.shop.product_attribute
- sylius.behat.context.ui.shop.browsing_product
- webgriffe.sylius_elasticsearch_plugin.behat.context.ui.shop.product
- webgriffe.sylius_elasticsearch_plugin.behat.context.ui.shop.search
filters:
tags: "@search_products&&@ui"

0 comments on commit ee52f56

Please sign in to comment.