Passed
Pull Request — main (#17)
by Rizart
10:12
created

UnitTestCase::assertAPIRequestHasMultipartField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 10
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) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after FOREACH keyword; 0 found
Loading history...
69
            $expected = sprintf('Content-Disposition: form-data; name="%s"', $key);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 83 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
70
            $this->assertStringContainsString($expected, $contents);
71
            $this->assertStringContainsString($value, $contents);
72
        }
73
    }
74
75
    protected function assertAPIRequestHasMultipartFile(
76
        array $expectedParams,
77
        array $container
78
    ) {
79
        $this->assertMiddlewarePushed($container);
80
        $this->assertArrayHasKey('request', $container[0]);
81
82
        /** @var Request $request */
83
        $request = $container[0]['request'];
84
85
        $contents = (string)$request->getBody();
86
87
        foreach($expectedParams as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after FOREACH keyword; 0 found
Loading history...
88
            $expected = sprintf('Content-Disposition: form-data; name="%s"; filename="%s"', $key, $key);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 104 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
89
            $this->assertStringContainsString($expected, $contents);
90
            $this->assertStringContainsString($value, $contents);
91
        }
92
    }
93
94
    protected function assertAPIRequestQueryHas($container, $string)
95
    {
96
        $this->assertMiddlewarePushed($container);
97
        $this->assertArrayHasKey('request', $container[0]);
98
99
        /** @var Request $request */
100
        $request = $container[0]['request'];
101
102
        $this->assertEquals($string, $request->getUri()->getQuery());
103
    }
104
105
    protected function assertAPIBaseURIEquals($container, $string)
106
    {
107
        $this->assertArrayHasKey('request', $container[0]);
108
109
        /** @var Request $request */
110
        $request = $container[0]['request'];
111
112
        $this->assertEquals($string, $request->getUri()->getHost());
113
    }
114
115
    protected function makeAttachment($content, $path = '/lorem.txt'): string
116
    {
117
        $root = vfsStream::setup();
118
        $attachmentPath = $root->url() . $path;
119
120
        file_put_contents($attachmentPath, serialize($content));
121
        return $attachmentPath;
122
    }
123
}
124