-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from tyx/feature/deal-with-failure
🆕 Introduce RestApiBrowser::sendRequestUntil
- Loading branch information
Showing
7 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
Feature: Send request until | ||
In order to test async system | ||
As a developer | ||
I should be able to try to send HTTP request until it validates my requirement | ||
|
||
Background: | ||
Given a file named "behat.yml" with: | ||
""" | ||
default: | ||
extensions: | ||
Rezzza\RestApiBehatExtension\Extension: | ||
rest: | ||
base_url: http://localhost:8888 | ||
suites: | ||
default: | ||
contexts: | ||
- FeatureContext | ||
- Rezzza\RestApiBehatExtension\RestApiContext | ||
""" | ||
|
||
Scenario: Send request until it works | ||
Given a file named "features/send_request_until.feature" with: | ||
""" | ||
Feature: Send request until | ||
In order to deal with async system | ||
As a feature runner | ||
I need to continue to send request until it works | ||
Scenario: Send request that could fail | ||
When I call my microservice | ||
And I call my microservice | ||
And I call my microservice | ||
Then print response | ||
""" | ||
And a file named "features/bootstrap/FeatureContext.php" with: | ||
""" | ||
<?php | ||
use Behat\Behat\Context\Context; | ||
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser; | ||
use mageekguy\atoum\asserter; | ||
class FeatureContext implements Context | ||
{ | ||
private $restApiBrowser; | ||
private $asserter; | ||
public function __construct(RestApiBrowser $restApiBrowser) | ||
{ | ||
$this->restApiBrowser = $restApiBrowser; | ||
$this->asserter = new asserter\generator; | ||
} | ||
/** | ||
* @When I call my microservice | ||
*/ | ||
public function callMyMicroservice() | ||
{ | ||
$restApiBrowser = $this->restApiBrowser; | ||
$asserter = $this->asserter; | ||
$this->restApiBrowser->sendRequestUntil( | ||
'GET', 'error_random', null, function () use ($restApiBrowser, $asserter) { | ||
$asserter->integer($restApiBrowser->getResponse()->getStatusCode())->isEqualTo(200); | ||
} | ||
); | ||
} | ||
} | ||
""" | ||
When I run behat "features/send_request_until.feature" | ||
Then it should pass with: | ||
""" | ||
200 OK | ||
""" | ||
|
||
Scenario: Send request that will fail always | ||
Given a file named "features/send_request_until.feature" with: | ||
""" | ||
Feature: Send request until | ||
In order to deal with async system | ||
As a feature runner | ||
I need to continue to send request until it works | ||
Scenario: Send request that fail | ||
When I call my microservice | ||
Then print response | ||
""" | ||
And a file named "features/bootstrap/FeatureContext.php" with: | ||
""" | ||
<?php | ||
use Behat\Behat\Context\Context; | ||
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser; | ||
use mageekguy\atoum\asserter; | ||
class FeatureContext implements Context | ||
{ | ||
private $restApiBrowser; | ||
private $asserter; | ||
public function __construct(RestApiBrowser $restApiBrowser) | ||
{ | ||
$this->restApiBrowser = $restApiBrowser; | ||
$this->asserter = new asserter\generator; | ||
} | ||
/** | ||
* @When I call my microservice | ||
*/ | ||
public function callMyMicroservice() | ||
{ | ||
$restApiBrowser = $this->restApiBrowser; | ||
$asserter = $this->asserter; | ||
$this->restApiBrowser->sendRequestUntil( | ||
'GET', 'always_error', null, function () use ($restApiBrowser, $asserter) { | ||
$asserter->integer($restApiBrowser->getResponse()->getStatusCode())->isEqualTo(200); | ||
}, | ||
5 | ||
); | ||
} | ||
} | ||
""" | ||
When I run behat "features/send_request_until.feature" | ||
Then it should fail with: | ||
""" | ||
integer(502) is not equal to integer(200) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Rezzza\RestApiBehatExtension\Tolerance; | ||
|
||
use Tolerance\Waiter\Waiter; | ||
use Tolerance\Waiter\StatefulWaiter; | ||
|
||
class ExecutionTimeLimited implements Waiter, StatefulWaiter | ||
{ | ||
/** | ||
* @var Waiter | ||
*/ | ||
private $waiter; | ||
|
||
private $maxExecutionTime; | ||
|
||
private $timeEllapsed; | ||
|
||
public function __construct(Waiter $waiter, $maxExecutionTime) | ||
{ | ||
$this->waiter = $waiter; | ||
$this->maxExecutionTime = $maxExecutionTime; | ||
$this->timeEllapsed = 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function wait($seconds = 1) | ||
{ | ||
$this->timeEllapsed += $seconds; | ||
if ($this->maxExecutionTime < $this->timeEllapsed) { | ||
throw MaxExecutionTimeReached::withValue($this->maxExecutionTime); | ||
} | ||
$this->waiter->wait($seconds); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resetState() | ||
{ | ||
// wait for 0.4.0 to have https://github.com/Tolerance/Tolerance/pull/67 | ||
// $this->timeEllapsed = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rezzza\RestApiBehatExtension\Tolerance; | ||
|
||
use Tolerance\Waiter\WaiterException; | ||
|
||
class MaxExecutionTimeReached extends WaiterException | ||
{ | ||
public static function withValue($maxExecutionTime) | ||
{ | ||
return new static(sprintf('Max execution "%s seconds" time is reached', $maxExecutionTime)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters