Skip to content

Commit 50db0ce

Browse files
authored
Merge pull request #2 from MacPaw/feat/addReadMe
Feat/add read me
2 parents a0c6e3e + 2a21ec3 commit 50db0ce

File tree

6 files changed

+136
-27
lines changed

6 files changed

+136
-27
lines changed

README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Behat HTTP Mock Context
44
| Version | Build Status | Code Coverage |
55
|:---------:|:-------------:|:-----:|
66
| `main` | [![CI][main Build Status Image]][main Build Status] | [![Coverage Status][main Code Coverage Image]][main Code Coverage] |
7-
| `develop` | [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] |
87

98
Installation
109
============
@@ -24,12 +23,6 @@ In the `config/services_test.yaml` file of your project:
2423
```yaml
2524
BehatHttpMockContext\:
2625
resource: '../vendor/macpaw/behat-http-mock-context/src/*'
27-
arguments:
28-
- '@test.service_container'
29-
30-
BehatHttpMockContext\Collection\ExtendedMockHttpClientCollection:
31-
arguments:
32-
- !tagged_iterator mock.http_client
3326
```
3427
3528
Step 2: Mock http client
@@ -89,9 +82,5 @@ Step 4: How to use:
8982
9083
[main Build Status]: https://github.com/macpaw/BehatHttpMockContext/actions?query=workflow%3ACI+branch%3Amain
9184
[main Build Status Image]: https://github.com/macpaw/BehatHttpMockContext/workflows/CI/badge.svg?branch=main
92-
[develop Build Status]: https://github.com/macpaw/BehatHttpMockContext/actions?query=workflow%3ACI+branch%3Adevelop
93-
[develop Build Status Image]: https://github.com/macpaw/BehatHttpMockContext/workflows/CI/badge.svg?branch=develop
9485
[main Code Coverage]: https://codecov.io/gh/macpaw/BehatHttpMockContext/branch/main
9586
[main Code Coverage Image]: https://img.shields.io/codecov/c/github/macpaw/BehatHttpMockContext/main?logo=codecov
96-
[develop Code Coverage]: https://codecov.io/gh/macpaw/BehatHttpMockContext/branch/develop
97-
[develop Code Coverage Image]: https://img.shields.io/codecov/c/github/macpaw/BehatHttpMockContext/develop?logo=codecov

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"symfony",
88
"behat",
99
"BDD",
10-
"Context",
11-
"Http"
10+
"MockContext",
11+
"HttpMockContext",
12+
"MockHttpResponse"
1213
],
1314
"authors": [
1415
{

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@
4040
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
4141

4242
<file>src/</file>
43+
<file>tests/</file>
4344
</ruleset>

src/Context/MockContext.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function __construct(
3030
public function afterScenario(): void
3131
{
3232
foreach ($this->extendedMockHttpClientCollection->getHandlers() as $extendedMockHttpClient) {
33+
if (!($extendedMockHttpClient instanceof ExtendedMockHttpClient)) {
34+
throw new RuntimeException('You should replace HTTP client service using ExtendedMockHttpClient');
35+
}
36+
3337
$extendedMockHttpClient->reset();
3438
}
3539
}

tests/Collection/ExtendedMockHttpClientCollectionTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,44 @@ class ExtendedMockHttpClientCollectionTest extends TestCase
1313
public function testSuccess(): void
1414
{
1515
$clientCollection = new ExtendedMockHttpClientCollection([]);
16-
16+
1717
$this->assertCount(0, $clientCollection->getHandlers());
1818
}
19-
19+
2020
public function testInitFailed(): void
2121
{
22-
$this->expectError(TypeError::class);
22+
$this->expectException(TypeError::class);
2323
new ExtendedMockHttpClientCollection('string');
2424
}
25-
25+
2626
public function testSetHandlersSuccess(): void
2727
{
2828
$clientCollection = new ExtendedMockHttpClientCollection([]);
29-
29+
3030
$this->assertCount(0, $clientCollection->getHandlers());
31-
31+
3232
$clientCollection->setHandlers([[]]);
33-
33+
3434
$this->assertCount(1, $clientCollection->getHandlers());
3535
}
36-
36+
3737
public function testSetHandlersFailed(): void
3838
{
39-
$this->expectError(TypeError::class);
39+
$this->expectException(TypeError::class);
4040
$clientCollection = new ExtendedMockHttpClientCollection([]);
41-
41+
4242
$this->assertCount(0, $clientCollection->getHandlers());
4343
$clientCollection->setHandlers('string');
4444
}
45-
45+
4646
public function testResetSuccess(): void
4747
{
4848
$clientCollection = new ExtendedMockHttpClientCollection([[]]);
49-
49+
5050
$this->assertCount(1, $clientCollection->getHandlers());
51-
51+
5252
$clientCollection->reset();
53-
53+
5454
$this->assertCount(0, $clientCollection->getHandlers());
5555
}
5656
}

tests/Context/MockContextTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BehatHttpMockContext\Tests\Context;
6+
7+
use BehatHttpMockContext\Collection\ExtendedMockHttpClientCollection;
8+
use BehatHttpMockContext\Context\MockContext;
9+
use ExtendedMockHttpClient\Builder\RequestMockBuilder;
10+
use ExtendedMockHttpClient\ExtendedMockHttpClient;
11+
use ExtendedMockHttpClient\Model\HttpFixture;
12+
use PHPUnit\Framework\TestCase;
13+
use RuntimeException;
14+
use stdClass;
15+
use Symfony\Component\DependencyInjection\Container;
16+
use Symfony\Component\HttpClient\Response\MockResponse;
17+
18+
class MockContextTest extends TestCase
19+
{
20+
public function testFailingObjectInCollection(): void
21+
{
22+
$this->expectException(RuntimeException::class);
23+
24+
$mockCollection = new ExtendedMockHttpClientCollection(
25+
['string']
26+
);
27+
28+
$mockContext = new MockContext(
29+
new Container(),
30+
$mockCollection
31+
);
32+
33+
self::assertCount(1, $mockCollection->getHandlers());
34+
35+
$mockContext->afterScenario();
36+
}
37+
38+
public function testSuccess(): void
39+
{
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 ExtendedMockHttpClientCollection([
49+
new ExtendedMockHttpClient('macpaw.com')
50+
]);
51+
52+
$mockContext = new MockContext(
53+
new Container(),
54+
$mockCollection
55+
);
56+
57+
self::assertCount(1, $mockCollection->getHandlers());
58+
59+
$mockContext->afterScenario();
60+
61+
self::assertCount(1, $mockCollection->getHandlers());
62+
}
63+
64+
public function testServiceNotFound(): void
65+
{
66+
$this->expectException(RuntimeException::class);
67+
$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 ExtendedMockHttpClientCollection([
77+
new ExtendedMockHttpClient('macpaw.com')
78+
]);
79+
80+
$mockContext = new MockContext(
81+
new Container(),
82+
$mockCollection
83+
);
84+
85+
self::assertCount(1, $mockCollection->getHandlers());
86+
87+
$mockContext->iMockHttpClientNextResponse('test', 204);
88+
}
89+
90+
public function testFailedClientService(): void
91+
{
92+
$this->expectException(RuntimeException::class);
93+
$this->expectErrorMessage('You should replace HTTP client service using ExtendedMockHttpClient');
94+
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 ExtendedMockHttpClientCollection([
104+
new ExtendedMockHttpClient('macpaw.com')
105+
]);
106+
107+
$container = new Container();
108+
$container->set('test', new stdClass());
109+
110+
$mockContext = new MockContext($container, $mockCollection);
111+
112+
$mockContext->iMockHttpClientNextResponse('test', 204);
113+
}
114+
}

0 commit comments

Comments
 (0)