Completed
Push — master ( a67a8b...0faee6 )
by Stéphane
07:30
created

ClientTest::testEmptyUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the beebot package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author    Stephane HULARD <[email protected]>
9
 * @package   Bee4\Test\Transport
10
 */
11
12
namespace Bee4\Test\Transport;
13
14
use Bee4\Events\Adapters\EvenementEventEmitterAdapter;
15
use Evenement\EventEmitter;
16
use Bee4\PHPUnit\HttpClientTestCase;
17
use Bee4\Transport\Client;
18
use Bee4\Transport\MagicHandler;
19
use Bee4\Transport\Events\MessageEvent;
20
use Bee4\Transport\Events\ErrorEvent;
21
22
/**
23
 * Transfer client test
24
 * @package Bee4\Test\Transport
25
 */
26
class ClientTest extends HttpClientTestCase
27
{
28
    /**
29
     * @var Client
30
     */
31
    protected $object;
32
33
    /**
34
     * Sets up the fixture, for example, opens a network connection.
35
     * This method is called before a test is executed.
36
     */
37
    protected function setUp()
38
    {
39
        if (!extension_loaded('curl')) {
40
            $this->markTestSkipped('The curl extension is not available.');
41
        }
42
43
        $this->object = new MagicHandler(new Client(self::getBaseUrl()));
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Bee4\Transport\Magi...nt(self::getBaseUrl())) of type object<Bee4\Transport\MagicHandler> is incompatible with the declared type object<Bee4\Transport\Client> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * @expectedException \InvalidArgumentException
48
     */
49
    public function testNonStringUrl()
50
    {
51
        $method = new \ReflectionMethod('\Bee4\Transport\Client', 'createRequest');
52
        $method->setAccessible(true);
53
        $method->invoke(new Client(), 'get', new \stdClass());
54
    }
55
56
    /**
57
     * @expectedException \InvalidArgumentException
58
     */
59
    public function testEmptyUrl()
60
    {
61
        $method = new \ReflectionMethod('\Bee4\Transport\Client', 'createRequest');
62
        $method->setAccessible(true);
63
        $method->invoke(new Client(), 'post', '');
64
    }
65
66
    public function testSend()
67
    {
68
        //Check that Post request is nicely mapped
69
        $request1 = $this->object->get('/index.html');
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Bee4\Transport\Client>.

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...
70
        $this->assertEquals(self::getBaseUrl().'/index.html', (string)$request1->getUrl());
71
72
        $this->assertInstanceOf('\Bee4\Transport\Message\AbstractMessage', $request1);
73
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\AbstractRequest', $request1);
74
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Get', $request1);
75
        $this->assertInstanceOf('\Bee4\Transport\Message\Response', $request1->send());
76
77
        //Check that Post request is nicely mapped
78
        $request2 = $this->object->post('/index.html');
0 ignored issues
show
Bug introduced by
The method post() does not seem to exist on object<Bee4\Transport\Client>.

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...
79
        $this->assertInstanceOf('\Bee4\Transport\Message\AbstractMessage', $request2);
80
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\AbstractRequest', $request2);
81
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Post', $request2);
82
        $this->assertInstanceOf('\Bee4\Transport\Message\Response', $request2->send());
83
    }
84
85 View Code Duplication
    public function testGet()
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...
86
    {
87
        $request = $this->object->get('/index.html');
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Bee4\Transport\Client>.

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...
88
        $response = $request->send();
89
        $options = $request->getOptions();
90
91
        $this->assertArrayHasKey(CURLOPT_HTTPGET, $options);
92
        $this->assertTrue($options[CURLOPT_HTTPGET]);
93
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Get', $response->getRequest());
94
    }
95
96 View Code Duplication
    public function testPost()
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...
97
    {
98
        $request = $this->object->post('/index.html')->setBody('{"body": "I\'m the body"}}');
0 ignored issues
show
Bug introduced by
The method post() does not seem to exist on object<Bee4\Transport\Client>.

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...
99
        $response = $request->send();
100
        $options = $request->getOptions();
101
102
        $this->assertArrayHasKey(CURLOPT_POST, $options);
103
        $this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options);
104
        $this->assertTrue($options[CURLOPT_POST]);
105
        $this->assertEquals('{"body": "I\'m the body"}}', $options[CURLOPT_POSTFIELDS]);
106
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Post', $response->getRequest());
107
    }
108
109 View Code Duplication
    public function testHead()
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...
110
    {
111
        $request = $this->object->head('/index.html');
0 ignored issues
show
Bug introduced by
The method head() does not seem to exist on object<Bee4\Transport\Client>.

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...
112
        $response = $request->send();
113
        $options = $request->getOptions();
114
115
        $this->assertArrayHasKey(CURLOPT_NOBODY, $options);
116
        $this->assertTrue($options[CURLOPT_NOBODY]);
117
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Head', $response->getRequest());
118
    }
119
120 View Code Duplication
    public function testDelete()
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...
121
    {
122
        $request = $this->object->delete('/index.html');
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Bee4\Transport\Client>.

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...
123
        $response = $request->send();
124
        $options = $request->getOptions();
125
126
        $this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options);
127
        $this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options);
128
        $this->assertEquals('DELETE', $options[CURLOPT_CUSTOMREQUEST]);
129
        $this->assertEquals(false, $options[CURLOPT_POSTFIELDS]);
130
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Delete', $response->getRequest());
131
    }
132
133 View Code Duplication
    public function testPutFromString()
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...
134
    {
135
        $request = $this->object->put('/index.html');
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Bee4\Transport\Client>.

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...
136
        $response = $request->send();
137
        $options = $request->getOptions();
138
139
        $this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options);
140
        $this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options);
141
        $this->assertEquals('PUT', $options[CURLOPT_CUSTOMREQUEST]);
142
        $this->assertEquals(false, $options[CURLOPT_POSTFIELDS]);
143
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Put', $response->getRequest());
144
    }
145
146
    public function testPutFromStream()
147
    {
148
        $stream = tmpfile();
149
        $size = 200;
150
        $content = str_repeat('*', $size);
151
152
        fwrite($stream, $content);
153
154
        $request = $this->object->put('/index.html');
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Bee4\Transport\Client>.

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...
155
        $request->setBody($stream);
156
        $response = $request->send();
157
        $options = $request->getOptions();
158
159
        $this->assertArrayHasKey(CURLOPT_PUT, $options);
160
        $this->assertArrayHasKey(CURLOPT_INFILE, $options);
161
        $this->assertArrayHasKey(CURLOPT_INFILESIZE, $options);
162
        $this->assertEquals(true, $options[CURLOPT_PUT]);
163
        $this->assertEquals($stream, $options[CURLOPT_INFILE]);
164
        $this->assertEquals($size, $options[CURLOPT_INFILESIZE]);
165
        $this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Put', $response->getRequest());
166
    }
167
168
    /**
169
     * @expectedException \Exception
170
     * @expectedExceptionMessage Yes event triggered
171
     */
172
    public function testRegister()
173
    {
174
        $dispatcher = new EvenementEventEmitterAdapter(new EventEmitter);
175
        $dispatcher->on(MessageEvent::REQUEST, function () {
176
            throw new \Exception("Yes event triggered");
177
        });
178
179
        $this->object->setDispatcher($dispatcher);
180
        $this->object->get('/index.html')->send();
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Bee4\Transport\Client>.

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...
181
    }
182
183
    public function testInvalidProtocol()
184
    {
185
        $this->object = new Client("unmapped://127.0.0.1");
186
187
        try {
188
            $this->object->createRequest('GET')->send();
189
        } catch (\Exception $error) {
190
            $this->assertInstanceOf("\\Bee4\\Transport\\Exception\\UnknownProtocolException", $error);
191
            return;
192
        }
193
        $this->fail();
194
    }
195
196
    public function testCurlError()
197
    {
198
        $this->expectOutputString('error');
199
        $this->object = new Client("ftp://127.0.0.1:8888");
200
201
        $dispatcher = new EvenementEventEmitterAdapter(new EventEmitter);
202
        $dispatcher->on(ErrorEvent::ERROR, function () {
203
            echo "error";
204
        });
205
        $this->object->setDispatcher($dispatcher);
206
207
        try {
208
            $this->object->createRequest('GET')->send();
209
        } catch (\Exception $error) {
210
            $this->assertInstanceOf("\\Bee4\\Transport\\Exception\\CurlException", $error);
211
            return;
212
        }
213
        $this->fail();
214
    }
215
}
216