Skip to content

Commit a8e66d9

Browse files
feat: update macpaw/extended_mock_http_client
BREAKING CHANGE: macpaw/extended_mock_http_client to 3 version
1 parent 5a706de commit a8e66d9

File tree

7 files changed

+145
-87
lines changed

7 files changed

+145
-87
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"behat/behat": "^3.0",
2626
"symfony/http-client": "^4.4 || ^5.0 || ^6.0",
2727
"symfony/cache": "^4.4 || ^5.0 || ^6.0",
28-
"macpaw/extended_mock_http_client": "^1.0 || ^2.0",
2928
"symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0",
30-
"symfony/http-kernel": "^4.4 || ^5.4 || ^6.0"
29+
"symfony/http-kernel": "^4.4 || ^5.4 || ^6.0",
30+
"macpaw/extended_mock_http_client": "^3.0"
3131
},
3232
"require-dev": {
3333
"phpstan/phpstan": "^1.2",

src/Collection/ExtendedHttpMockClientCollection.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,48 @@
55
namespace BehatHttpMockContext\Collection;
66

77
use Symfony\Component\Cache\ResettableInterface;
8+
use Symfony\Contracts\HttpClient\HttpClientInterface;
89

910
class ExtendedHttpMockClientCollection implements ResettableInterface
1011
{
11-
/*** @param iterable $handlers */
12-
public function __construct(private iterable $handlers)
12+
/**
13+
* @var HttpClientInterface[]
14+
*/
15+
private array $httpClients = [];
16+
17+
/**
18+
* @param HttpClientInterface[] $httpClients
19+
*/
20+
public function __construct(iterable $httpClients)
21+
{
22+
$this->setHttpClients($httpClients);
23+
}
24+
25+
/**
26+
* @param HttpClientInterface[] $httpClients
27+
*/
28+
public function setHttpClients(iterable $httpClients): void
1329
{
30+
foreach ($httpClients as $httpClient) {
31+
$this->addHttpClient($httpClient);
32+
}
1433
}
1534

16-
public function getHandlers(): iterable
35+
public function addHttpClient(HttpClientInterface $httpClient): void
1736
{
18-
return $this->handlers;
37+
$this->httpClients[] = $httpClient;
1938
}
2039

21-
public function setHandlers(iterable $handlers): void
40+
/**
41+
* @return HttpClientInterface[]
42+
*/
43+
public function getHttpClients(): iterable
2244
{
23-
$this->handlers = $handlers;
45+
return $this->httpClients;
2446
}
2547

2648
public function reset(): void
2749
{
28-
$this->handlers = [];
50+
$this->httpClients = [];
2951
}
3052
}

src/Context/HttpMockContext.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
use BehatHttpMockContext\Collection\ExtendedHttpMockClientCollection;
88
use Behat\Behat\Context\Context;
99
use Behat\Gherkin\Node\PyStringNode;
10-
use ExtendedMockHttpClient\Builder\RequestMockBuilder;
1110
use ExtendedMockHttpClient\ExtendedMockHttpClient;
12-
use ExtendedMockHttpClient\Model\HttpFixture;
1311
use RuntimeException;
1412
use Symfony\Component\DependencyInjection\ContainerInterface;
15-
use Symfony\Component\HttpClient\Response\MockResponse;
1613

1714
class HttpMockContext implements Context
1815
{
@@ -29,7 +26,7 @@ public function __construct(
2926
*/
3027
public function afterScenario(): void
3128
{
32-
foreach ($this->extendedHttpMockClientCollection->getHandlers() as $extendedMockHttpClient) {
29+
foreach ($this->extendedHttpMockClientCollection->getHttpClients() as $extendedMockHttpClient) {
3330
if (!($extendedMockHttpClient instanceof ExtendedMockHttpClient)) {
3431
throw new RuntimeException('You should replace HTTP client service using ExtendedMockHttpClient');
3532
}
@@ -82,14 +79,15 @@ public function iMockHttpClientUrlResponse(
8279
): void {
8380
$httpClient = $this->getHttpClient($httpClientName);
8481

85-
$httpClient->addFixture(new HttpFixture(
86-
(new RequestMockBuilder())
87-
->urlEquals(sprintf('%s/%s', $httpClient->getBaseUri(), $url))
88-
->build(),
89-
new MockResponse(trim($params->getRaw()), [
90-
'http_code' => $responseStatusCode
91-
])
92-
));
82+
$httpFixture = $httpClient->createFixture(
83+
null,
84+
sprintf('%s/%s', $httpClient->getBaseUri(), $url),
85+
null,
86+
null,
87+
$responseStatusCode,
88+
trim($params->getRaw())
89+
);
90+
$httpClient->addFixture($httpFixture);
9391
}
9492

9593
private function mockHttpClientNextResponse(
@@ -100,13 +98,15 @@ private function mockHttpClientNextResponse(
10098
$httpClient = $this->getHttpClient($httpClientName);
10199
$body = $params instanceof PyStringNode ? trim($params->getRaw()) : '';
102100

103-
$httpClient->addFixture(new HttpFixture(
104-
(new RequestMockBuilder())->build(),
105-
new MockResponse(
106-
$body,
107-
['http_code' => $responseStatusCode]
108-
)
109-
));
101+
$httpFixture = $httpClient->createFixture(
102+
null,
103+
null,
104+
null,
105+
null,
106+
$responseStatusCode,
107+
$body
108+
);
109+
$httpClient->addFixture($httpFixture);
110110
}
111111

112112
private function getHttpClient(string $httpClientName): ExtendedMockHttpClient

src/Resources/config/http_mock_context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
<argument key="$handlers" type="tagged_iterator" tag="mock.http_client"/>
1313
</service>
1414
</services>
15-
</container>
15+
</container>

tests/Unit/AbstractUnitTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BehatHttpMockContext\Tests\Unit;
6+
7+
use ExtendedMockHttpClient\ExtendedMockHttpClient;
8+
use ExtendedMockHttpClient\Factory\HttpFixtureBuilderFactory;
9+
use ExtendedMockHttpClient\Factory\HttpFixtureFactory;
10+
use PHPUnit\Framework\TestCase;
11+
12+
abstract class AbstractUnitTest extends TestCase
13+
{
14+
protected function createHttpClient(string $baseUri): ExtendedMockHttpClient
15+
{
16+
$httpFixtureFactory = new HttpFixtureFactory([
17+
'' => [ 'request', 'response' ],
18+
'request' => [ 'url' ],
19+
]);
20+
$httpFixtureBuilderFactory = new HttpFixtureBuilderFactory($httpFixtureFactory);
21+
22+
return new ExtendedMockHttpClient($baseUri, $httpFixtureBuilderFactory);
23+
}
24+
}

tests/Unit/Collection/ExtendedMockHttpClientCollectionTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,64 @@
55
namespace BehatHttpMockContext\Tests\Unit\Collection;
66

77
use BehatHttpMockContext\Collection\ExtendedHttpMockClientCollection;
8-
use PHPUnit\Framework\TestCase;
8+
use BehatHttpMockContext\Tests\Unit\AbstractUnitTest;
99
use TypeError;
1010

11-
class ExtendedMockHttpClientCollectionTest extends TestCase
11+
class ExtendedMockHttpClientCollectionTest extends AbstractUnitTest
1212
{
1313
public function testSuccess(): void
1414
{
1515
$clientCollection = new ExtendedHttpMockClientCollection([]);
1616

17-
$this->assertCount(0, $clientCollection->getHandlers());
17+
$this->assertCount(0, $clientCollection->getHttpClients());
1818
}
1919

20-
public function testInitFailed(): void
20+
public function testInitFailedParameterType(): void
2121
{
2222
$this->expectException(TypeError::class);
23+
2324
new ExtendedHttpMockClientCollection('string');
2425
}
2526

27+
public function testInitFailedHttpClientType(): void
28+
{
29+
$this->expectException(TypeError::class);
30+
31+
new ExtendedHttpMockClientCollection(['string']);
32+
}
33+
2634
public function testSetHandlersSuccess(): void
2735
{
2836
$clientCollection = new ExtendedHttpMockClientCollection([]);
2937

30-
$this->assertCount(0, $clientCollection->getHandlers());
38+
$this->assertCount(0, $clientCollection->getHttpClients());
3139

32-
$clientCollection->setHandlers([[]]);
40+
$clientCollection->setHttpClients([
41+
$this->createHttpClient('test2@test.test')
42+
]);
3343

34-
$this->assertCount(1, $clientCollection->getHandlers());
44+
$this->assertCount(1, $clientCollection->getHttpClients());
3545
}
3646

3747
public function testSetHandlersFailed(): void
3848
{
3949
$this->expectException(TypeError::class);
4050
$clientCollection = new ExtendedHttpMockClientCollection([]);
4151

42-
$this->assertCount(0, $clientCollection->getHandlers());
43-
$clientCollection->setHandlers('string');
52+
$this->assertCount(0, $clientCollection->getHttpClients());
53+
$clientCollection->setHttpClients('string');
4454
}
4555

4656
public function testResetSuccess(): void
4757
{
48-
$clientCollection = new ExtendedHttpMockClientCollection([[]]);
58+
$clientCollection = new ExtendedHttpMockClientCollection([
59+
$this->createHttpClient('test1@test.test')
60+
]);
4961

50-
$this->assertCount(1, $clientCollection->getHandlers());
62+
$this->assertCount(1, $clientCollection->getHttpClients());
5163

5264
$clientCollection->reset();
5365

54-
$this->assertCount(0, $clientCollection->getHandlers());
66+
$this->assertCount(0, $clientCollection->getHttpClients());
5567
}
5668
}

tests/Unit/Context/MockContextTest.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,82 @@
66

77
use BehatHttpMockContext\Collection\ExtendedHttpMockClientCollection;
88
use BehatHttpMockContext\Context\HttpMockContext;
9-
use ExtendedMockHttpClient\Builder\RequestMockBuilder;
10-
use ExtendedMockHttpClient\ExtendedMockHttpClient;
11-
use ExtendedMockHttpClient\Model\HttpFixture;
12-
use PHPUnit\Framework\TestCase;
9+
use BehatHttpMockContext\Tests\Unit\AbstractUnitTest;
1310
use RuntimeException;
1411
use stdClass;
1512
use Symfony\Component\DependencyInjection\Container;
16-
use Symfony\Component\HttpClient\Response\MockResponse;
13+
use Symfony\Component\HttpClient\CurlHttpClient;
1714

18-
class MockContextTest extends TestCase
15+
class MockContextTest extends AbstractUnitTest
1916
{
2017
public function testFailingObjectInCollection(): void
2118
{
2219
$this->expectException(RuntimeException::class);
2320

24-
$mockCollection = new ExtendedHttpMockClientCollection(
25-
['string']
26-
);
21+
$notMockedHttpClient = new CurlHttpClient();
22+
$mockCollection = new ExtendedHttpMockClientCollection([ $notMockedHttpClient ]);
2723

2824
$mockContext = new HttpMockContext(
2925
new Container(),
3026
$mockCollection
3127
);
3228

33-
self::assertCount(1, $mockCollection->getHandlers());
29+
self::assertCount(1, $mockCollection->getHttpClients());
3430

3531
$mockContext->afterScenario();
3632
}
3733

3834
public function testSuccess(): void
3935
{
40-
$client = new ExtendedMockHttpClient('http://test.test');
41-
$client->addFixture(new HttpFixture(
42-
(new RequestMockBuilder())->build(),
43-
new MockResponse('response body', [
44-
'http_code' => 200
45-
])
46-
));
47-
48-
$mockCollection = new ExtendedHttpMockClientCollection([
49-
new ExtendedMockHttpClient('macpaw.com')
50-
]);
36+
$client = $this->createHttpClient('http://test.test');
37+
$httpFixture = $client->createFixture(
38+
null,
39+
null,
40+
null,
41+
null,
42+
200,
43+
'response body'
44+
);
45+
$client->addFixture($httpFixture);
46+
47+
$mockCollection = new ExtendedHttpMockClientCollection([$client]);
5148

5249
$mockContext = new HttpMockContext(
5350
new Container(),
5451
$mockCollection
5552
);
5653

57-
self::assertCount(1, $mockCollection->getHandlers());
54+
self::assertCount(1, $mockCollection->getHttpClients());
5855

5956
$mockContext->afterScenario();
6057

61-
self::assertCount(1, $mockCollection->getHandlers());
58+
self::assertCount(1, $mockCollection->getHttpClients());
6259
}
6360

6461
public function testServiceNotFound(): void
6562
{
6663
$this->expectException(RuntimeException::class);
6764
$this->expectErrorMessage('Service not found');
68-
$client = new ExtendedMockHttpClient('http://test.test');
69-
$client->addFixture(new HttpFixture(
70-
(new RequestMockBuilder())->build(),
71-
new MockResponse('response body', [
72-
'http_code' => 200
73-
])
74-
));
75-
76-
$mockCollection = new ExtendedHttpMockClientCollection([
77-
new ExtendedMockHttpClient('macpaw.com')
78-
]);
65+
66+
$client = $this->createHttpClient('http://test.test');
67+
$httpFixture = $client->createFixture(
68+
null,
69+
null,
70+
null,
71+
null,
72+
200,
73+
'response body'
74+
);
75+
$client->addFixture($httpFixture);
76+
77+
$mockCollection = new ExtendedHttpMockClientCollection([ $client ]);
7978

8079
$mockContext = new HttpMockContext(
8180
new Container(),
8281
$mockCollection
8382
);
8483

85-
self::assertCount(1, $mockCollection->getHandlers());
84+
self::assertCount(1, $mockCollection->getHttpClients());
8685

8786
$mockContext->iMockHttpClientNextResponse('test', 204);
8887
}
@@ -92,17 +91,18 @@ public function testFailedClientService(): void
9291
$this->expectException(RuntimeException::class);
9392
$this->expectErrorMessage('You should replace HTTP client service using ExtendedMockHttpClient');
9493

95-
$client = new ExtendedMockHttpClient('http://test.test');
96-
$client->addFixture(new HttpFixture(
97-
(new RequestMockBuilder())->build(),
98-
new MockResponse('response body', [
99-
'http_code' => 200
100-
])
101-
));
102-
103-
$mockCollection = new ExtendedHttpMockClientCollection([
104-
new ExtendedMockHttpClient('macpaw.com')
105-
]);
94+
$client = $this->createHttpClient('http://test.test');
95+
$httpFixture = $client->createFixture(
96+
null,
97+
null,
98+
null,
99+
null,
100+
200,
101+
'response body'
102+
);
103+
$client->addFixture($httpFixture);
104+
105+
$mockCollection = new ExtendedHttpMockClientCollection([ $client ]);
106106

107107
$container = new Container();
108108
$container->set('test', new stdClass());

0 commit comments

Comments
 (0)