Test Failed
Branch main (0ffd95)
by
unknown
01:57
created

UnitTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 25.68 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 19
loc 74
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A mockAPIStatus() 10 10 1
A mockElasticEmailAPIRequest() 9 9 1
A assertMiddlewarePushed() 0 6 1
A assertAPIRequestBodyHas() 0 12 1
A assertAPIRequestMultipartHas() 0 19 1
A assertAPIRequestQueryHas() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$container is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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