StringFunctionsTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testStrBefore() 0 3 1
A testStrContains() 0 9 1
A testStrStartsWith() 0 9 1
A strAfterProvider() 0 9 1
A testStrSlug() 0 3 1
A strBeforeProvider() 0 8 1
A testStrRemoveAccents() 0 3 1
A testStrEndsWith() 0 9 1
A testStrAfter() 0 3 1
1
<?php
2
3
namespace Jasny\Tests;
4
5
use PHPStan\Testing\TestCase;
6
7
use function Jasny\str_starts_with;
8
use function Jasny\str_ends_with;
9
use function Jasny\str_contains;
10
use function Jasny\str_before;
11
use function Jasny\str_after;
12
use function Jasny\str_remove_accents;
13
use function Jasny\str_slug;
14
15
/**
16
 * Test string functions
17
 * @coversNothing
18
 */
19
class StringFunctionsTest extends TestCase
20
{
21
    /**
22
     * @covers Jasny\str_starts_with
23
     */
24
    public function testStrStartsWith()
25
    {
26
        $this->assertTrue(str_starts_with('foobar', 'foo'));
27
        $this->assertTrue(str_starts_with('foobar', 'foobar'));
28
        
29
        $this->assertFalse(str_starts_with('foobar', 'qux'));
30
        $this->assertFalse(str_starts_with('foobar', 'bar'));
31
        $this->assertFalse(str_starts_with('foobar', 'oba'));        
32
        $this->assertFalse(str_starts_with('foobar', 'foobarqux'));
33
    }
34
    
35
    /**
36
     * @covers Jasny\str_ends_with
37
     */
38
    public function testStrEndsWith()
39
    {
40
        $this->assertTrue(str_ends_with('foobar', 'bar'));
41
        $this->assertTrue(str_ends_with('foobar', 'foobar'));
42
        
43
        $this->assertFalse(str_ends_with('foobar', 'qux'));
44
        $this->assertFalse(str_ends_with('foobar', 'foo'));
45
        $this->assertFalse(str_ends_with('foobar', 'oba'));        
46
        $this->assertFalse(str_ends_with('foobar', 'quxfoobar'));
47
    }
48
    
49
    /**
50
     * @covers Jasny\str_contains
51
     */
52
    public function testStrContains()
53
    {
54
        $this->assertTrue(str_contains('foobar', 'oba'));
55
        $this->assertTrue(str_contains('foobar', 'foo'));
56
        $this->assertTrue(str_contains('foobar', 'bar'));
57
        $this->assertTrue(str_contains('foobar', 'foobar'));
58
                
59
        $this->assertFalse(str_contains('foobar', 'qux'));
60
        $this->assertFalse(str_contains('foobar', 'quxfoobar'));
61
    }
62
    
63
64
    public function strBeforeProvider()
65
    {
66
        return [
67
            ['foo', ';', 'foo'],
68
            ['foo;bar', ';', 'foo'],
69
            ['foo;bar;zoo', ';', 'foo'],
70
            [';bar;zoo', ';', ''],
71
            ['fooababar', 'aba', 'foo']
72
        ];
73
    }
74
75
    /**
76
     * @covers Jasny\str_before
77
     * @dataProvider strBeforeProvider
78
     *
79
     * @param string $string
80
     * @param string $expect
81
     */
82
    public function testStrBefore($string, $substr, $expect)
83
    {
84
        $this->assertSame($expect, str_before($string, $substr));
85
    }
86
    
87
88
    public function strAfterProvider()
89
    {
90
        return [
91
            ['foo', ';', ''],
92
            ['bar;foo', ';', 'foo'],
93
            ['bar;foo;zoo', ';', 'foo;zoo'],
94
            [';foo;zoo', ';', 'foo;zoo'],
95
            ['barabafoo', 'aba', 'foo'],
96
            ['abababababa', 'aba', 'babababa']
97
        ];
98
    }
99
100
    /**
101
     * @covers Jasny\str_after
102
     * @dataProvider strAfterProvider
103
     *
104
     * @param string $string
105
     * @param string $expect
106
     */
107
    public function testStrAfter($string, $substr, $expect)
108
    {
109
        $this->assertSame($expect, str_after($string, $substr));
110
    }
111
112
113
    /**
114
     * @covers Jasny\str_remove_accents
115
     */
116
    public function testStrRemoveAccents()
117
    {
118
        $this->assertSame('abcdehij', str_remove_accents('ábcdëhij'));
119
    }
120
    
121
    /**
122
     * @covers Jasny\str_slug
123
     */
124
    public function testStrSlug()
125
    {
126
        $this->assertSame('john-doe-master-ruler', str_slug('John Doé - master & ruler'));
127
    }
128
}
129
130