testFindResultExtensionSet()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
/**
3
 * Class FindServiceTest
4
 *
5
 * @author Mauro Moreno <[email protected]>
6
 */
7
namespace MauroMoreno\FindBundle\Tests\Services;
8
9
use MauroMoreno\FindBundle\Service\FindDirectoryService;
10
use MauroMoreno\FindBundle\Service\FinderInterface;
11
use MauroMoreno\FindBundle\Service\ListerInterface;
12
13
/**
14
 * Class FindServiceTest
15
 * @package MauroMoreno\FindBundle\Tests\FindService
16
 */
17
class FindDirectoryServiceTest extends \PHPUnit_Framework_TestCase
18
{
19
20
    private $finder_mock;
21
22
    private $lister_mock;
23
24
    /**
25
     * @beforeClass
26
     */
27
    public static function setUpBeforeClass()
28
    {
29
        if (!is_dir(__DIR__ . '/empty_directory')) {
30
            mkdir(__DIR__ . '/empty_directory');
31
        }
32
    }
33
34
    /**
35
     * @afterClass
36
     */
37
    public static function tearDownAfterClass()
38
    {
39
        if (is_dir(__DIR__ . '/empty_directory')) {
40
            rmdir(__DIR__ . '/empty_directory');
41
        }
42
    }
43
44
    /**
45
     * Test find empty pattern
46
     */
47
    public function testFindEmptyPatternArgument()
48
    {
49
        $this->setExpectedException(
50
            'InvalidArgumentException',
51
            'Pattern cannot be empty.'
52
        );
53
54
        $this->setPatternAndDirectory("")->find();
55
    }
56
57
    /**
58
     * Test find empty directory
59
     */
60
    public function testFindEmptyDirectoryArgument()
61
    {
62
        $this->setExpectedException(
63
            'InvalidArgumentException',
64
            'The target directory cannot be empty.'
65
        );
66
67
        $this->setPatternAndDirectory('pattern', '')->find();
68
    }
69
70
    /**
71
     * Test find wrong directory
72
     */
73
    public function testFindWrongDirectory()
74
    {
75
        $this->setExpectedException(
76
            'InvalidArgumentException',
77
            'The target directory "' . __DIR__ . '/not_directory" does not exist.'
78
        );
79
        $this->setPatternAndDirectory('pattern', '/not_directory')->find();
80
    }
81
82
    /**
83
     * Test find empty directory
84
     */
85
    public function testFindEmptyDirectory()
86
    {
87
        $find_directory_service = $this->setPatternAndDirectory(
88
            'pattern',
89
            '/empty_directory'
90
        );
91
        $this->lister_mock->expects($this->once())
92
            ->method('ls')
93
            ->will($this->returnValue(new \GlobIterator(
94
                __DIR__ . '/empty_directory/*'
95
            )));
96
97
        $found = $find_directory_service->find();
98
99
        $this->assertEquals('pattern', $find_directory_service->getPattern());
100
        $this->assertEquals(
101
            __DIR__ . '/empty_directory',
102
            $find_directory_service->getDirectory()
103
        );
104
105
        $this->assertFalse($found);
106
    }
107
108
    /**
109
     * Test find empty result
110
     */
111 View Code Duplication
    public function testFindEmptyResult()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $find_directory_service = $this->setPatternAndDirectory('bad');
114
        $this->lister_mock->expects($this->once())
115
            ->method('ls')
116
            ->will($this->returnValue(new \GlobIterator(
117
                __DIR__ . '/../Fixtures/directory/*'
118
            )));
119
        $this->finder_mock->expects($this->exactly(4))
120
            ->method('find')
121
            ->will($this->returnValue(false));
122
123
        $found = $find_directory_service->find();
124
125
        $this->assertEquals(0, count($found));
126
    }
127
128
    /**
129
     * Test find result
130
     */
131
    public function testFindResult()
132
    {
133
        $find_directory_service = $this->setPatternAndDirectory();
134
        $this->lister_mock->expects($this->once())
135
            ->method('ls')
136
            ->will($this->returnValue(new \GlobIterator(
137
                __DIR__ . '/../Fixtures/directory/*'
138
            )));
139
        $this->finder_mock->expects($this->at(0))
140
            ->method('find')
141
            ->will($this->returnValue(new \SplFileInfo(
142
                __DIR__ . '/../Fixtures/directory/file_1'
143
            )));
144
        $this->finder_mock->expects($this->at(1))
145
            ->method('find')
146
            ->will($this->returnValue(false));
147
        $this->finder_mock->expects($this->at(2))
148
            ->method('find')
149
            ->will($this->returnValue(new \SplFileInfo(
150
                __DIR__ . '/../Fixtures/directory/file_1.txt'
151
            )));
152
        $this->finder_mock->expects($this->at(3))
153
            ->method('find')
154
            ->will($this->returnValue(false));
155
156
        $found = $find_directory_service->find();
157
158
        $this->assertEquals(2, count($found));
159
        $this->assertEquals('file_1', $found[0]['filename']);
160
        $this->assertEquals(
161
            __DIR__ . '/../Fixtures/directory/file_1',
162
            $found[0]['pathname']
163
        );
164
    }
165
166
    /**
167
     * Test find empty result, extension set
168
     */
169 View Code Duplication
    public function testFindEmptyResultExtensionSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $find_directory_service = $this->setPatternAndDirectory(
172
            'bad',
173
            '/../Fixtures/directory',
174
            'txt'
175
        );
176
        $this->lister_mock->expects($this->once())
177
            ->method('ls')
178
            ->will($this->returnValue(new \GlobIterator(
179
                __DIR__ . '/../Fixtures/directory/*.txt'
180
            )));
181
        $this->finder_mock->expects($this->exactly(2))
182
            ->method('find')
183
            ->will($this->returnValue(false));
184
185
        $found = $find_directory_service->find();
186
187
        $this->assertEquals(0, count($found));
188
    }
189
190
    /**
191
     * Test find result, extension set
192
     */
193
    public function testFindResultExtensionSet()
194
    {
195
        $find_directory_service = $this->setPatternAndDirectory(
196
            'pattern',
197
            '/../Fixtures/directory',
198
            'txt'
199
        );
200
        $this->lister_mock->expects($this->once())
201
            ->method('ls')
202
            ->will($this->returnValue(new \GlobIterator(
203
                __DIR__ . '/../Fixtures/directory/*.txt'
204
            )));
205
        $this->finder_mock->expects($this->at(0))
206
            ->method('find')
207
            ->will($this->returnValue(new \SplFileInfo(
208
                __DIR__ . '/../Fixtures/directory/file_3.txt'
209
            )));
210
        $this->finder_mock->expects($this->at(1))
211
            ->method('find')
212
            ->will($this->returnValue(false));
213
214
        $found = $find_directory_service->find();
215
216
        $this->assertEquals('file_3.txt', $found[0]['filename']);
217
        $this->assertEquals(
218
            __DIR__ . '/../Fixtures/directory/file_3.txt',
219
            $found[0]['pathname']
220
        );
221
    }
222
223
    /**
224
     * Get FindDirectoryService instance
225
     * @return FindDirectoryService
226
     */
227
    private function getFindDirectoryService()
228
    {
229
        // Mock Finder.
230
        $mock_builder = $this->getMockBuilder(FinderInterface::class);
231
        $mock_builder->setMethods(['find']);
232
        $this->finder_mock = $mock_builder->getMock();
233
234
        // Mock Lister.
235
        $mock_builder = $this->getMockBuilder(ListerInterface::class);
236
        $mock_builder->setMethods(['ls']);
237
        $this->lister_mock = $mock_builder->getMock();
238
239
        // Return service with mocked interfaces.
240
        return new FindDirectoryService(
241
            $this->finder_mock,
242
            $this->lister_mock
243
        );
244
    }
245
246
    /**
247
     * Set Pattern and Directory to FindDirectoryService
248
     * @param string $pattern
249
     * @param string $directory
250
     * @param string $extension
251
     * @return FindDirectoryService
252
     */
253
    private function setPatternAndDirectory(
254
        $pattern = 'pattern',
255
        $directory = '/../Fixtures/directory',
256
        $extension = null
257
    ) {
258
        $find_directory_service = $this->getFindDirectoryService()
259
            ->setPattern($pattern)
260
            ->setDirectory(
261
                (!empty($directory) ? __DIR__ . $directory : $directory)
262
            );
263
        if (!empty($extension)) {
264
            $find_directory_service->setExtension($extension);
265
        }
266
        return $find_directory_service;
267
    }
268
269
}
270