Completed
Push — master ( 4207f0...333332 )
by Kamil
23:19 queued 01:42
created

it_locates_bundle_resource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 13
nc 1
nop 5
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\Locator\BundleResourceLocator;
17
use Sylius\Bundle\ThemeBundle\Locator\ResourceLocatorInterface;
18
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException;
19
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
22
use Symfony\Component\HttpKernel\KernelInterface;
23
24
/**
25
 * @mixin BundleResourceLocator
26
 *
27
 * @author Kamil Kokot <[email protected]>
28
 */
29
class BundleResourceLocatorSpec extends ObjectBehavior
30
{
31
    function let(Filesystem $filesystem, KernelInterface $kernel)
32
    {
33
        $this->beConstructedWith($filesystem, $kernel);
34
    }
35
36
    function it_is_initializable()
37
    {
38
        $this->shouldHaveType('Sylius\Bundle\ThemeBundle\Locator\BundleResourceLocator');
39
    }
40
41
    function it_implements_resource_locator_interface()
42
    {
43
        $this->shouldImplement(ResourceLocatorInterface::class);
44
    }
45
46
    function it_locates_bundle_resource(
47
        Filesystem $filesystem,
48
        KernelInterface $kernel,
49
        ThemeInterface $theme,
50
        BundleInterface $childBundle,
51
        BundleInterface $parentBundle
52
    ) {
53
        $kernel->getBundle('ParentBundle', false)->willReturn([$childBundle, $parentBundle]);
54
55
        $childBundle->getName()->willReturn('ChildBundle');
56
        $parentBundle->getName()->willReturn('ParentBundle');
57
58
        $theme->getPath()->willReturn('/theme/path');
59
60
        $filesystem->exists('/theme/path/ChildBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);
61
        $filesystem->exists('/theme/path/ParentBundle/views/index.html.twig')->shouldBeCalled()->willReturn(true);
62
63
        $this->locateResource('@ParentBundle/Resources/views/index.html.twig', $theme)->shouldReturn('/theme/path/ParentBundle/views/index.html.twig');
64
    }
65
66
    function it_throws_an_exception_if_resource_can_not_be_located(
67
        Filesystem $filesystem,
68
        KernelInterface $kernel,
69
        ThemeInterface $theme,
70
        BundleInterface $childBundle,
71
        BundleInterface $parentBundle
72
    ) {
73
        $kernel->getBundle('ParentBundle', false)->willReturn([$childBundle, $parentBundle]);
74
75
        $childBundle->getName()->willReturn('ChildBundle');
76
        $parentBundle->getName()->willReturn('ParentBundle');
77
78
        $theme->getSlug()->willReturn('theme/slug');
79
        $theme->getPath()->willReturn('/theme/path');
80
81
        $filesystem->exists('/theme/path/ChildBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);
82
        $filesystem->exists('/theme/path/ParentBundle/views/index.html.twig')->shouldBeCalled()->willReturn(false);
83
84
        $this->shouldThrow(ResourceNotFoundException::class)->during('locateResource', ['@ParentBundle/Resources/views/index.html.twig', $theme]);
85
    }
86
87
    function it_throws_an_exception_if_resource_path_does_not_start_with_an_asperand(ThemeInterface $theme)
88
    {
89
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['ParentBundle/Resources/views/index.html.twig', $theme]);
90
    }
91
92
    function it_throws_an_exception_if_resource_path_contains_two_dots_in_a_row(ThemeInterface $theme)
93
    {
94
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['@ParentBundle/Resources/views/../views/index.html.twig', $theme]);
95
    }
96
97
    function it_throws_an_exception_if_resource_path_does_not_contain_resources_dir(ThemeInterface $theme)
98
    {
99
        $this->shouldThrow(\InvalidArgumentException::class)->during('locateResource', ['@ParentBundle/views/Resources.index.html.twig', $theme]);
100
    }
101
}
102