Pull merge.
[yaffs-website] / vendor / consolidation / site-alias / tests / SiteSpecParserTest.php
1 <?php
2 namespace Consolidation\SiteAlias;
3
4 use PHPUnit\Framework\TestCase;
5
6 class SiteSpecParserTest extends TestCase
7 {
8     use FixtureFactory;
9
10     /**
11      * @dataProvider parserTestValues
12      */
13     public function testSiteSpecParser(
14         $spec,
15         $expected)
16     {
17         $root = $this->siteDir();
18         $fixtureSite = '/' . basename($root);
19         $parser = new SiteSpecParser();
20
21         // If the test spec begins with '/fixtures', substitute the
22         // actual path to our fixture site.
23         $spec = preg_replace('%^/fixtures%', $root, $spec);
24
25         // Make sure that our spec is valid
26         $this->assertTrue($parser->validSiteSpec($spec));
27
28         // Parse it!
29         $result = $parser->parse($spec, $root);
30
31         // If the result contains the path to our fixtures site, replace
32         // it with the simple string '/fixtures'.
33         if (isset($result['root'])) {
34             $result['root'] = preg_replace("%.*$fixtureSite%", '/fixtures', $result['root']);
35         }
36
37         // Compare the altered result with the expected value.
38         $this->assertEquals($expected, $result);
39     }
40
41     /**
42      * @dataProvider validSiteSpecs
43      */
44     public function testValidSiteSpecs($spec)
45     {
46         $this->isSpecValid($spec, true);
47     }
48
49     /**
50      * @dataProvider invalidSiteSpecs
51      */
52     public function testInvalidSiteSpecs($spec)
53     {
54         $this->isSpecValid($spec, false);
55     }
56
57     protected function isSpecValid($spec, $expected)
58     {
59         $parser = new SiteSpecParser();
60
61         $result = $parser->validSiteSpec($spec);
62         $this->assertEquals($expected, $result);
63     }
64
65     public static function validSiteSpecs()
66     {
67         return [
68             [ '/path/to/drupal#uri' ],
69             [ 'user@server/path/to/drupal#uri' ],
70             [ 'user.name@example.com/path/to/drupal#uri' ],
71             [ 'user@server/path/to/drupal' ],
72             [ 'user@example.com/path/to/drupal' ],
73             [ 'user@server#uri' ],
74             [ 'user@example.com#uri' ],
75             [ '#uri' ],
76         ];
77     }
78
79     public static function invalidSiteSpecs()
80     {
81         return [
82             [ 'uri' ],
83             [ '@/#' ],
84             [ 'user@#uri' ],
85             [ '@server/path/to/drupal#uri' ],
86             [ 'user@server/path/to/drupal#' ],
87             [ 'user@server/path/to/drupal#uri!' ],
88             [ 'user@server/path/to/drupal##uri' ],
89             [ 'user#server/path/to/drupal#uri' ],
90        ];
91     }
92
93     public static function parserTestValues()
94     {
95         return [
96             [
97                 'user@server/path#somemultisite',
98                 [
99                     'user' => 'user',
100                     'host' => 'server',
101                     'root' => '/path',
102                     'uri' => 'somemultisite',
103                 ],
104             ],
105
106             [
107                 'user.name@example.com/path#somemultisite',
108                 [
109                     'user' => 'user.name',
110                     'host' => 'example.com',
111                     'root' => '/path',
112                     'uri' => 'somemultisite',
113                 ],
114             ],
115
116             [
117                 'user@server/path',
118                 [
119                     'user' => 'user',
120                     'host' => 'server',
121                     'root' => '/path',
122                     'uri' => 'default',
123                 ],
124             ],
125
126             [
127                 'user.name@example.com/path',
128                 [
129                     'user' => 'user.name',
130                     'host' => 'example.com',
131                     'root' => '/path',
132                     'uri' => 'default',
133                 ],
134             ],
135
136             [
137                 '/fixtures#mymultisite',
138                 [
139                     'root' => '/fixtures',
140                     'uri' => 'mymultisite',
141                 ],
142             ],
143
144             [
145                 '#mymultisite',
146                 [
147                     'root' => '/fixtures',
148                     'uri' => 'mymultisite',
149                 ],
150             ],
151
152             [
153                 '/fixtures#somemultisite',
154                 [
155                 ],
156             ],
157
158             [
159                 '/path#somemultisite',
160                 [
161                 ],
162             ],
163
164             [
165                 '/path#mymultisite',
166                 [
167                 ],
168             ],
169
170             [
171                 '#somemultisite',
172                 [
173                 ],
174             ],
175         ];
176     }
177 }