1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
4
|
|
|
|
5
|
|
|
use ElasticEmail\Client; |
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
7
|
|
|
use GuzzleHttp\Middleware; |
8
|
|
|
use GuzzleHttp\Psr7\Request; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use org\bovigo\vfs\vfsStream; |
11
|
|
|
use Tests\TestCase; |
12
|
|
|
|
13
|
|
|
class UnitTestCase extends TestCase |
14
|
|
|
{ |
15
|
|
|
protected function mockAPIStatus(&$container = [], $key = 'key'): Client |
16
|
|
|
{ |
17
|
|
|
$history = Middleware::history($container); |
18
|
|
|
|
19
|
|
|
$body = json_encode(['success' => true, 'data' => 'mocked-data']); |
20
|
|
|
$response = new Response(200, [], $body); |
21
|
|
|
$mockHandler = new MockHandler([$response]); |
22
|
|
|
|
23
|
|
|
return new Client($key, [$history], $mockHandler); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function mockAPIRequest(&$container = [], $key = 'key'): Client |
27
|
|
|
{ |
28
|
|
|
$history = Middleware::history($container); |
29
|
|
|
$mockHandler = new MockHandler([ |
30
|
|
|
new Response(200, [], json_encode(['success' => true])), |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
return new Client($key, [$history], $mockHandler); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function assertAPIRequestBodyHas(array $expected, $container) |
37
|
|
|
{ |
38
|
|
|
$this->assertMiddlewarePushed($container); |
39
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
40
|
|
|
|
41
|
|
|
/** @var Request $request */ |
42
|
|
|
$request = $container[0]['request']; |
43
|
|
|
|
44
|
|
|
$actual = (string)$request->getBody(); |
45
|
|
|
$expected = http_build_query($expected); |
46
|
|
|
$this->assertSame($expected, $actual); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function assertMiddlewarePushed($container = []) |
50
|
|
|
{ |
51
|
|
|
$error = 'Expected history middleware was not pushed.'; |
52
|
|
|
|
53
|
|
|
$this->assertCount(1, $container, $error); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function assertAPIRequestHasMultipartField( |
57
|
|
|
array $expectedParams, |
58
|
|
|
array $container |
59
|
|
|
) { |
60
|
|
|
$this->assertMiddlewarePushed($container); |
61
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
62
|
|
|
|
63
|
|
|
/** @var Request $request */ |
64
|
|
|
$request = $container[0]['request']; |
65
|
|
|
|
66
|
|
|
$contents = (string)$request->getBody(); |
67
|
|
|
|
68
|
|
|
foreach ($expectedParams as $key => $value) { |
69
|
|
|
$format = 'Content-Disposition: form-data; name="%s"'; |
70
|
|
|
$expected = sprintf($format, $key); |
71
|
|
|
$this->assertStringContainsString($expected, $contents); |
72
|
|
|
$this->assertStringContainsString($value, $contents); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function assertAPIRequestHasMultipartFile( |
77
|
|
|
array $expectedParams, |
78
|
|
|
array $container |
79
|
|
|
) { |
80
|
|
|
$this->assertMiddlewarePushed($container); |
81
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
82
|
|
|
|
83
|
|
|
/** @var Request $request */ |
84
|
|
|
$request = $container[0]['request']; |
85
|
|
|
|
86
|
|
|
$contents = (string)$request->getBody(); |
87
|
|
|
|
88
|
|
|
$format = 'Content-Disposition: form-data; name="%s"; filename="%s"'; |
89
|
|
|
|
90
|
|
|
foreach ($expectedParams as $key => $value) { |
91
|
|
|
$expected = sprintf($format, $key, $key); |
92
|
|
|
$this->assertStringContainsString($expected, $contents); |
93
|
|
|
$this->assertStringContainsString($value, $contents); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function assertAPIRequestQueryHas($container, $string) |
98
|
|
|
{ |
99
|
|
|
$this->assertMiddlewarePushed($container); |
100
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
101
|
|
|
|
102
|
|
|
/** @var Request $request */ |
103
|
|
|
$request = $container[0]['request']; |
104
|
|
|
|
105
|
|
|
$this->assertEquals($string, $request->getUri()->getQuery()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function assertAPIBaseURIEquals($container, $string) |
109
|
|
|
{ |
110
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
111
|
|
|
|
112
|
|
|
/** @var Request $request */ |
113
|
|
|
$request = $container[0]['request']; |
114
|
|
|
|
115
|
|
|
$this->assertEquals($string, $request->getUri()->getHost()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function makeAttachment($content, $path = '/lorem.txt'): string |
119
|
|
|
{ |
120
|
|
|
$root = vfsStream::setup(); |
121
|
|
|
$attachmentPath = $root->url() . $path; |
122
|
|
|
|
123
|
|
|
file_put_contents($attachmentPath, serialize($content)); |
124
|
|
|
return $attachmentPath; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|