Skip to content

Commit 4ca5806

Browse files
committed
Fix behaviour tests
1 parent dff1a6b commit 4ca5806

9 files changed

Lines changed: 61 additions & 11 deletions

File tree

config/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ services:
4545
arguments:
4646
- '@webgriffe.repository.back_in_stock_notification_subscription'
4747
- '@sylius.repository.product_variant'
48+
- '@sylius.context.customer'
4849
tags:
4950
- { name: validator.constraint_validator }

e2e-test-server.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Make sure to set GOOGLE_CHROME env variable pointing to your Google Chrome binary. For example on macOS:
2+
# GOOGLE_CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ./e2e-test-server.sh
3+
4+
trap 'kill %1; kill %2' SIGINT
5+
cd tests/Application && APP_ENV=test symfony server:start --port=8080 --dir=public & "${GOOGLE_CHROME}" --enable-automation --disable-background-networking --no-default-browser-check --no-first-run --disable-popup-blocking --disable-default-apps --allow-insecure-localhost --disable-translate --disable-extensions --no-sandbox --enable-features=Metal --headless --remote-debugging-port=9222 --disable-gpu --window-size=2880,1800 --proxy-server='direct://' --proxy-bypass-list='*' --crash-dumps-dir=/tmp https://127.0.0.1 & tee

features/delete_product_subscription_from_my_account.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Feature: Ability to delete a particular subscription from my account
88
And there is a customer "Francis Underwood" identified by an email "francis@underwood.com" and a password "whitehouse"
99
And I am logged in as "francis@underwood.com"
1010

11-
@ui
11+
@ui @javascript
1212
Scenario: Being able to delete a subscription from my account
1313
When I view product "Knitted cap apple"
1414
And I subscribe to the alert list for the product "Knitted cap apple"

public/js/back-in-stock-notification.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
3636
if (notificationTrigger) {
3737
notificationTrigger.onclick = function () {
3838
jQuery('#notification-overlay').modal('show')
39-
document.querySelector('#back_in_stock_not input[type="hidden"]').value = getSelectedVariantCode()
39+
document.querySelector('input[back-in-stock-notification-plugin-variant-code-input][type="hidden"]').value = getSelectedVariantCode()
4040
}
4141
}
4242

src/Form/SubscriptionType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2929
],
3030
])
3131
->add('product_variant_code', HiddenType::class)
32-
->add('submit', SubmitType::class)
32+
->add('submit', SubmitType::class, [
33+
'label' => 'webgriffe_bisn.product_page.form_action',
34+
])
3335
;
3436
}
3537

src/Validator/SubscriptionUniqueValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Webgriffe\SyliusBackInStockNotificationPlugin\Validator;
66

7+
use Sylius\Component\Core\Model\CustomerInterface;
78
use Sylius\Component\Core\Model\ProductVariantInterface;
89
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
10+
use Sylius\Component\Customer\Context\CustomerContextInterface;
911
use Symfony\Component\Validator\Constraint;
1012
use Symfony\Component\Validator\ConstraintValidator;
1113
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -20,6 +22,7 @@ final class SubscriptionUniqueValidator extends ConstraintValidator
2022
public function __construct(
2123
private SubscriptionRepositoryInterface $subscriptionRepository,
2224
private ProductVariantRepositoryInterface $productVariantRepository,
25+
private CustomerContextInterface $customerContext,
2326
) {
2427
}
2528

@@ -41,7 +44,15 @@ public function validate(mixed $value, Constraint $constraint): void
4144
}
4245

4346
$productVariantCode = $value['product_variant_code'];
44-
$email = $value['email'];
47+
if (array_key_exists('email', $value)) {
48+
$email = $value['email'];
49+
} else {
50+
$customer = $this->customerContext->getCustomer();
51+
if (!$customer instanceof CustomerInterface) {
52+
return;
53+
}
54+
$email = $customer->getEmail();
55+
}
4556
if (!is_string($productVariantCode) || !is_string($email)) {
4657
return;
4758
}

templates/productSubscriptionForm.html.twig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
{{ form_start(form, {
77
'attr': {'class': 'ui form form-default', 'id': 'back_in_stock_not'},
88
'action': path('webgriffe_back_in_stock_notification_add_subscription')
9-
}) }}
9+
}|merge(sylius_test_form_attribute('subscribe-to-notifications'))) }}
1010

1111
{{ form_errors(form) }}
1212

1313
{% if form.email is defined %}
1414
{{ form_row(form.email, {'attr': {'data-test-fill-subscription-form-whit-my-email':''}}) }}
1515
{% endif %}
1616

17-
{{ form_row(form.product_variant_code, {'attr': {'data-test-add-email-to-back-in-stock-notification-list':''}}) }}
17+
{{ form_row(form.product_variant_code, {'attr': {
18+
'back-in-stock-notification-plugin-variant-code-input':''
19+
}}) }}
1820

19-
<button type="submit" class="ui huge primary icon labeled button" {{ sylius_test_html_attribute('subscribe-to-notifications') }}>
20-
<i class="bell icon"></i> {{ 'webgriffe_bisn.product_page.form_action'|trans }}
21-
</button>
21+
{{ form_row(form.submit, {
22+
'attr': {'class': 'ui huge primary icon labeled button'},
23+
}) }}
2224

2325
{{ form_row(form._token) }}
2426
{{ form_end(form, {'render_rest': false}) }}

tests/Behat/Element/Product/ShowPage/SubscriptionFormElement.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44

55
namespace Tests\Webgriffe\SyliusBackInStockNotificationPlugin\Behat\Element\Product\ShowPage;
66

7+
use Behat\Mink\Session;
78
use FriendsOfBehat\PageObjectExtension\Element\Element;
9+
use Sylius\Behat\Page\Shop\Product\ShowPageInterface;
10+
use Sylius\Behat\Service\DriverHelper;
11+
use Sylius\Behat\Service\JQueryHelper;
812

913
final class SubscriptionFormElement extends Element implements SubscriptionFormElementInterface
1014
{
15+
/**
16+
* @param mixed[]|\ArrayAccess<array-key, mixed> $minkParameters
17+
*/
18+
public function __construct(
19+
Session $session,
20+
$minkParameters,
21+
private ShowPageInterface $productPage,
22+
) {
23+
parent::__construct($session, $minkParameters);
24+
}
25+
1126
public function submitFormAsAGuest(string $variant, string $email): void
1227
{
1328
$this->getElement('add_email')->setValue($email);
14-
$this->getElement('submit_form')->click();
29+
$this->getElement('submit_form')->submit();
30+
31+
$this->waitForProductPageRefresh();
1532
}
1633

1734
public function submitFormAsALoggedCustomer(string $variant): void
1835
{
19-
$this->getElement('submit_form')->click();
36+
$this->getElement('submit_form')->submit();
37+
38+
$this->waitForProductPageRefresh();
2039
}
2140

2241
public function openOverlayForConfigurableProduct(): void
@@ -32,4 +51,12 @@ protected function getDefinedElements(): array
3251
'submit_form' => '[data-test-subscribe-to-notifications]',
3352
]);
3453
}
54+
55+
private function waitForProductPageRefresh(): void
56+
{
57+
if (DriverHelper::isJavascript($this->getDriver())) {
58+
JQueryHelper::waitForAsynchronousActionsToFinish($this->getSession());
59+
$this->getDocument()->waitFor(3, fn (): bool => $this->productPage->isOpen());
60+
}
61+
}
3562
}

tests/Behat/Resources/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ services:
1212
Tests\Webgriffe\SyliusBackInStockNotificationPlugin\Behat\Element\Product\ShowPage\SubscriptionFormElement:
1313
parent: 'sylius.behat.element'
1414
public: false
15+
arguments:
16+
- '@sylius.behat.page.shop.product.show'
1517

1618
Tests\Webgriffe\SyliusBackInStockNotificationPlugin\Behat\Page\Shop\Account\Subscription\IndexPage:
1719
parent: 'sylius.behat.symfony_page'

0 commit comments

Comments
 (0)