Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

RecursiveFileLocatorSpec::it_searches_for_file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
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
namespace spec\Sylius\Bundle\ThemeBundle\Locator;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface;
17
use Sylius\Bundle\ThemeBundle\Locator\FileLocatorInterface;
18
use Sylius\Bundle\ThemeBundle\Locator\RecursiveFileLocator;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\Finder\SplFileInfo;
21
22
/**
23
 * @mixin RecursiveFileLocator
24
 *
25
 * @author Kamil Kokot <[email protected]>
26
 */
27
class RecursiveFileLocatorSpec extends ObjectBehavior
28
{
29
    function let(FinderFactoryInterface $finderFactory)
30
    {
31
        $this->beConstructedWith($finderFactory, ['/search/path/']);
32
    }
33
34
    function it_is_initializable()
35
    {
36
        $this->shouldHaveType('Sylius\Bundle\ThemeBundle\Locator\RecursiveFileLocator');
37
    }
38
39
    function it_implements_sylius_file_locator_interface()
40
    {
41
        $this->shouldImplement(FileLocatorInterface::class);
42
    }
43
44
    function it_searches_for_file(FinderFactoryInterface $finderFactory, Finder $finder, SplFileInfo $splFileInfo)
45
    {
46
        $finderFactory->create()->willReturn($finder);
47
48
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
49
        $finder->in(['/search/path/'])->shouldBeCalled()->willReturn($finder);
50
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
51
        $finder->files()->shouldBeCalled()->willReturn($finder);
52
53
        $finder->getIterator()->willReturn(new \ArrayIterator([
54
            $splFileInfo->getWrappedObject(),
55
        ]));
56
        $finder->count()->willReturn(1);
57
58
        $splFileInfo->getPathname()->willReturn('/search/path/nested/readme.md');
59
60
        $this->locateFileNamed('readme.md')->shouldReturn('/search/path/nested/readme.md');
61
    }
62
63
    function it_searches_for_files(
64
        FinderFactoryInterface $finderFactory,
65
        Finder $finder,
66
        SplFileInfo $firstSplFileInfo,
67
        SplFileInfo $secondSplFileInfo
68
    ) {
69
        $finderFactory->create()->willReturn($finder);
70
71
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
72
        $finder->in(['/search/path/'])->shouldBeCalled()->willReturn($finder);
73
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
74
        $finder->files()->shouldBeCalled()->willReturn($finder);
75
76
        $finder->getIterator()->willReturn(new \ArrayIterator([
77
            $firstSplFileInfo->getWrappedObject(),
78
            $secondSplFileInfo->getWrappedObject(),
79
        ]));
80
        $finder->count()->willReturn(2);
81
82
        $firstSplFileInfo->getPathname()->willReturn('/search/path/nested1/readme.md');
83
        $secondSplFileInfo->getPathname()->willReturn('/search/path/nested2/readme.md');
84
85
        $this->locateFilesNamed('readme.md')->shouldReturn([
86
            '/search/path/nested1/readme.md',
87
            '/search/path/nested2/readme.md',
88
        ]);
89
    }
90
91
    function it_throws_an_exception_if_searching_for_file_with_empty_name()
92
    {
93
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['']);
94
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', [null]);
95
    }
96
97
    function it_throws_an_exception_if_searching_for_files_with_empty_name()
98
    {
99
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['']);
100
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', [null]);
101
    }
102
103
    function it_throws_an_exception_if_there_is_no_file_that_matches_the_given_name(
104
        FinderFactoryInterface $finderFactory,
105
        Finder $finder
106
    ) {
107
        $finderFactory->create()->willReturn($finder);
108
109
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
110
        $finder->in(['/search/path/'])->shouldBeCalled()->willReturn($finder);
111
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
112
        $finder->files()->shouldBeCalled()->willReturn($finder);
113
114
        $finder->getIterator()->willReturn(new \ArrayIterator());
115
        $finder->count()->willReturn(0);
116
117
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFileNamed', ['readme.md']);
118
    }
119
120
    function it_throws_an_exception_if_there_is_there_are_not_any_files_that_matches_the_given_name(
121
        FinderFactoryInterface $finderFactory,
122
        Finder $finder
123
    ) {
124
        $finderFactory->create()->willReturn($finder);
125
126
        $finder->name('readme.md')->shouldBeCalled()->willReturn($finder);
127
        $finder->in(['/search/path/'])->shouldBeCalled()->willReturn($finder);
128
        $finder->ignoreUnreadableDirs()->shouldBeCalled()->willReturn($finder);
129
        $finder->files()->shouldBeCalled()->willReturn($finder);
130
131
        $finder->getIterator()->willReturn(new \ArrayIterator());
132
        $finder->count()->willReturn(0);
133
134
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateFilesNamed', ['readme.md']);
135
    }
136
}
137