Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f310afd

Browse files
committed
testing assertion helper
1 parent c696984 commit f310afd

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/Sylius/Behat/Context/Ui/Admin/NotificationContext.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,73 @@
1616
use Behat\Behat\Context\Context;
1717
use Sylius\Behat\NotificationType;
1818
use Sylius\Behat\Service\NotificationCheckerInterface;
19+
use Sylius\Behat\TestAssertionHelper;
20+
use Sylius\Behat\TestAssertionHelperInterface;
21+
use Webmozart\Assert\Assert;
1922

2023
final class NotificationContext implements Context
2124
{
2225
/** @var NotificationCheckerInterface */
2326
private $notificationChecker;
2427

25-
public function __construct(NotificationCheckerInterface $notificationChecker)
28+
/** @var TestAssertionHelperInterface */
29+
private $assertionHelper;
30+
31+
public function __construct(NotificationCheckerInterface $notificationChecker, TestAssertionHelperInterface $assertionHelper)
2632
{
2733
$this->notificationChecker = $notificationChecker;
34+
$this->assertionHelper = $assertionHelper;
2835
}
2936

3037
/**
3138
* @Then I should be notified that it has been successfully created
3239
*/
3340
public function iShouldBeNotifiedItHasBeenSuccessfullyCreated()
3441
{
35-
$this->notificationChecker->checkNotification('has been successfully created.', NotificationType::success());
42+
$this->assertionHelper->waitUntilAssertionPasses(
43+
3,
44+
function (): void {
45+
$this->notificationChecker->checkNotification('has been successfully created.', NotificationType::success());
46+
}
47+
);
3648
}
3749

3850
/**
3951
* @Then I should be notified that it has been successfully edited
4052
*/
4153
public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited()
4254
{
43-
$this->notificationChecker->checkNotification('has been successfully updated.', NotificationType::success());
55+
$this->assertionHelper->waitUntilAssertionPasses(
56+
3,
57+
function (): void {
58+
$this->notificationChecker->checkNotification('has been successfully updated.', NotificationType::success());
59+
}
60+
);
4461
}
4562

4663
/**
4764
* @Then I should be notified that it has been successfully deleted
4865
*/
4966
public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted()
5067
{
51-
$this->notificationChecker->checkNotification('has been successfully deleted.', NotificationType::success());
68+
$this->assertionHelper->waitUntilAssertionPasses(
69+
3,
70+
function (): void {
71+
$this->notificationChecker->checkNotification('has been successfully deleted.', NotificationType::success());
72+
}
73+
);
5274
}
5375

5476
/**
5577
* @Then I should be notified that they have been successfully deleted
5678
*/
5779
public function iShouldBeNotifiedThatTheyHaveBeenSuccessfullyDeleted()
5880
{
59-
$this->notificationChecker->checkNotification('have been successfully deleted.', NotificationType::success());
81+
$this->assertionHelper->waitUntilAssertionPasses(
82+
3,
83+
function (): void {
84+
$this->notificationChecker->checkNotification('have been successfully deleted.', NotificationType::success());
85+
}
86+
);
6087
}
6188
}

src/Sylius/Behat/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
<service id="sylius.behat.shared_storage" class="Sylius\Behat\Service\SharedStorage" public="false"/>
9090

91+
<service id="sylius.behat.test_assertion_helper" class="Sylius\Behat\TestAssertionHelper" />
92+
9193
<service id="sylius.calendar" class="Sylius\Behat\Service\Provider\Calendar">
9294
<argument>%kernel.project_dir%</argument>
9395
</service>

src/Sylius/Behat/Resources/config/services/contexts/ui.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280

281281
<service id="sylius.behat.context.ui.admin.notification" class="Sylius\Behat\Context\Ui\Admin\NotificationContext">
282282
<argument type="service" id="sylius.behat.notification_checker" />
283+
<argument type="service" id="sylius.behat.test_assertion_helper" />
283284
</service>
284285

285286
<service id="sylius.behat.context.ui.admin.impersonating_customers" class="Sylius\Behat\Context\Ui\Admin\ImpersonatingCustomersContext">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\Behat;
6+
7+
final class TestAssertionHelper implements TestAssertionHelperInterface
8+
{
9+
public function waitUntilAssertionPasses(int $timeout, callable $assertion): void
10+
{
11+
$start = microtime(true);
12+
$end = $start + $timeout;
13+
14+
do {
15+
try {
16+
$assertion();
17+
} catch (\InvalidArgumentException $exception) {
18+
usleep(100000);
19+
20+
continue;
21+
}
22+
23+
return;
24+
} while (microtime(true) < $end);
25+
26+
throw new \InvalidArgumentException('Time has run out and the assertion has not passed yet.');
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\Behat;
6+
7+
interface TestAssertionHelperInterface
8+
{
9+
public function waitUntilAssertionPasses(int $timeout, callable $assertion): void;
10+
}

0 commit comments

Comments
 (0)