1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/configuration |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace spec\Slick\Configuration; |
11
|
|
|
|
12
|
|
|
use PhpSpec\Exception\Example\FailureException; |
13
|
|
|
use Slick\Configuration\Configuration; |
14
|
|
|
use PhpSpec\ObjectBehavior; |
15
|
|
|
use Slick\Configuration\ConfigurationInterface; |
16
|
|
|
use Slick\Configuration\Driver\Environment; |
17
|
|
|
use Slick\Configuration\Driver\Ini; |
18
|
|
|
use Slick\Configuration\Driver\Php; |
19
|
|
|
use Slick\Configuration\Exception\InvalidArgumentException; |
20
|
|
|
use Slick\Configuration\PriorityConfigurationChain; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ConfigurationSpec specs |
24
|
|
|
* |
25
|
|
|
* @package spec\Slick\Configuration |
26
|
|
|
*/ |
27
|
|
|
class ConfigurationSpec extends ObjectBehavior |
28
|
|
|
{ |
29
|
|
|
private $settingsFile; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
Configuration::addPath(__DIR__.'/Driver/fixtures'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function let() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->settingsFile = 'settings'; |
39
|
|
|
$this->beConstructedWith($this->settingsFile); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function it_is_initializable_with_a_driver_class_and_options() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->shouldHaveType(Configuration::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function it_initializes_a_php_driver_by_default() |
48
|
|
|
{ |
49
|
|
|
$chain = $this->initialize(); |
|
|
|
|
50
|
|
|
$chain->shouldBeAnInstanceOf(PriorityConfigurationChain::class); |
51
|
|
|
$chain->priorityList()->asArray()[0]->shouldBeAnInstanceOf(Php::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function it_can_determine_the_driver_for_a_given_file_extension() |
55
|
|
|
{ |
56
|
|
|
$this->beConstructedWith(__DIR__.'/Driver/fixtures/settings.ini'); |
57
|
|
|
$chain = $this->initialize(); |
58
|
|
|
$chain->shouldBeAnInstanceOf(PriorityConfigurationChain::class); |
59
|
|
|
$chain->priorityList()->asArray()[0]->shouldBeAnInstanceOf(Ini::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function it_throws_an_Exception_when_file_cannot_be_found() |
63
|
|
|
{ |
64
|
|
|
$this->beConstructedWith(); |
65
|
|
|
$this->shouldThrow(InvalidArgumentException::class) |
66
|
|
|
->during('initialize'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function it_throws_an_Exception_when_file_extension_is_unknown() |
70
|
|
|
{ |
71
|
|
|
$this->beConstructedWith('settings.cfg'); |
72
|
|
|
$this->shouldThrow(InvalidArgumentException::class) |
73
|
|
|
->during('initialize'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function it_can_be_created_with_multiple_drivers() |
77
|
|
|
{ |
78
|
|
|
$this->beConstructedWith([ |
79
|
|
|
[null, Configuration::DRIVER_ENV], |
80
|
|
|
[$this->settingsFile, null, 10], |
81
|
|
|
[__DIR__.'/Driver/fixtures/settings.ini'] |
82
|
|
|
]); |
83
|
|
|
$chain = $this->initialize(); |
84
|
|
|
$chain->shouldBeAnInstanceOf(PriorityConfigurationChain::class); |
85
|
|
|
$chain->priorityList()->asArray()[0]->shouldBeAnInstanceOf(Environment::class); |
86
|
|
|
$chain->priorityList()->asArray()[1]->shouldBeAnInstanceOf(Php::class); |
87
|
|
|
$chain->priorityList()->asArray()[2]->shouldBeAnInstanceOf(Ini::class); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function it_can_be_created_through_get() |
92
|
|
|
{ |
93
|
|
|
$this->beConstructedThrough('get', [$this->settingsFile]); |
94
|
|
|
$this->shouldHaveType(PriorityConfigurationChain::class); |
95
|
|
|
$object = Configuration::get('settings'); |
96
|
|
|
if ($this->getWrappedObject() !== $object) { |
97
|
|
|
throw new FailureException("Its not the same object..."); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function it_can_be_created_through_create() |
102
|
|
|
{ |
103
|
|
|
$this->beConstructedThrough('create', [$this->settingsFile]); |
104
|
|
|
$this->shouldHaveType(PriorityConfigurationChain::class); |
105
|
|
|
$object = Configuration::get('settings'); |
106
|
|
|
if ($this->getWrappedObject() === $object) { |
107
|
|
|
throw new FailureException("Its is the same object..."); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
function it_can_reach_nested_settings() |
112
|
|
|
{ |
113
|
|
|
Configuration::addPath(__DIR__); |
114
|
|
|
$this->beConstructedWith([ |
115
|
|
|
[null, Configuration::DRIVER_ENV, 10], |
116
|
|
|
['settings', Configuration::DRIVER_PHP, 20] |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$settings = $this->initialize(); |
120
|
|
|
|
121
|
|
|
$settings->get('some.deep.path')->shouldBe('/foo'); |
122
|
|
|
$settings->get('some.deep.value')->shouldBe('bar'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.