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 Tests\TestCase; |
11
|
|
|
|
12
|
|
|
class UnitTestCase extends TestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
protected function mockAPIStatus(&$container = [], $key = 'key') |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$history = Middleware::history($container); |
17
|
|
|
|
18
|
|
|
$mockHandler = new MockHandler([ |
19
|
|
|
new Response(200, [], json_encode(['success' => true, 'data' => 'mocked-data'])), |
20
|
|
|
]); |
21
|
|
|
|
22
|
|
|
return new Client($key, [$history], $mockHandler); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
protected function mockElasticEmailAPIRequest(&$container = [], $key = 'key') |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$history = Middleware::history($container); |
28
|
|
|
$mockHandler = new MockHandler([ |
29
|
|
|
new Response(200, [], json_encode(['success' => true])), |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
return new Client($key, [$history], $mockHandler); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function assertMiddlewarePushed($container = []) |
36
|
|
|
{ |
37
|
|
|
$error = 'Expected history middleware was not pushed.'; |
38
|
|
|
|
39
|
|
|
$this->assertCount(1, $container, $error); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function assertAPIRequestBodyHas(array $expected, $container) |
43
|
|
|
{ |
44
|
|
|
$this->assertMiddlewarePushed($container); |
45
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
46
|
|
|
|
47
|
|
|
/** @var Request $request */ |
48
|
|
|
$request = $container[0]['request']; |
49
|
|
|
|
50
|
|
|
$actual = (string)$request->getBody(); |
51
|
|
|
$expected = http_build_query($expected); |
52
|
|
|
$this->assertSame($expected, $actual); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function assertAPIRequestMultipartHas(array $params, array $container) |
56
|
|
|
{ |
57
|
|
|
$this->assertMiddlewarePushed($container); |
58
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
59
|
|
|
|
60
|
|
|
/** @var Request $request */ |
61
|
|
|
$request = $container[0]['request']; |
62
|
|
|
|
63
|
|
|
$contents = $request->getBody()->getContents(); |
64
|
|
|
|
65
|
|
|
$expected = sprintf('name="%s"', $params['name']); |
66
|
|
|
$this->assertStringContainsString($expected, $contents); |
67
|
|
|
|
68
|
|
|
$expected = sprintf("\r\n%s\r\n", $params['contents']); |
69
|
|
|
$this->assertStringContainsString($expected, $contents); |
70
|
|
|
|
71
|
|
|
$expected = sprintf("Content-Length: %s\r\n", strlen($params['contents'])); |
72
|
|
|
$this->assertStringContainsString($expected, $contents); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function assertAPIRequestQueryHas($container, $string) |
76
|
|
|
{ |
77
|
|
|
$this->assertMiddlewarePushed($container); |
78
|
|
|
$this->assertArrayHasKey('request', $container[0]); |
79
|
|
|
|
80
|
|
|
/** @var Request $request */ |
81
|
|
|
$request = $container[0]['request']; |
82
|
|
|
|
83
|
|
|
$this->assertEquals($string, $request->getUri()->getQuery()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.