Skip to content

Commit

Permalink
Issue mcustiel#27: Have ability to reload expectations described in J…
Browse files Browse the repository at this point in the history
…SON files
  • Loading branch information
drudoi committed Sep 28, 2017
1 parent 8ed3454 commit eb982f3
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Client/Phiremock.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public function createExpectation(Expectation $expectation)
$this->checkResponse($this->connection->send($request));
}

/**
* Restores pre-defined expectations and resets scenarios and requests counter.
*/
public function restoreExpectations()
{
$uri = $this->createBaseUri()->withPath(self::API_EXPECTATIONS_URL);
$request = (new PsrRequest())->withUri($uri)->withMethod('put');

$this->checkResponse($this->connection->send($request));
}

/**
* Clears all the currently configured expectations.
*/
Expand Down
68 changes: 68 additions & 0 deletions src/Server/Actions/RestoreExpectationsAction.php
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)
);
}
}
16 changes: 16 additions & 0 deletions src/Server/Config/dependencies-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Mcustiel\Phiremock\Server\Actions\ListExpectationsAction;
use Mcustiel\Phiremock\Server\Actions\ListRequestsAction;
use Mcustiel\Phiremock\Server\Actions\ResetRequestsCountAction;
use Mcustiel\Phiremock\Server\Actions\RestoreExpectationsAction;
use Mcustiel\Phiremock\Server\Actions\SearchRequestAction;
use Mcustiel\Phiremock\Server\Actions\StoreRequestAction;
use Mcustiel\Phiremock\Server\Actions\VerifyRequestFound;
Expand Down Expand Up @@ -101,6 +102,10 @@
return new ExpectationAutoStorage();
});

$di->register('expectationBackup', function () {
return new ExpectationAutoStorage();
});

$di->register('requestStorage', function () {
return new RequestAutoStorage();
});
Expand All @@ -126,6 +131,7 @@
return new FileExpectationsLoader(
$di->get('requestBuilder'),
$di->get('expectationStorage'),
$di->get('expectationBackup'),
$di->get('logger')
);
});
Expand Down Expand Up @@ -177,6 +183,16 @@
ListExpectationsAction::class,
[$di->get('expectationStorage')]
),
'restoreExpectations' => new SingletonLazyCreator(
RestoreExpectationsAction::class,
[
$di->get('expectationStorage'),
$di->get('expectationBackup'),
$di->get('requestStorage'),
$di->get('scenarioStorage'),
$di->get('logger'),
]
),
'clearExpectations' => new SingletonLazyCreator(
ClearExpectationsAction::class,
[$di->get('expectationStorage')]
Expand Down
18 changes: 18 additions & 0 deletions src/Server/Config/router-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@
'if-matches' => [
['listExpectations' => null],
],
'else' => [
['goto' => 'expectationMethodIsPut'],
],
],
],
'expectationMethodIsPut' => [
'condition' => [
'one-of' => [
[
'input-source' => ['method' => null],
'matcher' => ['isEqualTo' => 'PUT'],
],
],
],
'actions' => [
'if-matches' => [
['restoreExpectations' => null],
],
'else' => [
['goto' => 'expectationMethodIsDelete'],
],
Expand Down
10 changes: 10 additions & 0 deletions src/Server/Utils/FileExpectationsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class FileExpectationsLoader
* @var \Mcustiel\Phiremock\Server\Model\ExpectationStorage
*/
private $storage;
/**
* @var \Mcustiel\Phiremock\Server\Model\ExpectationStorage
*/
private $backup;
/**
* @var \Psr\Log\LoggerInterface
*/
Expand All @@ -28,10 +32,12 @@ class FileExpectationsLoader
public function __construct(
RequestBuilder $requestBuilder,
ExpectationStorage $storage,
ExpectationStorage $backup,
LoggerInterface $logger
) {
$this->requestBuilder = $requestBuilder;
$this->storage = $storage;
$this->backup = $backup;
$this->logger = $logger;
}

Expand All @@ -52,6 +58,10 @@ public function loadExpectationFromFile($fileName)

$this->logger->debug('Parsed expectation: ' . var_export($expectation, true));
$this->storage->addExpectation($expectation);
// As we have no API to modify expectation parsed the same object could be used for backup.
// On futher changes when $expectation modifications are possible something like deep-copy
// should be used to clone expectation.
$this->backup->addExpectation($expectation);
}

public function loadExpectationsFromDirectory($directory)
Expand Down
13 changes: 13 additions & 0 deletions tests/tests/_data/expectations/hello.json
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!"
}
}
49 changes: 49 additions & 0 deletions tests/tests/acceptance/RestoreExpectationsCest.php
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!');
}
}
3 changes: 2 additions & 1 deletion tests/tests/acceptance/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

// Here you can initialize variables that will be available to your tests

$command = 'exec php ' . APP_ROOT . 'bin/phiremock --port 8086';
$expectationsDir = __DIR__ . '/../_data/expectations';
$command = 'exec php ' . APP_ROOT . 'bin/phiremock --port 8086 -e ' . $expectationsDir;
echo 'Running ' . $command . PHP_EOL;
$process = new Process($command);

Expand Down

0 comments on commit eb982f3

Please sign in to comment.