Completed
Push — 1.0-phpstan ( ddfe6c )
by Kamil
23:36
created

RecursiveFileLocatorSpec   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 159
c 0
b 0
f 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_implements_sylius_file_locator_interface() 0 4 1
A it_searches_for_file() 0 17 1
B it_searches_for_files() 0 26 1
A it_throws_an_exception_if_searching_for_file_with_empty_name() 0 4 1
A it_throws_an_exception_if_searching_for_files_with_empty_name() 0 4 1
A it_throws_an_exception_if_there_is_no_file_that_matches_the_given_name() 0 15 1
A it_throws_an_exception_if_there_is_there_are_not_any_files_that_matches_the_given_name() 0 15 1
B it_isolates_finding_paths_from_multiple_sources() 0 29 1
B it_silences_finder_exceptions_even_if_searching_in_multiple_sources() 0 29 1
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
final class RecursiveFileLocatorSpec extends ObjectBehavior
23
{
24
    function let(FinderFactoryInterface $finderFactory): void
25
    {
26
        $this->beConstructedWith($finderFactory, ['/search/path/']);
27
    }
28
29
    function it_implements_sylius_file_locator_interface(): void
30
    {
31
        $this->shouldImplement(FileLocatorInterface::class);
32
    }
33
34
    function it_searches_for_file(FinderFactoryInterface $finderFactory, Finder $finder, SplFileInfo $splFileInfo): void
35
    {
36
        $finderFactory->create()->willReturn($finder);
37
38
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
39
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
40
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
41
        $finder->files()->shouldBeCalled()->willReturn($finder);
42
43
        $finder->getIterator()->willReturn(new \ArrayIterator([
44
            $splFileInfo->getWrappedObject(),
45
        ]));
46
47
        $splFileInfo->getPathname()->willReturn('/search/path/nested/readme.md');
48
49
        $this->locateFileNamed('readme.md')->shouldReturn('/search/path/nested/readme.md');
50
    }
51
52
    function it_searches_for_files(
53
        FinderFactoryInterface $finderFactory,
54
        Finder $finder,
55
        SplFileInfo $firstSplFileInfo,
56
        SplFileInfo $secondSplFileInfo
57
    ): void {
58
        $finderFactory->create()->willReturn($finder);
59
60
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
61
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
62
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
63
        $finder->files()->shouldBeCalled()->willReturn($finder);
64
65
        $finder->getIterator()->willReturn(new \ArrayIterator([
66
            $firstSplFileInfo->getWrappedObject(),
67
            $secondSplFileInfo->getWrappedObject(),
68
        ]));
69
70
        $firstSplFileInfo->getPathname()->willReturn('/search/path/nested1/readme.md');
71
        $secondSplFileInfo->getPathname()->willReturn('/search/path/nested2/readme.md');
72
73
        $this->locateFilesNamed('readme.md')->shouldReturn([
74
            '/search/path/nested1/readme.md',
75
            '/search/path/nested2/readme.md',
76
        ]);
77
    }
78
79
    function it_throws_an_exception_if_searching_for_file_with_empty_name(): void
80
    {
81
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['']);
82
    }
83
84
    function it_throws_an_exception_if_searching_for_files_with_empty_name(): void
85
    {
86
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['']);
87
    }
88
89
    function it_throws_an_exception_if_there_is_no_file_that_matches_the_given_name(
90
        FinderFactoryInterface $finderFactory,
91
        Finder $finder
92
    ): void {
93
        $finderFactory->create()->willReturn($finder);
94
95
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
96
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
97
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
98
        $finder->files()->shouldBeCalled()->willReturn($finder);
99
100
        $finder->getIterator()->willReturn(new \ArrayIterator());
101
102
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['readme.md']);
103
    }
104
105
    function it_throws_an_exception_if_there_is_there_are_not_any_files_that_matches_the_given_name(
106
        FinderFactoryInterface $finderFactory,
107
        Finder $finder
108
    ): void {
109
        $finderFactory->create()->willReturn($finder);
110
111
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
112
        $finder->in('/search/path/')->shouldBeCalled()->willReturn($finder);
113
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
114
        $finder->files()->shouldBeCalled()->willReturn($finder);
115
116
        $finder->getIterator()->willReturn(new \ArrayIterator());
117
118
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['readme.md']);
119
    }
120
121
    function it_isolates_finding_paths_from_multiple_sources(
122
        FinderFactoryInterface $finderFactory,
123
        Finder $firstFinder,
124
        Finder $secondFinder,
125
        SplFileInfo $splFileInfo
126
    ): void {
127
        $this->beConstructedWith($finderFactory, ['/search/path/first/', '/search/path/second/']);
128
129
        $finderFactory->create()->willReturn($firstFinder, $secondFinder);
130
131
        $firstFinder->name('readme.md')->shouldBeCalled()->willReturn($firstFinder);
132
        $firstFinder->in('/search/path/first/')->shouldBeCalled()->willReturn($firstFinder);
133
        $firstFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($firstFinder);
134
        $firstFinder->files()->shouldBeCalled()->willReturn($firstFinder);
135
136
        $secondFinder->name('readme.md')->shouldBeCalled()->willReturn($secondFinder);
137
        $secondFinder->in('/search/path/second/')->shouldBeCalled()->willReturn($secondFinder);
138
        $secondFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($secondFinder);
139
        $secondFinder->files()->shouldBeCalled()->willReturn($secondFinder);
140
141
        $firstFinder->getIterator()->willReturn(new \ArrayIterator([$splFileInfo->getWrappedObject()]));
142
        $secondFinder->getIterator()->willReturn(new \ArrayIterator());
143
144
        $splFileInfo->getPathname()->willReturn('/search/path/first/nested/readme.md');
145
146
        $this->locateFilesNamed('readme.md')->shouldReturn([
147
            '/search/path/first/nested/readme.md',
148
        ]);
149
    }
150
151
    function it_silences_finder_exceptions_even_if_searching_in_multiple_sources(
152
        FinderFactoryInterface $finderFactory,
153
        Finder $firstFinder,
154
        Finder $secondFinder,
155
        SplFileInfo $splFileInfo
156
    ): void {
157
        $this->beConstructedWith($finderFactory, ['/search/path/first/', '/search/path/second/']);
158
159
        $finderFactory->create()->willReturn($firstFinder, $secondFinder);
160
161
        $firstFinder->name('readme.md')->shouldBeCalled()->willReturn($firstFinder);
162
        $firstFinder->in('/search/path/first/')->shouldBeCalled()->willReturn($firstFinder);
163
        $firstFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($firstFinder);
164
        $firstFinder->files()->shouldBeCalled()->willReturn($firstFinder);
165
166
        $secondFinder->name('readme.md')->shouldBeCalled()->willReturn($secondFinder);
167
        $secondFinder->in('/search/path/second/')->shouldBeCalled()->willReturn($secondFinder);
168
        $secondFinder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($secondFinder);
169
        $secondFinder->files()->shouldBeCalled()->willReturn($secondFinder);
170
171
        $firstFinder->getIterator()->willReturn(new \ArrayIterator([$splFileInfo->getWrappedObject()]));
172
        $secondFinder->getIterator()->willThrow(\InvalidArgumentException::class);
173
174
        $splFileInfo->getPathname()->willReturn('/search/path/first/nested/readme.md');
175
176
        $this->locateFilesNamed('readme.md')->shouldReturn([
177
            '/search/path/first/nested/readme.md',
178
        ]);
179
    }
180
}
181