Completed
Push — master ( b587fb...d05392 )
by David
20:34
created

StackPluginTest::testOnException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Http\HttplugBundle\Tests\Unit\Collector;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use Http\Client\Exception\HttpException;
8
use Http\HttplugBundle\Collector\Collector;
9
use Http\HttplugBundle\Collector\Formatter;
10
use Http\HttplugBundle\Collector\Stack;
11
use Http\HttplugBundle\Collector\StackPlugin;
12
use Http\Promise\FulfilledPromise;
13
use Http\Promise\RejectedPromise;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
class StackPluginTest extends TestCase
19
{
20
    /**
21
     * @var Collector
22
     */
23
    private $collector;
24
25
    /**
26
     * @var Formatter
27
     */
28
    private $formatter;
29
30
    /**
31
     * @var RequestInterface
32
     */
33
    private $request;
34
35
    /**
36
     * @var ResponseInterface
37
     */
38
    private $response;
39
40
    /**
41
     * @var \Exception
42
     */
43
    private $exception;
44
45
    /**
46
     * @var StackPlugin
47
     */
48
    private $subject;
49
50
    public function setUp()
51
    {
52
        $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
53
        $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
54
        $this->request = new Request('GET', '/');
55
        $this->response = new Response();
56
        $this->exception = new HttpException('', $this->request, $this->response);
57
58
        $this->formatter
59
            ->method('formatRequest')
60
            ->with($this->request)
61
            ->willReturn('FormattedRequest')
62
        ;
63
64
        $this->formatter
65
            ->method('formatResponse')
66
            ->with($this->response)
67
            ->willReturn('FormattedResponse')
68
        ;
69
70
        $this->formatter
71
            ->method('formatException')
72
            ->with($this->exception)
73
            ->willReturn('FormattedException')
74
        ;
75
76
        $this->subject = new StackPlugin($this->collector, $this->formatter, 'default');
77
    }
78
79
    public function testStackIsInitialized()
80
    {
81
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            ->expects($this->once())
83
            ->method('addStack')
84
            ->with($this->callback(function (Stack $stack) {
85
                $this->assertEquals('default', $stack->getClient());
86
                $this->assertEquals('FormattedRequest', $stack->getRequest());
87
88
                return true;
89
            }))
90
        ;
91
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            ->expects($this->once())
93
            ->method('activateStack')
94
        ;
95
96
        $next = function () {
97
            return new FulfilledPromise($this->response);
98
        };
99
100
        $this->subject->handleRequest($this->request, $next, function () {
101
        });
102
    }
103
104 View Code Duplication
    public function testOnFulfilled()
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...
105
    {
106
        //Capture the current stack
107
        $currentStack = null;
108
        $this->collector
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            ->method('addStack')
110
            ->with($this->callback(function (Stack $stack) use (&$currentStack) {
111
                $currentStack = $stack;
112
113
                return true;
114
            }))
115
        ;
116
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            ->expects($this->once())
118
            ->method('deactivateStack')
119
        ;
120
121
        $next = function () {
122
            return new FulfilledPromise($this->response);
123
        };
124
125
        $promise = $this->subject->handleRequest($this->request, $next, function () {
126
        });
127
128
        $this->assertEquals($this->response, $promise->wait());
129
        $this->assertInstanceOf(Stack::class, $currentStack);
130
        $this->assertEquals('FormattedResponse', $currentStack->getResponse());
0 ignored issues
show
Bug introduced by
The method getResponse cannot be called on $currentStack (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
131
    }
132
133
    /**
134
     * @expectedException \Exception
135
     */
136 View Code Duplication
    public function testOnRejected()
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...
137
    {
138
        //Capture the current stack
139
        $currentStack = null;
140
        $this->collector
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
            ->method('addStack')
142
            ->with($this->callback(function (Stack $stack) use (&$currentStack) {
143
                $currentStack = $stack;
144
145
                return true;
146
            }))
147
        ;
148
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
            ->expects($this->once())
150
            ->method('deactivateStack')
151
        ;
152
153
        $next = function () {
154
            return new RejectedPromise($this->exception);
155
        };
156
157
        $promise = $this->subject->handleRequest($this->request, $next, function () {
158
        });
159
160
        $this->assertEquals($this->exception, $promise->wait());
161
        $this->assertInstanceOf(Stack::class, $currentStack);
162
        $this->assertEquals('FormattedException', $currentStack->getResponse());
0 ignored issues
show
Bug introduced by
The method getResponse cannot be called on $currentStack (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
163
        $this->assertTrue($currentStack->isFailed());
0 ignored issues
show
Bug introduced by
The method isFailed cannot be called on $currentStack (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
164
    }
165
166
    /**
167
     * @expectedException \Exception
168
     */
169
    public function testOnException()
170
    {
171
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
            ->expects($this->once())
173
            ->method('deactivateStack')
174
        ;
175
176
        $next = function () {
177
            throw new \Exception();
178
        };
179
180
        $this->subject->handleRequest($this->request, $next, function () {
181
        });
182
    }
183
184
    public function testOnError()
185
    {
186
        if (PHP_VERSION_ID <= 70000) {
187
            $this->markTestSkipped();
188
        }
189
190
        /*
191
         * Use the correct PHPUnit version
192
         * PHPUnit wrap any \Error into a \PHPUnit\Framework\Error\Warning.
193
         */
194
        if (class_exists('PHPUnit_Framework_Error')) {
195
            // PHPUnit 5.7
196
            $this->setExpectedException(\PHPUnit_Framework_Error::class);
197
        } else {
198
            // PHPUnit 6.0 and above
199
            $this->expectException(\PHPUnit\Framework\Error\Warning::class);
200
        }
201
202
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
            ->expects($this->once())
204
            ->method('deactivateStack')
205
        ;
206
207
        $next = function () {
208
            return 2 / 0;
209
        };
210
211
        $this->subject->handleRequest($this->request, $next, function () {
212
        });
213
    }
214
}
215