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

ProfilePluginTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 9.1127
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\Common\Plugin;
8
use Http\Client\Exception\TransferException;
9
use Http\HttplugBundle\Collector\Collector;
10
use Http\HttplugBundle\Collector\Formatter;
11
use Http\HttplugBundle\Collector\ProfilePlugin;
12
use Http\HttplugBundle\Collector\Stack;
13
use Http\Promise\FulfilledPromise;
14
use Http\Promise\Promise;
15
use Http\Promise\RejectedPromise;
16
use PHPUnit\Framework\TestCase;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
20
class ProfilePluginTest extends TestCase
21
{
22
    /**
23
     * @var Plugin
24
     */
25
    private $plugin;
26
27
    /**
28
     * @var Collector
29
     */
30
    private $collector;
31
32
    /**
33
     * @var RequestInterface
34
     */
35
    private $request;
36
37
    /**
38
     * @var ResponseInterface
39
     */
40
    private $response;
41
42
    /**
43
     * @var Promise
44
     */
45
    private $fulfilledPromise;
46
47
    /**
48
     * @var Stack
49
     */
50
    private $currentStack;
51
52
    /**
53
     * @var TransferException
54
     */
55
    private $exception;
56
57
    /**
58
     * @var Promise
59
     */
60
    private $rejectedPromise;
61
62
    /**
63
     * @var Formatter
64
     */
65
    private $formatter;
66
67
    /**
68
     * @var ProfilePlugin
69
     */
70
    private $subject;
71
72
    public function setUp()
73
    {
74
        $this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
75
        $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
76
        $this->request = new Request('GET', '/');
77
        $this->response = new Response();
78
        $this->fulfilledPromise = new FulfilledPromise($this->response);
79
        $this->currentStack = new Stack('default', 'FormattedRequest');
80
        $this->exception = new TransferException();
81
        $this->rejectedPromise = new RejectedPromise($this->exception);
82
        $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
83
84
        $this->collector
85
            ->method('getActiveStack')
86
            ->willReturn($this->currentStack)
87
        ;
88
89
        $this->plugin
90
            ->method('handleRequest')
91
            ->willReturnCallback(function ($request, $next, $first) {
0 ignored issues
show
Unused Code introduced by
The parameter $first is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
                return $next($request);
93
            })
94
        ;
95
96
        $this->formatter
97
            ->method('formatRequest')
98
            ->with($this->identicalTo($this->request))
99
            ->willReturn('FormattedRequest')
100
        ;
101
102
        $this->formatter
103
            ->method('formatResponse')
104
            ->with($this->identicalTo($this->response))
105
            ->willReturn('FormattedResponse')
106
        ;
107
108
        $this->formatter
109
            ->method('formatException')
110
            ->with($this->identicalTo($this->exception))
111
            ->willReturn('FormattedException')
112
        ;
113
114
        $this->subject = new ProfilePlugin(
115
            $this->plugin,
116
            $this->collector,
117
            $this->formatter,
118
            'http.plugin.mock'
0 ignored issues
show
Unused Code introduced by
The call to ProfilePlugin::__construct() has too many arguments starting with 'http.plugin.mock'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
        );
120
    }
121
122
    public function testCallDecoratedPlugin()
123
    {
124
        $this->plugin
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\Client\Common\Plugin>.

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...
125
            ->expects($this->once())
126
            ->method('handleRequest')
127
            ->with($this->request)
128
        ;
129
130
        $this->subject->handleRequest($this->request, function () {
131
            return $this->fulfilledPromise;
132
        }, function () {
133
        });
134
    }
135
136
    public function testProfileIsInitialized()
137
    {
138
        $this->subject->handleRequest($this->request, function () {
139
            return $this->fulfilledPromise;
140
        }, function () {
141
        });
142
143
        $this->assertCount(1, $this->currentStack->getProfiles());
144
        $profile = $this->currentStack->getProfiles()[0];
145
        $this->assertEquals(get_class($this->plugin), $profile->getPlugin());
146
    }
147
148
    public function testCollectRequestInformations()
149
    {
150
        $this->subject->handleRequest($this->request, function () {
151
            return $this->fulfilledPromise;
152
        }, function () {
153
        });
154
155
        $profile = $this->currentStack->getProfiles()[0];
156
        $this->assertEquals('FormattedRequest', $profile->getRequest());
157
    }
158
159 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...
160
    {
161
        $promise = $this->subject->handleRequest($this->request, function () {
162
            return $this->fulfilledPromise;
163
        }, function () {
164
        });
165
166
        $this->assertEquals($this->response, $promise->wait());
167
        $profile = $this->currentStack->getProfiles()[0];
168
        $this->assertEquals('FormattedResponse', $profile->getResponse());
169
    }
170
171
    /**
172
     * @expectedException \Http\Client\Exception\TransferException
173
     */
174 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...
175
    {
176
        $promise = $this->subject->handleRequest($this->request, function () {
177
            return $this->rejectedPromise;
178
        }, function () {
179
        });
180
181
        $this->assertEquals($this->exception, $promise->wait());
182
        $profile = $this->currentStack->getProfiles()[0];
183
        $this->assertEquals('FormattedException', $profile->getResponse());
184
    }
185
}
186