Completed
Push — master ( 6f962e...549eec )
by Kamil
21:17
created

createSettingsFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 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
namespace spec\Sylius\Bundle\ThemeBundle\Settings;
13
14
use org\bovigo\vfs\vfsStream as VfsStream;
15
use org\bovigo\vfs\vfsStreamDirectory as VfsStreamDirectory;
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Bundle\SettingsBundle\Schema\SchemaInterface;
19
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
20
use Sylius\Bundle\ThemeBundle\Settings\ThemeSettingsSchemaProvider;
21
use Sylius\Bundle\ThemeBundle\Settings\ThemeSettingsSchemaProviderInterface;
22
23
/**
24
 * @mixin ThemeSettingsSchemaProvider
25
 *
26
 * @author Kamil Kokot <[email protected]>
27
 */
28
final class ThemeSettingsSchemaProviderSpec extends ObjectBehavior
29
{
30
    /**
31
     * @var VfsStreamDirectory
32
     */
33
    private $vfsStream;
34
35
    function let()
36
    {
37
        $this->vfsStream = VfsStream::setup();
38
    }
39
40
    function it_is_initializable()
41
    {
42
        $this->shouldHaveType('Sylius\Bundle\ThemeBundle\Settings\ThemeSettingsSchemaProvider');
43
    }
44
45
    function it_implements_theme_settings_schema_provider_interface()
46
    {
47
        $this->shouldImplement(ThemeSettingsSchemaProviderInterface::class);
48
    }
49
50
    function it_returns_valid_settings_schema(ThemeInterface $theme)
51
    {
52
        $settingsPath = $this->createSettingsFile(
53
<<<'PHP'
54
<?php
55
56
use Sylius\Bundle\SettingsBundle\Schema\CallbackSchema;
57
use Sylius\Bundle\SettingsBundle\Schema\SettingsBuilderInterface;
58
use Symfony\Component\Form\FormBuilderInterface;
59
60
return new CallbackSchema(
61
    function (SettingsBuilderInterface $settingsBuilder) {
62
63
    },
64
    function (FormBuilderInterface $formBuilder) {
65
66
    }
67
);
68
PHP
69
        );
70
71
        $theme->getPath()->willReturn(dirname($settingsPath));
72
73
        $this->getSchema($theme)->shouldHaveType(SchemaInterface::class);
74
    }
75
76
    function it_throws_an_exception_if_settings_schema_is_of_incorrect_type(ThemeInterface $theme)
77
    {
78
        $settingsPath = $this->createSettingsFile(
79
<<<'PHP'
80
<?php
81
82
return new \stdClass();
83
PHP
84
        );
85
86
        $theme->getPath()->willReturn(dirname($settingsPath));
87
88
        $this
89
            ->shouldThrow(new \InvalidArgumentException(sprintf(
90
                'File "%s" must return an instance of "%s"',
91
                $settingsPath,
92
                SchemaInterface::class
93
            )))
94
            ->during('getSchema', [$theme])
95
        ;
96
    }
97
98
    function it_throws_an_exception_if_settings_schema_does_not_exist(ThemeInterface $theme)
99
    {
100
        $theme->getTitle()->willReturn('Candy shop');
101
        $theme->getName()->willReturn('candy/shop');
102
103
        $theme->getPath()->willReturn($this->vfsStream->url());
104
105
        $this
106
            ->shouldThrow(new \InvalidArgumentException(sprintf(
107
                'Could not find settings schema of theme "Candy shop" (candy/shop) in file "%s"',
108
                $this->vfsStream->url() . '/Settings.php'
109
            )))
110
            ->during('getSchema', [$theme])
111
        ;
112
    }
113
114
    /**
115
     * @param string $content
116
     *
117
     * @return string Created file URL
118
     */
119
    private function createSettingsFile($content)
120
    {
121
        $settingsFile = VfsStream::newFile('Settings.php');
122
        $settingsFile->setContent($content);
123
124
        $this->vfsStream->addChild($settingsFile);
125
126
        return $settingsFile->url();
127
    }
128
}
129