Skip to content

Commit

Permalink
Added setScenarioState method to the module interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mcustiel committed Oct 7, 2020
1 parent 0c9b252 commit af0e4fc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ return PhpCsFixer\Config::create()
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests/acceptance')
->in(__DIR__ . '/tests/unit')
)
;
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ Retrieves all the requests received by Phiremock server matching the one specifi
$I->grabRequestsMadeToRemoteService(getRequest()->andUrl(isEqualTo('/some/url')));
```

### setScenarioState
Forces the state of a scenario.

```php
$I->setScenarioState('scenarioName', 'newScenarioState');
```

### @expectation Annotations

Allows you to set up an expectation via a json file
Expand Down
13 changes: 9 additions & 4 deletions src/Module/Phiremock.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Phiremock extends CodeceptionModule

/** @var array */
protected $config = [
'host' => 'localhost',
'port' => 8086,
'reset_before_each_test' => false,
'client_factory' => 'default',
'host' => 'localhost',
'port' => 8086,
'reset_before_each_test' => false,
'client_factory' => 'default',
self::EXPECTATIONS_PATH_CONFIG => null,
];

Expand Down Expand Up @@ -136,6 +136,11 @@ public function grabRequestsMadeToRemoteService(ConditionsBuilder $builder): arr
return $this->phiremock->listExecutions($builder);
}

public function setScenarioState(string $name, string $state): void
{
$this->phiremock->setScenarioState($name, $state);
}

private function createFactory(): Factory
{
if (isset($this->config['client_factory'])) {
Expand Down
27 changes: 0 additions & 27 deletions tests/_support/FunctionalTester.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/_support/Helper/Functional.php

This file was deleted.

71 changes: 48 additions & 23 deletions tests/acceptance/BasicTestCest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

use Codeception\Configuration;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Mcustiel\Phiremock\Client\Phiremock;
use function Mcustiel\Phiremock\Client\postRequest;
use function Mcustiel\Phiremock\Client\respond;
use Mcustiel\Phiremock\Client\Utils\A;
use Mcustiel\Phiremock\Client\Utils\Is;
use Mcustiel\Phiremock\Client\Utils\Respond;
use function Mcustiel\Phiremock\Client\{postRequest, respond};
use GuzzleHttp\Client;

class BasicTestCest
{
Expand All @@ -15,7 +17,12 @@ class BasicTestCest

public function _before(AcceptanceTester $I)
{
$this->guzzle = new Client(['http_errors' => false]);
$this->guzzle = new Client(
[
'base_uri' => 'http://localhost:18080',
'http_errors' => false
]
);
}

public function severalExceptatationsInOneTest(AcceptanceTester $I)
Expand Down Expand Up @@ -49,7 +56,7 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
)
);
foreach (['potato', 'tomato', 'banana', 'coconut'] as $item) {
$response = file_get_contents('http://localhost:18080/' . $item);
$response = (string) $this->guzzle->get("/{$item}")->getBody();
$I->assertEquals('I am a ' . $item, $response);
}
$I->seeRemoteServiceReceived(4, A::getRequest());
Expand All @@ -61,19 +68,37 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
$I->seeRemoteServiceReceived(0, A::getRequest());
}

public function shouldSetTheScenarioState(AcceptanceTester $I): void
{
$I->expectARequestToRemoteServiceWithAResponse(
Phiremock::on(
A::getRequest()
->andUrl(Is::equalTo('/potato'))
->andScenarioState('tomatoScenario', 'potatoState')
)->then(
Respond::withStatusCode(203)->andBody('I am a potato')
)
);
$response = $this->guzzle->get('/potato');
$I->assertSame(404, $response->getStatusCode());
$I->setScenarioState('tomatoScenario', 'potatoState');
$response = $this->guzzle->get('/potato');
$I->assertSame(203, $response->getStatusCode());
$I->assertSame('I am a potato', (string) $response->getBody());
}

public function shouldCreateAnExpectationWithBinaryResponseTest(AcceptanceTester $I)
{
$responseContents = file_get_contents(Configuration::dataDir() . '/fixtures/silhouette-1444982_640.png');
$I->expectARequestToRemoteServiceWithAResponse(
Phiremock::on(
A::getRequest()->andUrl(Is::equalTo('/show-me-the-video'))
)->then(
respond(200)->andBinaryBody($responseContents)
A::getRequest()->andUrl(Is::equalTo('/show-me-the-video'))
)->then(
respond(200)->andBinaryBody($responseContents)
)
);


$responseBody = file_get_contents('http://localhost:18080/show-me-the-video');
$responseBody = (string) $this->guzzle->get('/show-me-the-video')->getBody();
$I->assertEquals($responseContents, $responseBody);
}

Expand All @@ -84,14 +109,13 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)
Phiremock::on($requestBuilder)->then(respond(200))
);

$options = array(
'http' => array(
'header' => 'Content-Type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => http_build_query(['a' => 'b'])
)
$request = new Request(
'POST',
'/some/url',
['Content-Type' => 'application/x-www-form-urlencoded'],
http_build_query(['a' => 'b'])
);
file_get_contents('http://localhost:18080/some/url', false, stream_context_create($options));
$this->guzzle->send($request);

$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(1, $requests);
Expand All @@ -101,7 +125,7 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)

$headers = (array) $first->headers;
$expectedSubset = [
'Host' => ['localhost:18080'],
'Host' => ['localhost:18080'],
'Content-Type' => ['application/x-www-form-urlencoded']
];

Expand All @@ -118,12 +142,12 @@ public function testGrabRequestsMadeToRemoteService(AcceptanceTester $I)
public function testAnnotationExpectationIsLoaded(AcceptanceTester $I)
{
$requestBuilder = A::getRequest()->andUrl(Is::equalTo('/expectation/1'));
$response = file_get_contents('http://localhost:18080/expectation/1');
$response = (string) $this->guzzle->get('/expectation/1')->getBody();

$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(1, $requests);

$I->assertEquals("response", $response);
$I->assertEquals('response', $response);
}

/**
Expand All @@ -134,12 +158,12 @@ public function testAnnotationExpectationIsLoaded(AcceptanceTester $I)
public function testMultipleAnnotationsAreLoaded(AcceptanceTester $I)
{
$requestBuilder = A::getRequest()->andUrl(Is::matching('/\\/expectation\\/\\d+/'));
file_get_contents('http://localhost:18080/expectation/1');
file_get_contents('http://localhost:18080/expectation/2');
$this->guzzle->get('/expectation/1');
$this->guzzle->get('/expectation/2');
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(2, $requests);
}

/**
* @param AcceptanceTester $I
* @expectation("subdirectory/test_first_get")
Expand All @@ -148,7 +172,8 @@ public function testAnnotationInSubdirectoryIsLoaded(AcceptanceTester $I)
{
$conditionsBuilder = A::getRequest();
$requestBuilder = $conditionsBuilder->andMethod(Is::equalTo('GET'))->andUrl(Is::equalTo('/expectation/subdirectory'));
file_get_contents('http://localhost:18080/expectation/subdirectory');
$responseBody = (string) $this->guzzle->get('/expectation/subdirectory')->getBody();
$I->assertSame('response', $responseBody);
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(1, $requests);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ExpectationParserTestCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function jsonExtensionIsOptional()

public function expectationNotFoundThrowsParseError(UnitTester $I)
{
$I->expectException(ParseException::class,function(){
$I->expectException(ParseException::class, function () {
$this->parser->parseExpectation("random.expectation");
});
}
Expand Down

0 comments on commit af0e4fc

Please sign in to comment.