FileFunctionsTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testFileContains() 0 16 1
A testFnmatchExtended() 0 3 1
A fnmatchExtendedProvider() 0 38 1
A testFileContainsWithNonExistingFile() 0 7 1
A setUp() 0 3 1
1
<?php
2
3
namespace Jasny\Tests;
4
5
use org\bovigo\vfs\vfsStream;
6
use Jasny\TestHelper;
7
use PHPStan\Testing\TestCase;
8
9
use function Jasny\file_contains;
10
use function Jasny\fnmatch_extended;
11
12
/**
13
 * Test server functions
14
 * @coversNothing
15
 */
16
class FileFunctionsTest extends TestCase
17
{
18
    use TestHelper;
19
    
20
    /**
21
     * @var \org\bovigo\vfs\vfsStreamDirectory
22
     */
23
    protected $root;
24
    
25
    public function setUp()
26
    {
27
        $this->root = vfsStream::setup();
28
    }
29
    
30
    /**
31
     * @covers Jasny\file_contains
32
     */
33
    public function testFileContains()
34
    {
35
        $text = wordwrap(str_repeat("abcdefghijklmopqrstuvwxyz", 100), 80, "\n", true);
36
        
37
        vfsStream::create([
38
            'abc.txt' => $text
39
        ]);
40
        
41
        $file = vfsStream::url('root/abc.txt');
42
        
43
        $this->assertTrue(file_contains($file, "klm"), "klm");
44
        $this->assertTrue(file_contains($file, "abcde\nfgh"), 'abcd\nefgh');
45
        
46
        $this->assertFalse(file_contains($file, "foobar"), "foobar");
47
        
48
        $this->assertTrue(file_contains($file, substr($text, 140, 400)), 'long sub string');
49
    }
50
    
51
    /**
52
     * @covers Jasny\file_contains
53
     */
54
    public function testFileContainsWithNonExistingFile()
55
    {
56
        $file = vfsStream::url('root/non-existing.txt');
57
58
        $this->assertFalse(@file_contains($file, "foo"));
59
        
60
        $this->assertLastError(E_WARNING);
61
    }
62
    
63
    
64
    public function fnmatchExtendedProvider()
65
    {
66
        return  [
67
            // Valid
68
            ['/foo/bar/zoo', '/foo/bar/zoo', true],
69
70
            ['/foo/?ar/zoo', '/foo/bar/zoo', true],
71
            ['/foo/*/zoo', '/foo/bar/zoo', true],
72
            ['/foo/b*/zoo', '/foo/bar/zoo', true],
73
            ['/foo/bar*/zoo', '/foo/bar/zoo', true],
74
75
            ['/foo/bar/#', '/foo/bar/22', true],
76
            ['/foo/bar/?#', '/foo/bar/a22', true],
77
78
            ['/foo/**', '/foo/bar/zoo', true],
79
            ['/foo/**', '/foo/', true],
80
            ['/foo/**', '/foo', true],
81
82
            ['/foo/{bar,baz}/zoo', '/foo/bar/zoo', true],
83
            ['/foo/{12,89}/zoo', '/foo/12/zoo', true],
84
            ['/foo/[bc]ar/zoo', '/foo/bar/zoo', true],
85
            ['/foo/[a-c]ar/zoo', '/foo/bar/zoo', true],
86
87
            // Invalid
88
            ['/foo/qux/zoo', '/foo/bar/zoo', false],
89
90
            ['/foo/?a/zoo', '/foo/bar/zoo', false],
91
            ['/foo/*/zoo', '/foo/zoo', false],
92
93
            ['/foo/bar/#', '/foo/bar/n00', false],
94
            ['/foo/bar/?#', '/foo/bar/2', false],
95
96
            ['/foo/**', '/foobar/zoo', false],
97
98
            ['/foo/{bar,baz}/zoo', '/foo/{bar,baz}/zoo', false],
99
            ['/foo/{12,89}/zoo', '/foo/1289/zoo', false],
100
            ['/foo/[bc]ar/zoo', '/foo/dar/zoo', false],
101
            ['/foo/[d-q]ar/zoo', '/foo/bar/zoo', false]
102
        ];
103
    }
104
    
105
    /**
106
     * @covers Jasny\fnmatch_extended
107
     * @dataProvider fnmatchExtendedProvider
108
     * 
109
     * @param string  $pattern
110
     * @param string  $path
111
     * @param boolean $expect
112
     */
113
    public function testFnmatchExtended($pattern, $path, $expect)
114
    {
115
        $this->assertEquals($expect, fnmatch_extended($pattern, $path));
116
    }
117
}
118
119