forked from brainbits/functional-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientMockTraitTest.php
More file actions
105 lines (90 loc) · 3.78 KB
/
HttpClientMockTraitTest.php
File metadata and controls
105 lines (90 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Brainbits\FunctionalTestHelpers\Tests\HttpClientMock;
use Brainbits\FunctionalTestHelpers\HttpClientMock\HttpClientMockTrait;
use Brainbits\FunctionalTestHelpers\HttpClientMock\Matcher\MethodMatcher;
use Brainbits\FunctionalTestHelpers\HttpClientMock\Matcher\QueryParamMatcher;
use Brainbits\FunctionalTestHelpers\HttpClientMock\Matcher\UriMatcher;
use Brainbits\FunctionalTestHelpers\HttpClientMock\Matcher\UriParams;
use Brainbits\FunctionalTestHelpers\HttpClientMock\MockRequestBuilderCollection;
use Brainbits\FunctionalTestHelpers\HttpClientMock\MockRequestMatcher;
use Monolog\Logger;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class HttpClientMockTraitTest extends TestCase
{
use HttpClientMockTrait;
private static MockRequestBuilderCollection|null $collection = null;
private static EventDispatcherInterface|null $dispatcher = null;
private static Logger|null $logger = null;
#[Before(100)]
public function createContainerServices(): void
{
self::$collection = new MockRequestBuilderCollection();
self::$dispatcher = $this->createMock(EventDispatcherInterface::class);
self::$logger = $this->createMock(Logger::class);
}
public static function getContainer(): Container
{
$container = new Container();
$container->set(MockRequestBuilderCollection::class, self::$collection);
$container->set('event_dispatcher', self::$dispatcher);
$container->set('monolog.logger', self::$logger);
return $container;
}
public function testMockRequest(): void
{
$this->mockRequest('GET', '/query')
->name('test')
->queryParam('firstname', 'peter')
->queryParam('lastname', '%s', 'peterson')
->queryParam('email', '')
->header('content-type', 'text/plain')
->content('this is text')
->willRespond(
$this->mockResponse()
->header('content-type', 'text/plain')
->content('test'),
);
// simulate http request
(self::$collection)(
'GET',
'/query?firstname=peter&lastname=peterson&email=',
[
'headers' => ['Content-Type: text/plain'],
'body' => 'this is text',
],
);
$this->assertAllRequestMocksAreCalled();
}
public function testMockRequestParsesLegacyQueryParamsFromUri(): void
{
$builder = $this->mockRequest('GET', '/query?&firstname=peter&lastname={lastname}&email=');
$this->assertEquals(
$builder->getMatcher(),
new MockRequestMatcher(null, [
new MethodMatcher('GET'),
new QueryParamMatcher('firstname', 'peter', []),
new QueryParamMatcher('lastname', '{lastname}', []),
new QueryParamMatcher('email', '', []),
new UriMatcher('/query', new UriParams()),
]),
);
}
public function testMockRequestParsesLegacyQueryParamsFromUriWithHost(): void
{
$builder = $this->mockRequest('GET', 'https://example.com/query?&firstname=peter&lastname={lastname}&email=');
$this->assertEquals(
$builder->getMatcher(),
new MockRequestMatcher(null, [
new MethodMatcher('GET'),
new QueryParamMatcher('firstname', 'peter', []),
new QueryParamMatcher('lastname', '{lastname}', []),
new QueryParamMatcher('email', '', []),
new UriMatcher('https://example.com/query', new UriParams()),
]),
);
}
}