Skip to content

Commit af0e4fc

Browse files
committed
Added setScenarioState method to the module interface
1 parent 0c9b252 commit af0e4fc

File tree

7 files changed

+67
-65
lines changed

7 files changed

+67
-65
lines changed

.php_cs.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ return PhpCsFixer\Config::create()
4545
->setFinder(
4646
PhpCsFixer\Finder::create()
4747
->in(__DIR__ . '/src')
48+
->in(__DIR__ . '/tests/acceptance')
49+
->in(__DIR__ . '/tests/unit')
4850
)
4951
;

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ Retrieves all the requests received by Phiremock server matching the one specifi
131131
$I->grabRequestsMadeToRemoteService(getRequest()->andUrl(isEqualTo('/some/url')));
132132
```
133133

134+
### setScenarioState
135+
Forces the state of a scenario.
136+
137+
```php
138+
$I->setScenarioState('scenarioName', 'newScenarioState');
139+
```
140+
134141
### @expectation Annotations
135142

136143
Allows you to set up an expectation via a json file

src/Module/Phiremock.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class Phiremock extends CodeceptionModule
3636

3737
/** @var array */
3838
protected $config = [
39-
'host' => 'localhost',
40-
'port' => 8086,
41-
'reset_before_each_test' => false,
42-
'client_factory' => 'default',
39+
'host' => 'localhost',
40+
'port' => 8086,
41+
'reset_before_each_test' => false,
42+
'client_factory' => 'default',
4343
self::EXPECTATIONS_PATH_CONFIG => null,
4444
];
4545

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

139+
public function setScenarioState(string $name, string $state): void
140+
{
141+
$this->phiremock->setScenarioState($name, $state);
142+
}
143+
139144
private function createFactory(): Factory
140145
{
141146
if (isset($this->config['client_factory'])) {

tests/_support/FunctionalTester.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/_support/Helper/Functional.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/acceptance/BasicTestCest.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

33
use Codeception\Configuration;
4+
use GuzzleHttp\Client;
5+
use GuzzleHttp\Psr7\Request;
46
use Mcustiel\Phiremock\Client\Phiremock;
7+
use function Mcustiel\Phiremock\Client\postRequest;
8+
use function Mcustiel\Phiremock\Client\respond;
59
use Mcustiel\Phiremock\Client\Utils\A;
610
use Mcustiel\Phiremock\Client\Utils\Is;
711
use Mcustiel\Phiremock\Client\Utils\Respond;
8-
use function Mcustiel\Phiremock\Client\{postRequest, respond};
9-
use GuzzleHttp\Client;
1012

1113
class BasicTestCest
1214
{
@@ -15,7 +17,12 @@ class BasicTestCest
1517

1618
public function _before(AcceptanceTester $I)
1719
{
18-
$this->guzzle = new Client(['http_errors' => false]);
20+
$this->guzzle = new Client(
21+
[
22+
'base_uri' => 'http://localhost:18080',
23+
'http_errors' => false
24+
]
25+
);
1926
}
2027

2128
public function severalExceptatationsInOneTest(AcceptanceTester $I)
@@ -49,7 +56,7 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
4956
)
5057
);
5158
foreach (['potato', 'tomato', 'banana', 'coconut'] as $item) {
52-
$response = file_get_contents('http://localhost:18080/' . $item);
59+
$response = (string) $this->guzzle->get("/{$item}")->getBody();
5360
$I->assertEquals('I am a ' . $item, $response);
5461
}
5562
$I->seeRemoteServiceReceived(4, A::getRequest());
@@ -61,19 +68,37 @@ public function severalExceptatationsInOneTest(AcceptanceTester $I)
6168
$I->seeRemoteServiceReceived(0, A::getRequest());
6269
}
6370

71+
public function shouldSetTheScenarioState(AcceptanceTester $I): void
72+
{
73+
$I->expectARequestToRemoteServiceWithAResponse(
74+
Phiremock::on(
75+
A::getRequest()
76+
->andUrl(Is::equalTo('/potato'))
77+
->andScenarioState('tomatoScenario', 'potatoState')
78+
)->then(
79+
Respond::withStatusCode(203)->andBody('I am a potato')
80+
)
81+
);
82+
$response = $this->guzzle->get('/potato');
83+
$I->assertSame(404, $response->getStatusCode());
84+
$I->setScenarioState('tomatoScenario', 'potatoState');
85+
$response = $this->guzzle->get('/potato');
86+
$I->assertSame(203, $response->getStatusCode());
87+
$I->assertSame('I am a potato', (string) $response->getBody());
88+
}
89+
6490
public function shouldCreateAnExpectationWithBinaryResponseTest(AcceptanceTester $I)
6591
{
6692
$responseContents = file_get_contents(Configuration::dataDir() . '/fixtures/silhouette-1444982_640.png');
6793
$I->expectARequestToRemoteServiceWithAResponse(
6894
Phiremock::on(
69-
A::getRequest()->andUrl(Is::equalTo('/show-me-the-video'))
70-
)->then(
71-
respond(200)->andBinaryBody($responseContents)
95+
A::getRequest()->andUrl(Is::equalTo('/show-me-the-video'))
96+
)->then(
97+
respond(200)->andBinaryBody($responseContents)
7298
)
7399
);
74100

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

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

87-
$options = array(
88-
'http' => array(
89-
'header' => 'Content-Type: application/x-www-form-urlencoded',
90-
'method' => 'POST',
91-
'content' => http_build_query(['a' => 'b'])
92-
)
112+
$request = new Request(
113+
'POST',
114+
'/some/url',
115+
['Content-Type' => 'application/x-www-form-urlencoded'],
116+
http_build_query(['a' => 'b'])
93117
);
94-
file_get_contents('http://localhost:18080/some/url', false, stream_context_create($options));
118+
$this->guzzle->send($request);
95119

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

102126
$headers = (array) $first->headers;
103127
$expectedSubset = [
104-
'Host' => ['localhost:18080'],
128+
'Host' => ['localhost:18080'],
105129
'Content-Type' => ['application/x-www-form-urlencoded']
106130
];
107131

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

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

126-
$I->assertEquals("response", $response);
150+
$I->assertEquals('response', $response);
127151
}
128152

129153
/**
@@ -134,12 +158,12 @@ public function testAnnotationExpectationIsLoaded(AcceptanceTester $I)
134158
public function testMultipleAnnotationsAreLoaded(AcceptanceTester $I)
135159
{
136160
$requestBuilder = A::getRequest()->andUrl(Is::matching('/\\/expectation\\/\\d+/'));
137-
file_get_contents('http://localhost:18080/expectation/1');
138-
file_get_contents('http://localhost:18080/expectation/2');
161+
$this->guzzle->get('/expectation/1');
162+
$this->guzzle->get('/expectation/2');
139163
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
140164
$I->assertCount(2, $requests);
141165
}
142-
166+
143167
/**
144168
* @param AcceptanceTester $I
145169
* @expectation("subdirectory/test_first_get")
@@ -148,7 +172,8 @@ public function testAnnotationInSubdirectoryIsLoaded(AcceptanceTester $I)
148172
{
149173
$conditionsBuilder = A::getRequest();
150174
$requestBuilder = $conditionsBuilder->andMethod(Is::equalTo('GET'))->andUrl(Is::equalTo('/expectation/subdirectory'));
151-
file_get_contents('http://localhost:18080/expectation/subdirectory');
175+
$responseBody = (string) $this->guzzle->get('/expectation/subdirectory')->getBody();
176+
$I->assertSame('response', $responseBody);
152177
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
153178
$I->assertCount(1, $requests);
154179
}

tests/unit/ExpectationParserTestCest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function jsonExtensionIsOptional()
2121

2222
public function expectationNotFoundThrowsParseError(UnitTester $I)
2323
{
24-
$I->expectException(ParseException::class,function(){
24+
$I->expectException(ParseException::class, function () {
2525
$this->parser->parseExpectation("random.expectation");
2626
});
2727
}

0 commit comments

Comments
 (0)