Completed
Push — master ( 817b84...0e5c1d )
by Jaap
13s queued 12s
created

InPathTest::testNoFalsePositiveWithLongerDirName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace Flyfinder\Specification;
15
16
use Flyfinder\Path;
17
use Mockery as m;
18
use PHPUnit\Framework\TestCase;
19
use function dirname;
20
21
/**
22
 * Test case for InPath
23
 *
24
 * @coversDefaultClass \Flyfinder\Specification\InPath
25
 * @covers ::<private>
26
 */
27
class InPathTest extends TestCase
28
{
29
    /** @var InPath */
30
    private $fixture;
31
32
    public function tearDown() : void
33
    {
34
        m::close();
35
    }
36
37
    /**
38
     * @uses \Flyfinder\Path
39
     *
40
     * @covers ::__construct
41
     * @covers ::isSatisfiedBy
42
     * @dataProvider validDirnames
43
     */
44
    public function testExactMatch() : void
45
    {
46
        $absolutePath = 'absolute/path/to/file.txt';
47
        $spec         = new InPath(new Path($absolutePath));
48
        $this->assertTrue($spec->isSatisfiedBy([
49
            'type' => 'file',
50
            'path' => $absolutePath,
51
            'dirname' => $absolutePath,
52
            'filename' => 'file',
53
            'extension' => 'txt',
54
            'basename' => 'file.txt',
55
        ]));
56
    }
57
58
    private function useWildcardPath() : void
59
    {
60
        $this->fixture = new InPath(new Path('*dden?ir/n'));
61
    }
62
63
    /**
64
     * @uses \Flyfinder\Path
65
     *
66
     * @covers ::__construct
67
     * @covers ::isSatisfiedBy
68
     * @dataProvider validDirnames
69
     */
70
    public function testIfSpecificationIsSatisfied(string $dirname) : void
71
    {
72
        $this->useWildcardPath();
73
        $this->assertTrue($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
74
    }
75
76
    /**
77
     * @uses \Flyfinder\Path
78
     *
79
     * @covers ::__construct
80
     * @covers ::isSatisfiedBy
81
     * @dataProvider validDirnames
82
     */
83
    public function testWithSingleDotSpec(string $dirname) : void
84
    {
85
        $spec = new InPath(new Path('.'));
86
        $this->assertTrue($spec->isSatisfiedBy(['dirname' => $dirname]));
87
    }
88
89
    /**
90
     * @uses \Flyfinder\Path
91
     *
92
     * @covers ::__construct
93
     * @covers ::isSatisfiedBy
94
     * @dataProvider validDirnames
95
     */
96
    public function testWithCurrentDirSpec(string $dirname) : void
97
    {
98
        $spec = new InPath(new Path('./'));
99
        $this->assertTrue($spec->isSatisfiedBy(['dirname' => $dirname]));
100
    }
101
102
    /**
103
     * Data provider for testIfSpecificationIsSatisfied. Contains a few valid directory names
104
     *
105
     * @return string[][]
106
     */
107
    public function validDirnames() : array
108
    {
109
        return [
110
            ['.hiddendir/n'],
111
            ['.hiddendir/n/'],
112
            ['.hiddendir/n/somedir'],
113
            ['.hiddendir/n/somedir.txt'],
114
            ['ddenxir/n'],
115
        ];
116
    }
117
118
    /**
119
     * @uses \Flyfinder\Path
120
     *
121
     * @covers ::__construct
122
     * @covers ::isSatisfiedBy
123
     * @dataProvider invalidDirnames
124
     */
125
    public function testIfSpecificationIsNotSatisfied(string $dirname) : void
126
    {
127
        $this->useWildcardPath();
128
        $this->assertFalse($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
129
    }
130
131
    /**
132
     * Data provider for testIfSpecificationIsNotSatisfied. Contains a few invalid directory names
133
     *
134
     * @return string[][]
135
     */
136
    public function invalidDirnames() : array
137
    {
138
        return [
139
            ['/hiddendir/n'],
140
            ['.hiddendir/normaldir'],
141
            ['.hiddendir.ext/n'],
142
            ['.hiddenxxir/n'],
143
            ['.hiddenir/n'],
144
        ];
145
    }
146
147
    /**
148
     * @uses \Flyfinder\Path
149
     *
150
     * @covers ::__construct
151
     * @covers ::isSatisfiedBy
152
     */
153
    public function testNoFalsePositiveWithLongerDirName() : void
154
    {
155
        $prefixDir    = 'absolute/path';
156
        $absolutePath = 'absolute/pathMOAR/to/file.txt';
157
        $spec         = new InPath(new Path($prefixDir));
158
        $this->assertFalse($spec->isSatisfiedBy([
159
            'type' => 'file',
160
            'path' => $absolutePath,
161
            'dirname' => dirname($absolutePath),
162
            'filename' => 'file',
163
            'extension' => 'txt',
164
            'basename' => 'file.txt',
165
        ]));
166
    }
167
}
168