Passed
Push — main ( fd90d3...b4e90b )
by Dylan
02:22
created

UtilsTest::test_set_get_var()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Lifeboat\Tests;
4
5
use Lifeboat\Utils\ArrayLib;
6
use Lifeboat\Utils\Curl;
7
use Lifeboat\Utils\URL;
8
use Lifeboat\Utils\Utils;
9
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...
10
11
/**
12
 * Class UtilsTest
13
 * @package Lifeboat\Tests
14
 */
15
class UtilsTest extends TestCase {
16
17
    /**
18
     * @test
19
     * @covers \Lifeboat\Utils\ArrayLib::is_associative
20
     */
21
    public function test_is_associative_array()
22
    {
23
        $this->assertEquals(true, ArrayLib::is_associative(['a' => 1]));
24
        $this->assertEquals(false, ArrayLib::is_associative([1,2,3]));
25
        $this->assertEquals(false, ArrayLib::is_associative([]));
26
27
        try {
28
            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

28
            ArrayLib::is_associative(/** @scrutinizer ignore-type */ new \stdClass());
Loading history...
29
            $this->fail('ArrayLib::is_associative should not accept non array parameters');
30
        } catch (\TypeError $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
32
        try {
33
            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

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

118
            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

118
            new Curl('/url', /** @scrutinizer ignore-type */ '123', 'abc');
Loading history...
119
            $this->fail('Curl::__construct parameters 2 and 3 should of type array only');
120
        } catch (\TypeError $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
121
    }
122
}
123