|
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; |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
29
|
|
|
$this->fail('ArrayLib::is_associative should not accept non array parameters'); |
|
30
|
|
|
} catch (\TypeError $e) {} |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
try { |
|
33
|
|
|
ArrayLib::is_associative('123'); |
|
|
|
|
|
|
34
|
|
|
$this->fail('ArrayLib::is_associative should not accept non array parameters'); |
|
35
|
|
|
} catch (\TypeError $e) {} |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
119
|
|
|
$this->fail('Curl::__construct parameters 2 and 3 should of type array only'); |
|
120
|
|
|
} catch (\TypeError $e) {} |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: