FormatterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testFormatRequest() 0 12 1
A testFormatResponse() 0 12 1
A testFormatHttpException() 0 15 1
A testFormatTransferException() 0 6 1
A testFormatException() 0 5 1
A testFormatAsCurlCommand() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Unit\Collector;
6
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use Http\Client\Exception\HttpException;
10
use Http\Client\Exception\TransferException;
11
use Http\HttplugBundle\Collector\Formatter;
12
use Http\Message\Formatter as MessageFormatter;
13
use Http\Message\Formatter\CurlCommandFormatter;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
class FormatterTest extends TestCase
18
{
19
    /**
20
     * @var MessageFormatter|MockObject
21
     */
22
    private $formatter;
23
24
    /**
25
     * @var CurlCommandFormatter|MockObject
26
     */
27
    private $curlFormatter;
28
29
    /**
30
     * @var Formatter
31
     */
32
    private $subject;
33
34
    public function setUp(): void
35
    {
36
        $this->formatter = $this->getMockBuilder(MessageFormatter::class)->getMock();
37
        $this->curlFormatter = $this->getMockBuilder(CurlCommandFormatter::class)->getMock();
38
39
        $this->subject = new Formatter($this->formatter, $this->curlFormatter);
0 ignored issues
show
Documentation introduced by
$this->formatter is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\Message\Formatter>.

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...
Documentation introduced by
$this->curlFormatter is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\Message\Form...r\CurlCommandFormatter>.

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
    public function testFormatRequest(): void
43
    {
44
        $request = new Request('GET', '/');
45
46
        $this->formatter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Message\Formatter.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            ->expects($this->once())
48
            ->method('formatRequest')
49
            ->with($this->identicalTo($request))
50
        ;
51
52
        $this->subject->formatRequest($request);
53
    }
54
55
    public function testFormatResponse(): void
56
    {
57
        $response = new Response();
58
59
        $this->formatter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Message\Formatter.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
60
            ->expects($this->once())
61
            ->method('formatResponse')
62
            ->with($this->identicalTo($response))
63
        ;
64
65
        $this->subject->formatResponse($response);
66
    }
67
68
    public function testFormatHttpException(): void
69
    {
70
        $request = new Request('GET', '/');
71
        $response = new Response();
72
        $exception = new HttpException('', $request, $response);
73
74
        $this->formatter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Message\Formatter.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
            ->expects($this->once())
76
            ->method('formatResponse')
77
            ->with($this->identicalTo($response))
78
            ->willReturn('FormattedException')
79
        ;
80
81
        $this->assertEquals('FormattedException', $this->subject->formatException($exception));
82
    }
83
84
    public function testFormatTransferException(): void
85
    {
86
        $exception = new TransferException('ExceptionMessage');
87
88
        $this->assertEquals('Transfer error: ExceptionMessage', $this->subject->formatException($exception));
89
    }
90
91
    public function testFormatException(): void
92
    {
93
        $exception = new \RuntimeException('Unexpected error');
94
        $this->assertEquals('Unexpected exception of type "RuntimeException": Unexpected error', $this->subject->formatException($exception));
95
    }
96
97
    public function testFormatAsCurlCommand(): void
98
    {
99
        $request = new Request('GET', '/');
100
101
        $this->curlFormatter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Message\Formatter\CurlCommandFormatter.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
            ->expects($this->once())
103
            ->method('formatRequest')
104
            ->with($this->identicalTo($request))
105
            ->willReturn('curl -L http://example.com')
106
        ;
107
108
        $this->assertEquals('curl -L http://example.com', $this->subject->formatAsCurlCommand($request));
109
    }
110
}
111