|
1
|
|
|
<?php |
|
2
|
|
|
class HelperTest extends \PHPUnit\Framework\TestCase |
|
3
|
|
|
{ |
|
4
|
|
|
public function testDataGetKeyIsNull() |
|
5
|
|
|
{ |
|
6
|
|
|
$dataArr = array('test' => 42); |
|
7
|
|
|
|
|
8
|
|
|
$this->assertEquals($dataArr, dataGet($dataArr, null)); |
|
9
|
|
|
$this->assertEquals(42, dataGet($dataArr, 'test')); |
|
10
|
|
|
$this->assertEquals(43, dataGet($dataArr, 'test-unknown', 43)); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function testP() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->expectOutputString("<pre>\n1\n</pre><pre>\nArray\n(\n [a] => 1\n)\n\n</pre>"); |
|
16
|
|
|
_p(1); |
|
17
|
|
|
|
|
18
|
|
|
_p(['a' => 1]); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testHead() |
|
22
|
|
|
{ |
|
23
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
24
|
|
|
$this->assertSame(1, head($arr)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testLast() |
|
28
|
|
|
{ |
|
29
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
30
|
|
|
$this->assertSame(3, last($arr)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testFlatten() |
|
34
|
|
|
{ |
|
35
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => ['s1' => 3, 's2' => 4]]; |
|
36
|
|
|
$this->assertSame([1, 2, 3, 4], flatten($arr)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testValue() |
|
40
|
|
|
{ |
|
41
|
|
|
$value = 1; |
|
42
|
|
|
$this->assertSame(1, value($value)); |
|
43
|
|
|
|
|
44
|
|
|
$value = function() { |
|
45
|
|
|
return 2; |
|
46
|
|
|
}; |
|
47
|
|
|
$this->assertSame(2, value($value)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testCamelCase() |
|
51
|
|
|
{ |
|
52
|
|
|
$str = 'this_is_a_test'; |
|
53
|
|
|
$this->assertSame('ThisIsATest', camelCase($str)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testContains() |
|
57
|
|
|
{ |
|
58
|
|
|
$haystack = 'this is not a long sentence'; |
|
59
|
|
|
$this->assertTrue(contains($haystack, 'not')); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertTrue(contains($haystack, ['these', 'short', 'sentence'])); |
|
62
|
|
|
$this->assertFalse(contains($haystack, ['these', 'short', 'wordlist'])); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testStartsWith() |
|
66
|
|
|
{ |
|
67
|
|
|
$haystack = 'this is not a long sentence'; |
|
68
|
|
|
$this->assertTrue(startsWith($haystack, 'this')); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue(startsWith($haystack, ['these', 'short', 'sentence', 'this'])); |
|
71
|
|
|
$this->assertFalse(startsWith($haystack, ['these', 'short', 'wordlist'])); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testEndsWith() |
|
75
|
|
|
{ |
|
76
|
|
|
$haystack = 'this is not a long sentence'; |
|
77
|
|
|
$this->assertTrue(endsWith($haystack, 'sentence')); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertTrue(endsWith($haystack, ['these', 'short', 'sentence', 'this'])); |
|
80
|
|
|
$this->assertFalse(endsWith($haystack, ['these', 'short', 'wordlist'])); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testWordLimit() |
|
84
|
|
|
{ |
|
85
|
|
|
$haystack = 'this is not a long sentence'; |
|
86
|
|
|
$this->assertEquals('this...', wordLimit($haystack, 4)); |
|
87
|
|
|
$this->assertEquals('this...', wordLimit($haystack, 5)); |
|
88
|
|
|
$this->assertEquals('this is...', wordLimit($haystack, 8)); |
|
89
|
|
|
$this->assertEquals('this??', wordLimit($haystack, 4, '??')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testApp() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertInstanceOf(\Suricate\App::class, app()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testSlug() |
|
98
|
|
|
{ |
|
99
|
|
|
$str = 'This is a long sentence in an URL'; |
|
100
|
|
|
$this->assertEquals(slug($str), 'this-is-a-long-sentence-in-an-url'); |
|
101
|
|
|
$str = 'Une chaîne accentuée_et_des underscores'; |
|
102
|
|
|
$this->assertEquals(slug($str), 'une-chaine-accentueeetdes-underscores'); |
|
103
|
|
|
} |
|
104
|
|
|
public function testNiceTime() |
|
105
|
|
|
{ |
|
106
|
|
|
$time = time() - 45; |
|
107
|
|
|
$this->assertEquals(niceTime($time), 'il y a moins d\'une minute.'); |
|
108
|
|
|
$time = time() - 115; |
|
109
|
|
|
$this->assertEquals(niceTime($time), 'il y a environ une minute.'); |
|
110
|
|
|
$time = time() - 60*40; |
|
111
|
|
|
$this->assertEquals(niceTime($time), 'il y a 40 minutes.'); |
|
112
|
|
|
$time = time() - 60*65; |
|
113
|
|
|
$this->assertEquals(niceTime($time), 'il y a environ une heure.'); |
|
114
|
|
|
$time = time() - 60*60*14; |
|
115
|
|
|
$this->assertEquals(niceTime($time), 'il y a environ 14 heures.'); |
|
116
|
|
|
$time = time() - 60*64*30; |
|
117
|
|
|
$this->assertEquals(niceTime($time), 'hier.'); |
|
118
|
|
|
$time = time() - 60*60*24*17; |
|
119
|
|
|
$this->assertEquals(niceTime($time), 'il y a 17 jours.'); |
|
120
|
|
|
$time = time() - 60*60*24*65; |
|
121
|
|
|
$this->assertEquals(niceTime($time), 'il y a 2 mois.'); |
|
122
|
|
|
$time = time() - 60*60*24*375; |
|
123
|
|
|
$this->assertEquals(niceTime($time), 'il y a plus d\'un an.'); |
|
124
|
|
|
$time = time() - 60*60*24*800; |
|
125
|
|
|
$this->assertEquals(niceTime($time), 'il y a plus de 2 ans.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|