Completed
Push — 1.0 ( 32d139...1bbf5e )
by Kamil
79:12 queued 52:28
created

UrlPackageSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 7 1
A it_implements_package_interface() 0 4 1
A it_extends_symfony_url_package() 0 4 1
A it_returns_vanilla_url_if_there_are_no_active_themes_and_with_base_url() 0 15 1
A it_returns_modified_url_if_there_is_active_theme_and_with_base_url() 0 16 1
A it_returns_url_without_changes_if_it_is_absolute() 0 5 1
A it_does_prepend_it_with_base_url_if_modified_url_is_an_absolute_one() 0 16 1
A it_does_not_prepend_it_with_base_url_if_modified_url_is_an_absolute_url() 0 16 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\Asset\Package;
15
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Bundle\ThemeBundle\Asset\PathResolverInterface;
18
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
19
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
20
use Symfony\Component\Asset\PackageInterface;
21
use Symfony\Component\Asset\UrlPackage;
22
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
23
24
/**
25
 * @author Kamil Kokot <[email protected]>
26
 * @author Stefan Doorn <[email protected]>
27
 */
28
final class UrlPackageSpec extends ObjectBehavior
29
{
30
    function let(
31
        VersionStrategyInterface $versionStrategy,
32
        ThemeContextInterface $themeContext,
33
        PathResolverInterface $urlResolver
34
    ): void {
35
        $this->beConstructedWith(['https://cdn-url.com/'], $versionStrategy, $themeContext, $urlResolver);
36
    }
37
38
    function it_implements_package_interface(): void
39
    {
40
        $this->shouldImplement(PackageInterface::class);
41
    }
42
43
    function it_extends_symfony_url_package(): void
44
    {
45
        $this->shouldImplement(UrlPackage::class);
46
    }
47
48
    function it_returns_vanilla_url_if_there_are_no_active_themes_and_with_base_url(
49
        VersionStrategyInterface $versionStrategy,
50
        ThemeContextInterface $themeContext,
51
        PathResolverInterface $urlResolver
52
    ): void {
53
        $this->beConstructedWith('https://cdn-url.com/', $versionStrategy, $themeContext, $urlResolver);
54
55
        $url = 'bundles/sample/asset.js';
56
        $versionedPath = 'bundles/sample/asset.js?v=42';
57
58
        $themeContext->getTheme()->shouldBeCalled()->willReturn(null);
59
        $versionStrategy->applyVersion($url)->shouldBeCalled()->willReturn($versionedPath);
60
61
        $this->getUrl($url)->shouldReturn('https://cdn-url.com/' . $versionedPath);
62
    }
63
64
    function it_returns_modified_url_if_there_is_active_theme_and_with_base_url(
65
        ThemeContextInterface $themeContext,
66
        VersionStrategyInterface $versionStrategy,
67
        PathResolverInterface $urlResolver,
68
        ThemeInterface $theme
69
    ): void {
70
        $url = 'bundles/sample/asset.js';
71
        $themedPath = 'bundles/theme/foo/bar/sample/asset.js';
72
        $versionedThemedPath = 'bundles/theme/foo/bar/sample/asset.js?v=42';
73
74
        $themeContext->getTheme()->shouldBeCalled()->willReturn($theme);
75
        $urlResolver->resolve($url, $theme)->shouldBeCalled()->willReturn($themedPath);
76
        $versionStrategy->applyVersion($themedPath)->shouldBeCalled()->willReturn($versionedThemedPath);
77
78
        $this->getUrl($url)->shouldReturn('https://cdn-url.com/' . $versionedThemedPath);
79
    }
80
81
    function it_returns_url_without_changes_if_it_is_absolute(): void
82
    {
83
        $this->getUrl('//localhost/asset.js')->shouldReturn('//localhost/asset.js');
84
        $this->getUrl('https://localhost/asset.js')->shouldReturn('https://localhost/asset.js');
85
    }
86
87
    function it_does_prepend_it_with_base_url_if_modified_url_is_an_absolute_one(
88
        ThemeContextInterface $themeContext,
89
        VersionStrategyInterface $versionStrategy,
90
        PathResolverInterface $urlResolver,
91
        ThemeInterface $theme
92
    ): void {
93
        $url = 'bundles/sample/asset.js';
94
        $themedPath = 'bundles/theme/foo/bar/sample/asset.js';
95
        $versionedThemedPath = 'https://cdn-url.com/bundles/theme/foo/bar/sample/asset.js?v=42';
96
97
        $themeContext->getTheme()->shouldBeCalled()->willReturn($theme);
98
        $urlResolver->resolve($url, $theme)->shouldBeCalled()->willReturn($themedPath);
99
        $versionStrategy->applyVersion($themedPath)->shouldBeCalled()->willReturn($versionedThemedPath);
100
101
        $this->getUrl($url)->shouldReturn($versionedThemedPath);
102
    }
103
104
    function it_does_not_prepend_it_with_base_url_if_modified_url_is_an_absolute_url(
105
        ThemeContextInterface $themeContext,
106
        VersionStrategyInterface $versionStrategy,
107
        PathResolverInterface $urlResolver,
108
        ThemeInterface $theme
109
    ): void {
110
        $url = 'bundles/sample/asset.js';
111
        $themedPath = 'bundles/theme/foo/bar/sample/asset.js';
112
        $versionedThemedPath = 'https://bundles/theme/foo/bar/sample/asset.js?v=42';
113
114
        $themeContext->getTheme()->shouldBeCalled()->willReturn($theme);
115
        $urlResolver->resolve($url, $theme)->shouldBeCalled()->willReturn($themedPath);
116
        $versionStrategy->applyVersion($themedPath)->shouldBeCalled()->willReturn($versionedThemedPath);
117
118
        $this->getUrl($url)->shouldReturn($versionedThemedPath);
119
    }
120
}
121