Test Failed
Push — main ( b4e90b...e2e1c9 )
by Dylan
02:11
created

UtilsTest::test_set_curl_method()   A

Complexity

Conditions 3
Paths 15

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 23
rs 9.7666
c 1
b 0
f 0
cc 3
nc 15
nop 0
1
<?php
2
3
namespace Lifeboat\Tests;
4
5
use Lifeboat\CurlResponse;
6
use Lifeboat\Exceptions\InvalidArgumentException;
7
use Lifeboat\Utils\ArrayLib;
8
use Lifeboat\Utils\Curl;
9
use Lifeboat\Utils\URL;
10
use Lifeboat\Utils\Utils;
11
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Lifeboat\Tests\TestCase. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
/**
14
 * Class UtilsTest
15
 * @package Lifeboat\Tests
16
 */
17
class UtilsTest extends TestCase {
18
19
    /**
20
     * @test
21
     * @covers \Lifeboat\Utils\ArrayLib::is_associative
22
     */
23
    public function test_is_associative_array()
24
    {
25
        $this->assertEquals(true, ArrayLib::is_associative(['a' => 1]));
26
        $this->assertEquals(false, ArrayLib::is_associative([1,2,3]));
27
        $this->assertEquals(false, ArrayLib::is_associative([]));
28
29
        try {
30
            ArrayLib::is_associative(new \stdClass());
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type array expected by parameter $array of Lifeboat\Utils\ArrayLib::is_associative(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            ArrayLib::is_associative(/** @scrutinizer ignore-type */ new \stdClass());
Loading history...
31
            $this->fail('ArrayLib::is_associative should not accept non array parameters');
32
        } catch (\TypeError $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
34
        try {
35
            ArrayLib::is_associative('123');
0 ignored issues
show
Bug introduced by
'123' of type string is incompatible with the type array expected by parameter $array of Lifeboat\Utils\ArrayLib::is_associative(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            ArrayLib::is_associative(/** @scrutinizer ignore-type */ '123');
Loading history...
36
            $this->fail('ArrayLib::is_associative should not accept non array parameters');
37
        } catch (\TypeError $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
38
    }
39
40
    /**
41
     * @test
42
     * @covers \Lifeboat\Utils\URL::is_absolute_url
43
     */
44
    public function test_is_absolute()
45
    {
46
        $this->assertFalse(URL::is_absolute_url('/'));
47
        $this->assertFalse(URL::is_absolute_url('/123'));
48
        $this->assertFalse(URL::is_absolute_url('/123?a'));
49
        $this->assertFalse(URL::is_absolute_url('local.test/123?a'));
50
        $this->assertFalse(URL::is_absolute_url('/#'));
51
52
        $this->assertTrue(URL::is_absolute_url('://local.test'));
53
        $this->assertTrue(URL::is_absolute_url('https://local.test'));
54
        $this->assertTrue(URL::is_absolute_url('https://local.test/'));
55
        $this->assertTrue(URL::is_absolute_url('https://local.test?a'));
56
        $this->assertTrue(URL::is_absolute_url('https://local.test/?a'));
57
        $this->assertTrue(URL::is_absolute_url('https://local.test/#'));
58
        $this->assertTrue(URL::is_absolute_url('https://local.test#'));
59
        $this->assertTrue(URL::is_absolute_url('https://user:[email protected]#'));
60
    }
61
62
    /**
63
     * @test
64
     * @covers \Lifeboat\Utils\URL::setGetVar
65
     */
66
    public function test_set_get_var()
67
    {
68
        $rel_path = '/path';
69
        $this->assertEquals($rel_path . '?var=1', URL::setGetVar('var', 1, $rel_path));
70
71
        $rel_path_t = '/path/';
72
        $this->assertEquals($rel_path_t . '?var=1', URL::setGetVar('var', 1, $rel_path_t));
73
74
        $absolute = 'https://test.com';
75
        $this->assertEquals($absolute . '?var=1', URL::setGetVar('var', 1, $absolute));
76
77
        $absolute_t = 'https://test.com/';
78
        $this->assertEquals($absolute_t . '?var=1', URL::setGetVar('var', 1, $absolute_t));
79
    }
80
81
    /**
82
     * @test
83
     * @covers \Lifeboat\Utils\Utils::create_random_string
84
     */
85
    public function test_create_random_string()
86
    {
87
        // Test the length of the generated string
88
        $this->assertTrue(strlen(Utils::create_random_string()) === 24);
89
90
        // Test that only the supplied characters are used
91
        $this->assertTrue(intval(Utils::create_random_string(2, '123456789')) > 0);
92
93
        // Test the randomness
94
        $generated = [];
95
        while (count($generated) < 100000) $generated[] = Utils::create_random_string();
96
        $count  = count($generated);
97
        $unique = count(array_unique($generated));
98
        $random = 100 - ((100 / $count) * $unique);
99
100
        // Less than 2% repeat
101
        $this->assertTrue($random < 2);
102
    }
103
104
    /**
105
     * @test
106
     * @covers \Lifeboat\Utils\Curl::__construct
107
     * @covers \Lifeboat\Utils\Curl::getURL
108
     * @covers \Lifeboat\Utils\Curl::setURL
109
     * @covers \Lifeboat\Utils\Curl::addDataParam
110
     * @covers \Lifeboat\Utils\Curl::getDataParams
111
     * @covers \Lifeboat\Utils\Curl::addHeader
112
     * @covers \Lifeboat\Utils\Curl::getHeaders
113
     * @covers \Lifeboat\Utils\Curl::removeHeader
114
     * @covers \Lifeboat\Utils\Curl::isFileUpload
115
     * @covers \Lifeboat\Utils\Curl::setIsFileUpload
116
     * @covers \Lifeboat\Utils\Curl::removeDataParam
117
     */
118
    public function test_curl_construct()
119
    {
120
        $params     = ['a' => 1];
121
        $headers    = ['header' => 'value'];
122
        $curl       = new Curl('/url', $params, $headers);
123
124
        // Remove the default headers
125
        $curl->removeHeader('Content-Type');
126
        $curl->removeHeader('X-Requested-By');
127
128
        $this->assertEquals('/url', $curl->getURL());
129
        $this->assertEquals($headers, $curl->getHeaders());
130
        $this->assertEquals($params, $curl->getDataParams());
131
132
        $curl->removeDataParam('a');
133
        $this->assertEquals([], $curl->getDataParams());
134
135
        $this->assertFalse($curl->isFileUpload());
136
        $curl->setIsFileUpload(true);
137
        $this->assertTrue($curl->isFileUpload());
138
139
        $headers_set = $curl->getHeaders();
140
        if (!array_key_exists('Content-Type', $headers_set) ||
141
            $headers_set['Content-Type'] !== 'multipart/form-data') {
142
            $this->fail('Curl::setIsFileUpload() did not set the correct headers');
143
        }
144
145
        try {
146
            new Curl('/url', '123', 'abc');
0 ignored issues
show
Bug introduced by
'abc' of type string is incompatible with the type array expected by parameter $headers of Lifeboat\Utils\Curl::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

146
            new Curl('/url', '123', /** @scrutinizer ignore-type */ 'abc');
Loading history...
Bug introduced by
'123' of type string is incompatible with the type array expected by parameter $data of Lifeboat\Utils\Curl::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

146
            new Curl('/url', /** @scrutinizer ignore-type */ '123', 'abc');
Loading history...
147
            $this->fail('Curl::__construct parameters 2 and 3 should of type array only');
148
        } catch (\TypeError $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
149
    }
150
151
    /**
152
     * @test
153
     * @covers \Lifeboat\Utils\Curl::setMethod
154
     * @covers \Lifeboat\Utils\Curl::getMethod
155
     */
156
    public function test_set_curl_method()
157
    {
158
        $curl = new Curl('/');
159
160
        $this->assertEquals('GET', $curl->getMethod());
161
162
        // Set method should be case insensitive
163
        $curl->setMethod('pOsT');
164
        $this->assertEquals('POST', $curl->getMethod());
165
166
        try {
167
            $curl->setMethod('GET');
168
            $curl->setMethod('POST');
169
            $curl->setMethod('PUT');
170
            $curl->setMethod('DELETE');
171
        } catch (InvalidArgumentException $e) {
172
            $this->fail('Curl::setMethod should accept GET,POST,PUT,DELETE');
173
        }
174
175
        try {
176
            $curl->setMethod('X');
177
            $this->fail('Curl::setMethod should only accept valid HTTP methods');
178
        } catch (InvalidArgumentException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
179
    }
180
181
    /**
182
     * @test
183
     *
184
     * @covers \Lifeboat\CurlResponse::__construct
185
     * @covers \Lifeboat\CurlResponse::getRaw
186
     * @covers \Lifeboat\CurlResponse::getHTTPCode
187
     * @covers \Lifeboat\CurlResponse::isValid
188
     * @covers \Lifeboat\CurlResponse::isJSON
189
     * @covers \Lifeboat\CurlResponse::getError
190
     * @covers \Lifeboat\CurlResponse::getErrors
191
     */
192
    public function test_curl_response()
193
    {
194
        // Non-JSON
195
        $response = new CurlResponse(204, 'No Content');
196
        $this->assertEquals('No Content', $response->getRaw());
197
        $this->assertEquals(204, $response->getHTTPCode());
198
        $this->assertTrue($response->isValid());
199
        $this->assertFalse($response->isJSON());
200
201
        // JSON
202
        $json = new CurlResponse(400, '{"errors":[{"error":"xxx"}]}');
203
        $this->assertFalse($json->isValid());
204
        $this->assertTrue($json->isJSON());
205
        $this->assertEquals([['error' => 'xxx']], $json->getErrors());
206
        $this->assertEquals([['error' => 'xxx']], $json->getJSON());
207
        $this->assertEquals('xxx', $json->getError());
208
    }
209
}
210