00ac33a51120aef335dbe001d342bd968eeb5e25
[yaffs-website] / vendor / consolidation / site-alias / tests / SiteAliasManagerTest.php
1 <?php
2 namespace Consolidation\SiteAlias;
3
4 use Consolidation\SiteAlias\Util\YamlDataFileLoader;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Yaml\Yaml;
7
8 class SiteAliasManagerTest extends TestCase
9 {
10     use FixtureFactory;
11
12     protected $manager;
13
14     /**
15      * Set up for tests
16      */
17     public function setUp()
18     {
19         $root = $this->siteDir();
20         $referenceData = [];
21         $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/sites';
22
23         $aliasLoader = new SiteAliasFileLoader();
24         $ymlLoader = new YamlDataFileLoader();
25         $aliasLoader->addLoader('yml', $ymlLoader);
26
27         $this->manager = new SiteAliasManager($aliasLoader, $root);
28         $this->manager
29             ->setReferenceData($referenceData)
30             ->addSearchLocation($siteAliasFixtures);
31     }
32
33     public function managerGetTestValues()
34     {
35         return [
36             [
37                 '@single.other', false,
38             ],
39
40             [
41                 '@other.single.other', false,
42             ],
43
44             [
45                 '@single.dev', 'foo: bar
46 root: /path/to/single',
47             ],
48
49         ];
50     }
51
52     public function managerGetWithOtherLocationTestValues()
53     {
54         return [
55             [
56                 '@single.other', false,
57             ],
58
59             [
60                 '@other.single.other', 'foo: baz
61 root: /other/other/path/to/single',
62             ],
63
64             [
65                 '@single.dev', 'foo: bar
66 root: /path/to/single',
67             ],
68
69         ];
70     }
71
72     public function managerGetWithDupLocationsTestValues()
73     {
74         return [
75             [
76                 '@single.dev', 'foo: bar
77 root: /path/to/single',
78             ],
79
80             [
81                 '@other.single.dev', 'foo: baz
82 root: /other/path/to/single',
83             ],
84
85             [
86                 '@dup.single.dev', 'foo: dup
87 root: /dup/path/to/single',
88             ],
89
90         ];
91     }
92
93     /**
94      * This test is just to ensure that our fixture data is being loaded
95      * accurately so that we can start writing tests. Its okay to remove
96      * rather than maintain this test once the suite is mature.
97      */
98     public function testGetMultiple()
99     {
100         // First set of tests: get all aliases in the default location
101
102         $all = $this->manager->getMultiple();
103         $allNames = array_keys($all);
104         sort($allNames);
105
106         $this->assertEquals('@single.alternate,@single.dev,@single.empty,@wild.*,@wild.dev', implode(',', $allNames));
107
108         $all = $this->manager->getMultiple('@single');
109         $allNames = array_keys($all);
110         sort($allNames);
111
112         $this->assertEquals('@single.alternate,@single.dev,@single.empty', implode(',', $allNames));
113
114         // Next set of tests: Get all aliases in the 'other' location
115
116         $this->addAlternateLocation('other');
117
118         $all = $this->manager->getMultiple();
119         $allNames = array_keys($all);
120         sort($allNames);
121
122         $this->assertEquals('@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other,@single.alternate,@single.dev,@single.empty,@wild.*,@wild.dev', implode(',', $allNames));
123
124         $all = $this->manager->getMultiple('@other');
125         $allNames = array_keys($all);
126         sort($allNames);
127
128         $this->assertEquals('@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other', implode(',', $allNames));
129
130         // Add the 'dup' location and do some more tests
131
132         $this->addAlternateLocation('dup');
133
134         $all = $this->manager->getMultiple();
135         $allNames = array_keys($all);
136         sort($allNames);
137
138         $this->assertEquals('@dup.bob.dev,@dup.bob.other,@dup.fred.dev,@dup.fred.other,@dup.single.alternate,@dup.single.dev,@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other,@single.alternate,@single.dev,@single.empty,@wild.*,@wild.dev', implode(',', $allNames));
139
140         $all = $this->manager->getMultiple('@dup');
141         $allNames = array_keys($all);
142         sort($allNames);
143
144         $this->assertEquals('@dup.bob.dev,@dup.bob.other,@dup.fred.dev,@dup.fred.other,@dup.single.alternate,@dup.single.dev', implode(',', $allNames));
145
146         $all = $this->manager->getMultiple('@other');
147         $allNames = array_keys($all);
148         sort($allNames);
149
150         $this->assertEquals('@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other', implode(',', $allNames));
151
152         $all = $this->manager->getMultiple('@dup.single');
153         $allNames = array_keys($all);
154         sort($allNames);
155
156         $this->assertEquals('@dup.single.alternate,@dup.single.dev', implode(',', $allNames));
157
158         $all = $this->manager->getMultiple('@other.single');
159         $allNames = array_keys($all);
160         sort($allNames);
161
162         $this->assertEquals('@other.single.dev,@other.single.other', implode(',', $allNames));
163     }
164
165     /**
166      * @dataProvider managerGetTestValues
167      */
168     public function testGet(
169         $aliasName,
170         $expected)
171     {
172         $alias = $this->manager->get($aliasName);
173         $actual = $this->renderAlias($alias);
174         $this->assertEquals($expected, $actual);
175     }
176
177     /**
178      * @dataProvider managerGetWithOtherLocationTestValues
179      */
180     public function testGetWithOtherLocation(
181         $aliasName,
182         $expected)
183     {
184         $this->addAlternateLocation('other');
185         $this->testGet($aliasName, $expected);
186     }
187
188     /**
189      * @dataProvider managerGetWithDupLocationsTestValues
190      */
191     public function testGetWithDupLocations(
192         $aliasName,
193         $expected)
194     {
195         $this->addAlternateLocation('dup');
196         $this->testGetWithOtherLocation($aliasName, $expected);
197     }
198
199     protected function addAlternateLocation($fixtureDirName)
200     {
201         // Add another search location IN ADDITION to the one
202         // already added in the setup() mehtod.
203         $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/' . $fixtureDirName;
204         $this->manager->addSearchLocation($siteAliasFixtures);
205     }
206
207     protected function renderAlias($alias)
208     {
209         if (!$alias) {
210             return false;
211         }
212
213         return trim(Yaml::Dump($alias->export()));
214     }
215 }