Completed
Push — 1.0-coding-standard-2-addition... ( 10d1c2 )
by Kamil
34:11 queued 09:30
created

RecursiveFileLocatorSpec::it_searches_for_files()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 19
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\ThemeBundle\Locator;
15
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface;
18
use Sylius\Bundle\ThemeBundle\Locator\FileLocatorInterface;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\Finder\SplFileInfo;
21
22
/**
23
 * @author Kamil Kokot <[email protected]>
24
 */
25
final class RecursiveFileLocatorSpec extends ObjectBehavior
26
{
27
    function let(FinderFactoryInterface $finderFactory): void
28
    {
29
        $this->beConstructedWith($finderFactory, ['/search/path/']);
30
    }
31
32
    function it_implements_sylius_file_locator_interface(): void
33
    {
34
        $this->shouldImplement(FileLocatorInterface::class);
35
    }
36
37
    function it_searches_for_file(FinderFactoryInterface $finderFactory, Finder $finder, SplFileInfo $splFileInfo): void
38
    {
39
        $finderFactory->create()->willReturn($finder);
40
41
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
42
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
43
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
44
        $finder->files()->shouldBeCalled()->willReturn($finder);
45
46
        $finder->getIterator()->willReturn(new \ArrayIterator([
47
            $splFileInfo->getWrappedObject(),
48
        ]));
49
50
        $splFileInfo->getPathname()->willReturn('/search/path/nested/readme.md');
51
52
        $this->locateFileNamed('readme.md')->shouldReturn('/search/path/nested/readme.md');
53
    }
54
55
    function it_searches_for_files(
56
        FinderFactoryInterface $finderFactory,
57
        Finder $finder,
58
        SplFileInfo $firstSplFileInfo,
59
        SplFileInfo $secondSplFileInfo
60
    ): void {
61
        $finderFactory->create()->willReturn($finder);
62
63
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
64
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
65
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
66
        $finder->files()->shouldBeCalled()->willReturn($finder);
67
68
        $finder->getIterator()->willReturn(new \ArrayIterator([
69
            $firstSplFileInfo->getWrappedObject(),
70
            $secondSplFileInfo->getWrappedObject(),
71
        ]));
72
73
        $firstSplFileInfo->getPathname()->willReturn('/search/path/nested1/readme.md');
74
        $secondSplFileInfo->getPathname()->willReturn('/search/path/nested2/readme.md');
75
76
        $this->locateFilesNamed('readme.md')->shouldReturn([
77
            '/search/path/nested1/readme.md',
78
            '/search/path/nested2/readme.md',
79
        ]);
80
    }
81
82
    function it_throws_an_exception_if_searching_for_file_with_empty_name(): void
83
    {
84
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['']);
85
    }
86
87
    function it_throws_an_exception_if_searching_for_files_with_empty_name(): void
88
    {
89
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['']);
90
    }
91
92
    function it_throws_an_exception_if_there_is_no_file_that_matches_the_given_name(
93
        FinderFactoryInterface $finderFactory,
94
        Finder $finder
95
    ): void {
96
        $finderFactory->create()->willReturn($finder);
97
98
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
99
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
100
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
101
        $finder->files()->shouldBeCalled()->willReturn($finder);
102
103
        $finder->getIterator()->willReturn(new \ArrayIterator());
104
105
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['readme.md']);
106
    }
107
108
    function it_throws_an_exception_if_there_is_there_are_not_any_files_that_matches_the_given_name(
109
        FinderFactoryInterface $finderFactory,
110
        Finder $finder
111
    ): void {
112
        $finderFactory->create()->willReturn($finder);
113
114
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
115
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
116
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
117
        $finder->files()->shouldBeCalled()->willReturn($finder);
118
119
        $finder->getIterator()->willReturn(new \ArrayIterator());
120
121
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['readme.md']);
122
    }
123
124
    function it_isolates_finding_paths_from_multiple_sources(
125
        FinderFactoryInterface $finderFactory,
126
        Finder $firstFinder,
127
        Finder $secondFinder,
128
        SplFileInfo $splFileInfo
129
    ): void {
130
        $this->beConstructedWith($finderFactory, ['/search/path/first/', '/search/path/second/']);
131
132
        $finderFactory->create()->willReturn($firstFinder, $secondFinder);
133
134
        $firstFinder->name('readme.md')->shouldBeCalled()->willReturn($firstFinder);
135
        $firstFinder->in('/search/path/first/')->shouldBeCalled()->willReturn($firstFinder);
136
        $firstFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($firstFinder);
137
        $firstFinder->files()->shouldBeCalled()->willReturn($firstFinder);
138
139
        $secondFinder->name('readme.md')->shouldBeCalled()->willReturn($secondFinder);
140
        $secondFinder->in('/search/path/second/')->shouldBeCalled()->willReturn($secondFinder);
141
        $secondFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($secondFinder);
142
        $secondFinder->files()->shouldBeCalled()->willReturn($secondFinder);
143
144
        $firstFinder->getIterator()->willReturn(new \ArrayIterator([$splFileInfo->getWrappedObject()]));
145
        $secondFinder->getIterator()->willReturn(new \ArrayIterator());
146
147
        $splFileInfo->getPathname()->willReturn('/search/path/first/nested/readme.md');
148
149
        $this->locateFilesNamed('readme.md')->shouldReturn([
150
            '/search/path/first/nested/readme.md',
151
        ]);
152
    }
153
154
    function it_silences_finder_exceptions_even_if_searching_in_multiple_sources(
155
        FinderFactoryInterface $finderFactory,
156
        Finder $firstFinder,
157
        Finder $secondFinder,
158
        SplFileInfo $splFileInfo
159
    ): void {
160
        $this->beConstructedWith($finderFactory, ['/search/path/first/', '/search/path/second/']);
161
162
        $finderFactory->create()->willReturn($firstFinder, $secondFinder);
163
164
        $firstFinder->name('readme.md')->shouldBeCalled()->willReturn($firstFinder);
165
        $firstFinder->in('/search/path/first/')->shouldBeCalled()->willReturn($firstFinder);
166
        $firstFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($firstFinder);
167
        $firstFinder->files()->shouldBeCalled()->willReturn($firstFinder);
168
169
        $secondFinder->name('readme.md')->shouldBeCalled()->willReturn($secondFinder);
170
        $secondFinder->in('/search/path/second/')->shouldBeCalled()->willReturn($secondFinder);
171
        $secondFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($secondFinder);
172
        $secondFinder->files()->shouldBeCalled()->willReturn($secondFinder);
173
174
        $firstFinder->getIterator()->willReturn(new \ArrayIterator([$splFileInfo->getWrappedObject()]));
175
        $secondFinder->getIterator()->willThrow(\InvalidArgumentException::class);
176
177
        $splFileInfo->getPathname()->willReturn('/search/path/first/nested/readme.md');
178
179
        $this->locateFilesNamed('readme.md')->shouldReturn([
180
            '/search/path/first/nested/readme.md',
181
        ]);
182
    }
183
}
184