Completed
Push — scalar-types/theme ( 71f30c )
by Kamil
21:15
created

BundleResourceLocatorSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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