Completed
Push — master ( c694e4...5a55d9 )
by Mauro
12:38 queued 05:22
created

FindDirectoryServiceTest::setPatternAndDirectory()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 11
nc 2
nop 3
1
<?php
2
/**
3
 * Class FindServiceTest
4
 *
5
 * @author Mauro Moreno <[email protected]>
6
 */
7
namespace MauroMoreno\FindBundle\Tests\FindServices;
8
9
use MauroMoreno\FindBundle\Tests\Fixtures\app\AppKernel;
10
use MauroMoreno\FindBundle\Service\FindDirectoryService;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Class FindServiceTest
15
 * @package MauroMoreno\FindBundle\Tests\FindService
16
 */
17
class FindDirectoryServiceTest extends \PHPUnit_Framework_TestCase
18
{
19
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function setUp()
29
    {
30
        $kernel = new AppKernel(getenv('ENV'), getenv('DEBUG') === 'true');
31
        $kernel->boot();
32
33
        $this->container = $kernel->getContainer();
34
        mkdir(__DIR__ . '/empty_directory');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function tearDown()
41
    {
42
        rmdir(__DIR__ . '/empty_directory');
43
        parent::tearDown();
44
    }
45
46
    /**
47
     * Test if service is defined in container.
48
     */
49
    public function testServiceIsDefinedInContainer()
50
    {
51
        $service = $this->container->get(
52
            'mauro_moreno_find.find_directory_service'
53
        );
54
55
        $this->assertInstanceOf(FindDirectoryService::class, $service);
56
    }
57
58
    /**
59
     * Test find empty pattern
60
     */
61
    public function testFindEmptyPatternArgument()
62
    {
63
        $this->setExpectedException(
64
            'InvalidArgumentException',
65
            'Pattern cannot be empty.'
66
        );
67
68
        $this->setPatternAndDirectory("")->find();
69
    }
70
71
    /**
72
     * Test find empty directory
73
     */
74
    public function testFindEmptyDirectoryArgument()
75
    {
76
        $this->setExpectedException(
77
            'InvalidArgumentException',
78
            'The target directory cannot be empty.'
79
        );
80
81
        $this->setPatternAndDirectory('pattern', '')->find();
82
    }
83
84
    /**
85
     * Test find wrong directory
86
     */
87
    public function testFindWrongDirectory()
88
    {
89
        $this->setExpectedException(
90
            'InvalidArgumentException',
91
            'The target directory "' . __DIR__ . '/not_directory" does not exist.'
92
        );
93
        $this->setPatternAndDirectory('pattern', '/not_directory')->find();
94
    }
95
96
    /**
97
     * Test find empty directory
98
     */
99
    public function testFindEmptyDirectory()
100
    {
101
        $find_directory_service = $this->setPatternAndDirectory('pattern', '/empty_directory');
102
        $found = $find_directory_service->find();
103
104
        $this->assertEquals('/pattern/', $find_directory_service->getPattern());
105
        $this->assertEquals(
106
            __DIR__ . '/empty_directory',
107
            $find_directory_service->getDirectory()
108
        );
109
110
        $file_iterator = $find_directory_service->getFileIterator();
111
        $this->assertInstanceOf(
112
            \GlobIterator::class,
113
            $file_iterator
114
        );
115
116
        // TODO: Fix when bug is fixed https://bugs.php.net/bug.php?id=55701
117
        // $this->assertEquals(0, $file_iterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
        $this->assertEquals(0, count(iterator_to_array($file_iterator)));
119
120
        $this->assertFalse($found);
121
    }
122
123
    /**
124
     * Test find empty result
125
     */
126 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...
127
    {
128
        $find_directory_service = $this->setPatternAndDirectory('bad');
129
        $found = $find_directory_service->find();
130
131
        $file_iterator = $find_directory_service->getFileIterator();
132
        $this->assertInstanceOf(
133
            \GlobIterator::class,
134
            $file_iterator
135
        );
136
137
        // TODO: Fix when bug is fixed https://bugs.php.net/bug.php?id=55701
138
        // $this->assertEquals(0, $file_iterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
        $this->assertEquals(4, count(iterator_to_array($file_iterator)));
140
        $this->assertEquals(0, count($found));
141
    }
142
143
    /**
144
     * Test find result
145
     */
146 View Code Duplication
    public function testFindResult()
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...
147
    {
148
        $find_directory_service = $this->setPatternAndDirectory();
149
        $found = $find_directory_service->find();
150
151
        $file_iterator = $find_directory_service->getFileIterator();
152
        $this->assertInstanceOf(
153
            \GlobIterator::class,
154
            $file_iterator
155
        );
156
157
        // TODO: Fix when bug is fixed https://bugs.php.net/bug.php?id=55701
158
        // $this->assertEquals(0, $file_iterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
        $this->assertEquals(4, count(iterator_to_array($file_iterator)));
160
        $this->assertEquals(2, count($found));
161
        $this->assertEquals('file_1', $found[0]['filename']);
162
        $this->assertEquals(
163
            __DIR__ . '/../Fixtures/directory/file_1',
164
            $found[0]['pathname']
165
        );
166
    }
167
168
    /**
169
     * Test find empty result, extension set
170
     */
171 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...
172
    {
173
        $find_directory_service = $this->setPatternAndDirectory(
174
            'bad',
175
            '/../Fixtures/directory',
176
            'txt'
177
        );
178
        $found = $find_directory_service->find();
179
180
        $file_iterator = $find_directory_service->getFileIterator();
181
        $this->assertInstanceOf(
182
            \GlobIterator::class,
183
            $file_iterator
184
        );
185
186
        // TODO: Fix when bug is fixed https://bugs.php.net/bug.php?id=55701
187
        // $this->assertEquals(0, $file_iterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
        $this->assertEquals(2, count(iterator_to_array($file_iterator)));
189
        $this->assertEquals(0, count($found));
190
    }
191
192
    /**
193
     * Test find result, extension set
194
     */
195 View Code Duplication
    public function testFindResultExtensionSet()
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...
196
    {
197
        $find_directory_service = $this->setPatternAndDirectory(
198
            'pattern',
199
            '/../Fixtures/directory',
200
            'txt'
201
        );
202
        $found = $find_directory_service->find();
203
204
        $file_iterator = $find_directory_service->getFileIterator();
205
        $this->assertInstanceOf(
206
            \GlobIterator::class,
207
            $file_iterator
208
        );
209
210
        // TODO: Fix when bug is fixed https://bugs.php.net/bug.php?id=55701
211
        // $this->assertEquals(0, $file_iterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
212
        $this->assertEquals(2, count(iterator_to_array($file_iterator)));
213
        $this->assertEquals(1, count($found));
214
        $this->assertEquals('file_3.txt', $found[0]['filename']);
215
        $this->assertEquals(
216
            __DIR__ . '/../Fixtures/directory/file_3.txt',
217
            $found[0]['pathname']
218
        );
219
    }
220
221
    /**
222
     * Set Pattern and Directory to FindDirectoryService
223
     * @param string $pattern
224
     * @param string $directory
225
     * @param null $extension
226
     * @return FindDirectoryService
227
     */
228
    private function setPatternAndDirectory(
229
        $pattern = 'pattern',
230
        $directory = '/../Fixtures/directory',
231
        $extension = null
232
    ) {
233
        $find_directory_service = $this->getFindDirectoryService()
234
            ->setPattern($pattern)
235
            ->setDirectory(
236
                (!empty($directory) ? __DIR__ . $directory : $directory)
237
            );
238
        if (!empty($extension)) {
239
            $find_directory_service->setExtension($extension);
240
        }
241
        return $find_directory_service;
242
    }
243
244
    /**
245
     * Get FindDirectoryService instance
246
     * @return FindDirectoryService
247
     */
248
    private function getFindDirectoryService()
249
    {
250
        return new FindDirectoryService();
251
    }
252
253
}
254