RequestNotificationTypeTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace TonicHealthCheck\Test\Incident\Siren\NotificationType;
4
5
use Http\Client\Common\HttpMethodsClient;
6
use Http\Discovery\MessageFactoryDiscovery;
7
use Http\Mock\Client as MockClient;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use PHPUnit_Framework_TestCase;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamInterface;
12
use TonicHealthCheck\Incident\IncidentInterface;
13
use TonicHealthCheck\Incident\Siren\NotificationType\RequestNotificationType;
14
use TonicHealthCheck\Test\Incident\IncidentCreateTrait;
15
use TonicHealthCheck\Test\Incident\Subject\SubjectCreateTrait;
16
17
/**
18
 * Class RequestNotificationTypeTest.
19
 */
20
class RequestNotificationTypeTest extends PHPUnit_Framework_TestCase
21
{
22
    use SubjectCreateTrait;
23
    use IncidentCreateTrait;
24
25
    /**
26
     * @var PHPUnit_Framework_MockObject_MockObject|MockClient
27
     */
28
    private $mockClient;
29
30
    /**
31
     * @var HttpMethodsClient
32
     */
33
    private $httpClient;
34
35
    /**
36
     * @var RequestNotificationType;
37
     */
38
    private $requestNType;
39
40
    /**
41
     * @var string
42
     */
43
    private $resourceUrl = '/incident';
44
45
    /**
46
     * set up base env Request type test.
47
     */
48
    public function setUp()
49
    {
50
        $this->setMockClient(new MockClient());
51
52
        $this->setHttpClient(
53
            $this
54
                ->getMockBuilder(HttpMethodsClient::class)
55
                ->setConstructorArgs([
56
                    $this->getMockClient(),
57
                    MessageFactoryDiscovery::find(),
58
                ])
59
                ->enableProxyingToOriginalMethods()
60
                ->getMock()
61
        );
62
63
        $this->setRequestNType(new RequestNotificationType(
64
            $this->getHttpClient(),
0 ignored issues
show
Bug introduced by
It seems like $this->getHttpClient() targeting TonicHealthCheck\Test\In...peTest::getHttpClient() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TonicHealthCheck\Inciden...tionType::__construct() does only seem to accept object<Http\Client\Common\HttpMethodsClient>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
65
            $this->getResourceUrl()
66
        ));
67
    }
68
69
    /**
70
     * Test request notify create.
71
     */
72
    public function testNotifyCreate()
73
    {
74
        $this->assertEquals($this->getResourceUrl(), $this->getRequestNType()->getResourceUrl());
75
76
        $incident = $this->createIncidentMock();
77
78
        $incident
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in TonicHealthCheck\Incident\IncidentInterface.

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...
79
            ->expects($this->any())
80
            ->method('getStatus')
81
            ->willReturn(IncidentInterface::STATUS_OK + 1);
82
83
        $subject = $this->createSubject('target', '* * * * *');
84
85
        $streamMock = $this->getMockBuilder(StreamInterface::class)->getMock();
86
87
        $streamMock
88
            ->expects($this->once())
89
            ->method('getContents')
90
            ->willReturn('{"data":{"id":22}}');
91
92
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
93
94
        $response->expects($this->once())->method('getBody')->willReturn($streamMock);
95
96
        $this->getMockClient()->addResponse($response);
97
98
        $this->getHttpClient()
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Http\Client\Common\HttpMethodsClient.

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...
99
            ->expects($this->once())
100
            ->method('post')
101
            ->with(
102
                $this->identicalTo($subject->getTarget().$this->getResourceUrl()),
103
                $this->identicalTo(['Content-type' => 'application/json']),
104
                $this->identicalTo(
105
                    sprintf(
106
                        '{"name":"%s","message":"%s","status":1,"visible":1}',
107
                        $incident->getIdent(),
0 ignored issues
show
Bug introduced by
The method getIdent does only exist in TonicHealthCheck\Incident\IncidentInterface, but not in PHPUnit_Framework_MockObject_MockObject.

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...
108
                        $incident->getMessage()
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in TonicHealthCheck\Incident\IncidentInterface, but not in PHPUnit_Framework_MockObject_MockObject.

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...
109
                        )
110
                )
111
            );
112
113
        $this->getRequestNType()->notify($subject, $incident);
0 ignored issues
show
Bug introduced by
It seems like $incident defined by $this->createIncidentMock() on line 76 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TonicHealthCheck\Inciden...ificationType::notify() does only seem to accept object<TonicHealthCheck\...dent\IncidentInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
    }
115
116
    /**
117
     * Test request notify update.
118
     */
119
    public function testNotifyUpdate()
120
    {
121
        $incident = $this->createIncidentMock();
122
123
        $incident->expects($this->any())->method('getStatus')->willReturn(IncidentInterface::STATUS_OK);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in TonicHealthCheck\Incident\IncidentInterface.

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...
124
125
        $subject = $this->createSubject('target', '* * * * *');
126
127
        $this->getHttpClient()
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Http\Client\Common\HttpMethodsClient.

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...
128
            ->expects($this->once())
129
            ->method('put')
130
            ->with(
131
                $this->identicalTo($subject->getTarget().$this->getResourceUrl().'/'),
132
                $this->identicalTo(['Content-type' => 'application/json']),
133
                $this->identicalTo('{"status":4}')
134
            );
135
136
        $this->getRequestNType()->notify($subject, $incident);
0 ignored issues
show
Bug introduced by
It seems like $incident defined by $this->createIncidentMock() on line 121 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TonicHealthCheck\Inciden...ificationType::notify() does only seem to accept object<TonicHealthCheck\...dent\IncidentInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
137
    }
138
139
    /**
140
     * @return MockClient
141
     */
142
    protected function getMockClient()
143
    {
144
        return $this->mockClient;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->mockClient; of type PHPUnit_Framework_MockOb...Object|Http\Mock\Client adds the type PHPUnit_Framework_MockObject_MockObject to the return on line 144 which is incompatible with the return type documented by TonicHealthCheck\Test\In...TypeTest::getMockClient of type Http\Mock\Client.
Loading history...
145
    }
146
147
    /**
148
     * @param MockClient $mockClient
149
     */
150
    protected function setMockClient(MockClient $mockClient)
151
    {
152
        $this->mockClient = $mockClient;
153
    }
154
155
    /**
156
     * @return PHPUnit_Framework_MockObject_MockObject|HttpMethodsClient
157
     */
158
    protected function getHttpClient()
159
    {
160
        return $this->httpClient;
161
    }
162
163
    /**
164
     * @param HttpMethodsClient $httpClient
165
     */
166
    protected function setHttpClient(HttpMethodsClient $httpClient)
167
    {
168
        $this->httpClient = $httpClient;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    protected function getResourceUrl()
175
    {
176
        return $this->resourceUrl;
177
    }
178
179
    /**
180
     * @return RequestNotificationType
181
     */
182
    protected function getRequestNType()
183
    {
184
        return $this->requestNType;
185
    }
186
187
    /**
188
     * @param RequestNotificationType $requestNType
189
     */
190
    protected function setRequestNType(RequestNotificationType $requestNType)
191
    {
192
        $this->requestNType = $requestNType;
193
    }
194
}
195