2 namespace Consolidation\SiteAlias;
4 use Consolidation\SiteAlias\Util\YamlDataFileLoader;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Yaml\Yaml;
8 class SiteAliasManagerTest extends TestCase
17 public function setUp()
19 $root = $this->siteDir();
21 $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/sites';
23 $aliasLoader = new SiteAliasFileLoader();
24 $ymlLoader = new YamlDataFileLoader();
25 $aliasLoader->addLoader('yml', $ymlLoader);
27 $this->manager = new SiteAliasManager($aliasLoader, $root);
29 ->setReferenceData($referenceData)
30 ->addSearchLocation($siteAliasFixtures);
33 public function managerGetTestValues()
37 '@single.other', false,
41 '@other.single.other', false,
45 '@single.dev', 'foo: bar
46 root: /path/to/single',
52 public function managerGetWithOtherLocationTestValues()
56 '@single.other', false,
60 '@other.single.other', 'foo: baz
61 root: /other/other/path/to/single',
65 '@single.dev', 'foo: bar
66 root: /path/to/single',
72 public function managerGetWithDupLocationsTestValues()
76 '@single.dev', 'foo: bar
77 root: /path/to/single',
81 '@other.single.dev', 'foo: baz
82 root: /other/path/to/single',
86 '@dup.single.dev', 'foo: dup
87 root: /dup/path/to/single',
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.
98 public function testGetMultiple()
100 // First set of tests: get all aliases in the default location
102 $all = $this->manager->getMultiple();
103 $allNames = array_keys($all);
106 $this->assertEquals('@single.alternate,@single.dev,@single.empty,@wild.*,@wild.dev', implode(',', $allNames));
108 $all = $this->manager->getMultiple('@single');
109 $allNames = array_keys($all);
112 $this->assertEquals('@single.alternate,@single.dev,@single.empty', implode(',', $allNames));
114 // Next set of tests: Get all aliases in the 'other' location
116 $this->addAlternateLocation('other');
118 $all = $this->manager->getMultiple();
119 $allNames = array_keys($all);
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));
124 $all = $this->manager->getMultiple('@other');
125 $allNames = array_keys($all);
128 $this->assertEquals('@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other', implode(',', $allNames));
130 // Add the 'dup' location and do some more tests
132 $this->addAlternateLocation('dup');
134 $all = $this->manager->getMultiple();
135 $allNames = array_keys($all);
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));
140 $all = $this->manager->getMultiple('@dup');
141 $allNames = array_keys($all);
144 $this->assertEquals('@dup.bob.dev,@dup.bob.other,@dup.fred.dev,@dup.fred.other,@dup.single.alternate,@dup.single.dev', implode(',', $allNames));
146 $all = $this->manager->getMultiple('@other');
147 $allNames = array_keys($all);
150 $this->assertEquals('@other.bob.dev,@other.bob.other,@other.fred.dev,@other.fred.other,@other.single.dev,@other.single.other', implode(',', $allNames));
152 $all = $this->manager->getMultiple('@dup.single');
153 $allNames = array_keys($all);
156 $this->assertEquals('@dup.single.alternate,@dup.single.dev', implode(',', $allNames));
158 $all = $this->manager->getMultiple('@other.single');
159 $allNames = array_keys($all);
162 $this->assertEquals('@other.single.dev,@other.single.other', implode(',', $allNames));
166 * @dataProvider managerGetTestValues
168 public function testGet(
172 $alias = $this->manager->get($aliasName);
173 $actual = $this->renderAlias($alias);
174 $this->assertEquals($expected, $actual);
178 * @dataProvider managerGetWithOtherLocationTestValues
180 public function testGetWithOtherLocation(
184 $this->addAlternateLocation('other');
185 $this->testGet($aliasName, $expected);
189 * @dataProvider managerGetWithDupLocationsTestValues
191 public function testGetWithDupLocations(
195 $this->addAlternateLocation('dup');
196 $this->testGetWithOtherLocation($aliasName, $expected);
199 protected function addAlternateLocation($fixtureDirName)
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);
207 protected function renderAlias($alias)
213 return trim(Yaml::Dump($alias->export()));