forked from mcustiel/phiremock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue mcustiel#27: Have ability to reload expectations described in J…
…SON files
- Loading branch information
Showing
8 changed files
with
187 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Mcustiel\Phiremock\Server\Actions; | ||
|
||
use Mcustiel\Phiremock\Server\Model\ExpectationStorage; | ||
use Mcustiel\Phiremock\Server\Model\RequestStorage; | ||
use Mcustiel\Phiremock\Server\Model\ScenarioStorage; | ||
use Mcustiel\PowerRoute\Actions\ActionInterface; | ||
use Mcustiel\PowerRoute\Common\TransactionData; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class RestoreExpectationsAction implements ActionInterface | ||
{ | ||
/** | ||
* @var \Mcustiel\Phiremock\Server\Model\ExpectationStorage | ||
*/ | ||
private $expectationStorage; | ||
/** | ||
* @var \Mcustiel\Phiremock\Server\Model\ExpectationStorage | ||
*/ | ||
private $expectationBackup; | ||
/** | ||
* @var \Mcustiel\Phiremock\Server\Model\RequestStorage | ||
*/ | ||
private $requestStorage; | ||
/** | ||
* @var \Mcustiel\Phiremock\Server\Model\ScenarioStorage | ||
*/ | ||
private $scenarioStorage; | ||
/** | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct( | ||
ExpectationStorage $expectationStorage, | ||
ExpectationStorage $expectationBackup, | ||
RequestStorage $requestStorage, | ||
ScenarioStorage $scenarioStorage, | ||
LoggerInterface $logger | ||
) { | ||
$this->expectationStorage = $expectationStorage; | ||
$this->expectationBackup = $expectationBackup; | ||
$this->requestStorage = $requestStorage; | ||
$this->scenarioStorage = $scenarioStorage; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute() | ||
*/ | ||
public function execute(TransactionData $transactionData, $argument = null) | ||
{ | ||
$this->expectationStorage->clearExpectations(); | ||
$this->requestStorage->clearRequests(); | ||
$this->scenarioStorage->clearScenarios(); | ||
foreach ($this->expectationBackup->listExpectations() as $expectation) { | ||
$this->expectationStorage->addExpectation($expectation); | ||
} | ||
$this->logger->debug('Pre-defined expectations are restored, scenarios and requests history are cleared.'); | ||
|
||
$transactionData->setResponse( | ||
$transactionData->getResponse()->withStatus(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
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,13 @@ | ||
{ | ||
"scenarioName": "hello", | ||
"request": { | ||
"method": "GET", | ||
"url": { | ||
"isEqualTo": "/hello" | ||
} | ||
}, | ||
"response": { | ||
"statusCode": 200, | ||
"body": "Hello!" | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
use Mcustiel\Phiremock\Client\Phiremock as PhiremockClient; | ||
use Mcustiel\Phiremock\Client\Utils\A; | ||
use Mcustiel\Phiremock\Client\Utils\Is; | ||
use Mcustiel\Phiremock\Client\Utils\Respond; | ||
|
||
class RestoreExpectationsCest | ||
{ | ||
private $phiremock; | ||
|
||
public function _before(AcceptanceTester $I) | ||
{ | ||
$I->sendDELETE('/__phiremock/expectations'); | ||
$this->phiremock = new PhiremockClient('127.0.0.1', '8086'); | ||
} | ||
|
||
public function restoreExpectationAfterDelete(AcceptanceTester $I) | ||
{ | ||
$this->phiremock->restoreExpectations(); | ||
|
||
$I->sendGET('/hello'); | ||
$I->seeResponseCodeIs('200'); | ||
$I->seeResponseEquals('Hello!'); | ||
} | ||
|
||
public function restoreExpectationAfterRewrite(AcceptanceTester $I) | ||
{ | ||
$this->phiremock->restoreExpectations(); | ||
|
||
$expectation = PhiremockClient::on( | ||
A::getRequest()->andUrl(Is::equalTo('/hello')) | ||
)->then( | ||
Respond::withStatusCode(200) | ||
->andBody('Bye!') | ||
)->setPriority(1); | ||
$this->phiremock->createExpectation($expectation); | ||
|
||
$I->sendGET('/hello'); | ||
$I->seeResponseCodeIs('200'); | ||
$I->seeResponseEquals('Bye!'); | ||
|
||
$this->phiremock->restoreExpectations(); | ||
|
||
$I->sendGET('/hello'); | ||
$I->seeResponseCodeIs('200'); | ||
$I->seeResponseEquals('Hello!'); | ||
} | ||
} |
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