Completed
Push — master ( 298f36...c6c82d )
by Mihail
06:19 queued 10s
created

FunctionsTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 186
rs 10
wmc 15
1
<?php
2
3
namespace Koded\Stdlib;
4
5
use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset;
6
use PHPUnit\Framework\TestCase;
7
8
class FunctionsTest extends TestCase
9
{
10
11
    public function test_value_function()
12
    {
13
        $value = value([1, 2, 3]);
14
        $this->assertInstanceOf(Immutable::class, $value);
15
        $this->assertSame([1, 2, 3], $value->toArray());
16
    }
17
18
    public function test_arguments_function()
19
    {
20
        $value = arguments([1, 2, 3]);
21
        $this->assertInstanceOf(Arguments::class, $value);
22
        $this->assertSame([1, 2, 3], $value->toArray());
23
24
        $value->foo = 'bar';
25
        $this->assertSame([1, 2, 3, 'foo' => 'bar'], $value->toArray());
26
    }
27
28
    public function test_htmlescape_function()
29
    {
30
        $value = htmlencode('<script>');
31
        $this->assertSame('&lt;script&gt;', $value);
32
    }
33
34
    /**
35
     * @dataProvider camelCaseData
36
     */
37
    public function test_camel_case_function($string, $expected)
38
    {
39
        $this->assertSame($expected, snake_to_camel_case($string));
40
    }
41
42
    /**
43
     * @dataProvider snakeCaseData
44
     */
45
    public function test_snake_case_function($string, $expected)
46
    {
47
        $this->assertSame($expected, camel_to_snake_case($string));
48
    }
49
50
    /**
51
     * @dataProvider isAssociativeArrayData
52
     */
53
    public function test_is_array_assoc_function($array, $expected)
54
    {
55
        $this->assertSame($expected, is_associative($array));
56
    }
57
58
    public function test_now_function()
59
    {
60
        $now = now();
61
        $this->assertSame('UTC', $now->getTimezone()->getName());
62
63
        $timestamp = now()->getTimestamp();
64
        $duration = new \DateInterval('PT12H');
65
66
        $now->add($duration);
67
        $this->assertEquals($timestamp, $now->getTimestamp(), 'The DateTime object is not changed');
68
69
        $after12hours = $now->add($duration);
70
        $this->assertNotSame($now, $after12hours, 'now() is immutable');
71
        $this->assertEquals(60 * 60 * 12, $after12hours->getTimestamp() - $timestamp);
72
    }
73
74
75
    public function test_rmdir_function()
76
    {
77
        $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . randomstring(9);
78
        $file = $dir . DIRECTORY_SEPARATOR . randomstring(9) . '.txt';
79
80
        $this->assertTrue(mkdir($dir), 'Should create an empty directory');
81
        $this->assertSame(4, file_put_contents($file, 'test'), 'Should create the file in the directory');
82
        $this->assertFileExists($file, 'Should add file in the directory');
83
84
        $this->assertTrue(rmdir($dir));
85
        $this->assertFileNotExists($file, 'Directories with files are deleted');
86
87
        $this->assertDirectoryNotExists($dir . '/foo');
88
        $this->assertTrue(rmdir($dir), 'The non-existent directories are not processed at all');
89
    }
90
91
92
    /**
93
     * @dataProvider forDelimiterTest
94
     */
95
    public function test_to_delimited_string($string, $delimiter, $expected)
96
    {
97
        $this->assertSame($expected, to_delimited_string($string, ord($delimiter)));
98
    }
99
100
    /**
101
     * @dataProvider forKebabTest
102
     */
103
    public function test_to_kebab_string($string, $expected)
104
    {
105
        $this->assertSame($expected, to_kebab_string($string));
106
    }
107
108
    /*
109
     *
110
     * Data providers
111
     *
112
     */
113
114
    public function camelCaseData()
115
    {
116
        return [
117
            [' excessive    Spaces  are    removed', 'ExcessiveSpacesAreRemoved'],
118
            ['all 123 numbers 456 are 789 preserved', 'All123Numbers456Are789Preserved'],
119
            ['the-dashes_and_underscores_are-removed ', 'TheDashesAndUnderscoresAreRemoved'],
120
            ['non alpha-numeric 4*z characters #1q ARE "removed"', 'NonAlphaNumeric4ZCharacters1qARERemoved'],
121
            ['th*1s-is%-ridic&&&&ulous', 'Th1sIsRidicUlous'],
122
        ];
123
    }
124
125
    public function snakeCaseData()
126
    {
127
        return [
128
            ['onlyCamelCaseStringMakesSense', 'only_camel_case_string_makes_sense'],
129
            ['This is NOT Converted as youThink', 'this_is_n_o_t_converted_as_you_think'],
130
            ['All123Numbers456Are789There', 'all123_numbers456_are789_there'],
131
            ['Non?AlphaNumeric4*XCharacters#1q"Are"mess', 'non_alpha_numeric4_x_characters1q_are_mess'],
132
            ['th*1s-is%-ridic&&&&ULous', 'th1s_is_ridic_u_lous'],
133
        ];
134
    }
135
136
    public function isAssociativeArrayData()
137
    {
138
        return [
139
            // All of these are considered associative
140
141
            [['key' => 'val'], true],
142
            [[1 => 'val'], true],
143
            [[2 => 0, 0 => 1, 1 => 2], true],
144
            [['2' => 0, '0' => 1, '1' => 2], true],
145
            [[0 => 1, 3 => 3, 4 => 4], true],
146
147
            // These are sequential
148
149
            [[], false],
150
            [[''], false],
151
            [[0], false],
152
            [[1], false],
153
            [[0 => 0], false],
154
            [[0 => 1], false],
155
            [['0', '1', '2'], false],
156
            [['0'], false],
157
            [[1.0], false],
158
            [[0, 3], false],
159
            [['val0'], false],
160
            [['val0', 'val1', 'val2'], false],
161
            [[0 => 0, 1 => 1, 2 => 2], false],
162
163
            // The unfortunate string-to-integer internal convert
164
165
            [['0' => false], false],
166
            [['0' => 0, '1' => 1, '2' => 2], false],
167
            [[0 => 1, '1' => 1, 2 => 1], false],
168
169
            // None of the keys are valid or sane, but it "works" because PHP
170
171
            [[null => 1], true], // NULL is converted to ''
172
            [[false => 1], false], // FALSE is converted to 0
173
            [[true => 1], true], // TRUE is converted to 1
174
175
            [[2.7 => 'yes'], true], // FLOAT is a different level of weird (float-to-string)
176
        ];
177
    }
178
179
    public function forDelimiterTest()
180
    {
181
        return [
182
            ["Hello fri3nd, you're
183
       looking          good today!", '.', "Hello.fri3nd.you're.looking.good.today."],
184
            ["Hello fri3nd, you're
185
       looking          good today!", '_', "Hello_fri3nd_you're_looking_good_today_"],
186
187
        ];
188
    }
189
190
    public function forKebabTest()
191
    {
192
        return [
193
            ['this_is_converted to kebab_Case', 'this-is-converted-to-kebab-case']
194
        ];
195
    }
196
197
}
198