diff --git a/src/SiteAliasFileLoader.php b/src/SiteAliasFileLoader.php index 4f5cdc8..ce422e7 100644 --- a/src/SiteAliasFileLoader.php +++ b/src/SiteAliasFileLoader.php @@ -217,7 +217,7 @@ protected function createAliasRecordsFromSiteData($sitename, $siteData, $locatio return $result; } foreach ($siteData as $envName => $data) { - if (is_array($data)) { + if (is_array($data) && $this->isValidEnvName($envName)) { $aliasName = new SiteAliasName($sitename, $envName, $location); $processor = new ConfigProcessor(); @@ -228,6 +228,17 @@ protected function createAliasRecordsFromSiteData($sitename, $siteData, $locatio return $result; } + /** + * isValidEnvName determines if a given entry should be skipped or not + * (e.g. the "common" entry). + * + * @param string $envName The environment name to test + */ + protected function isValidEnvName($envName) + { + return $envName != 'common'; + } + /** * Store an alias record in a list. If the alias record has * a known name, then the key of the list will be the record's name. diff --git a/src/SiteAliasManager.php b/src/SiteAliasManager.php index cbb5df9..2efce6b 100644 --- a/src/SiteAliasManager.php +++ b/src/SiteAliasManager.php @@ -30,6 +30,7 @@ public function __construct($aliasLoader = null, $root = '') public function setReferenceData($data) { $this->aliasLoader->setReferenceData($data); + return $this; } /** @@ -160,10 +161,10 @@ public function getAlias($aliasName) * If the provided name is a site specification et. al., * then this method will return 'false'. * - * @param string $name Alias name or site specification + * @param string $name Alias name * @return AliasRecord[]|false */ - public function getMultiple($name) + public function getMultiple($name = '') { if (empty($name)) { return $this->aliasLoader->loadAll(); diff --git a/tests/SiteAliasFileLoaderTest.php b/tests/SiteAliasFileLoaderTest.php index 7e55ac3..7c66758 100644 --- a/tests/SiteAliasFileLoaderTest.php +++ b/tests/SiteAliasFileLoaderTest.php @@ -142,7 +142,7 @@ public function testLoadAll() $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/other'); $all = $this->sut->loadAll(); - $this->assertEquals('@other.single.common,@other.single.dev,@other.single.other,@single.alternate,@single.common,@single.dev,@wild.*,@wild.common,@wild.dev', implode(',', array_keys($all))); + $this->assertEquals('@other.single.dev,@other.single.other,@single.alternate,@single.dev,@wild.*,@wild.dev', implode(',', array_keys($all))); } public function testLoadMultiple() @@ -151,7 +151,7 @@ public function testLoadMultiple() $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/other'); $aliases = $this->sut->loadMultiple('single'); - $this->assertEquals('@single.dev,@single.alternate,@single.common,@other.single.dev,@other.single.other,@other.single.common', implode(',', array_keys($aliases))); + $this->assertEquals('@single.dev,@single.alternate,@other.single.dev,@other.single.other', implode(',', array_keys($aliases))); } public function testLoadLocation() @@ -160,6 +160,6 @@ public function testLoadLocation() $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/other'); $aliases = $this->sut->loadLocation('other'); - $this->assertEquals('@other.single.dev,@other.single.other,@other.single.common', implode(',', array_keys($aliases))); + $this->assertEquals('@other.single.dev,@other.single.other', implode(',', array_keys($aliases))); } } diff --git a/tests/SiteAliasManagerTest.php b/tests/SiteAliasManagerTest.php new file mode 100644 index 0000000..090a802 --- /dev/null +++ b/tests/SiteAliasManagerTest.php @@ -0,0 +1,203 @@ +siteDir(); + $referenceData = []; + $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/sites'; + + $aliasLoader = new SiteAliasFileLoader(); + $ymlLoader = new YamlDataFileLoader(); + $aliasLoader->addLoader('yml', $ymlLoader); + + $this->manager = new SiteAliasManager($aliasLoader, $root); + $this->manager + ->setReferenceData($referenceData) + ->addSearchLocation($siteAliasFixtures); + } + + public function managerGetTestValues() + { + return [ + [ + '@single.other', false, + ], + + [ + '@other.single.other', false, + ], + + [ + '@single.dev', 'foo: bar +root: /path/to/single', + ], + + ]; + } + + public function managerGetWithOtherLocationTestValues() + { + return [ + [ + '@single.other', false, + ], + + [ + '@other.single.other', 'foo: baz +root: /other/other/path/to/single', + ], + + [ + '@single.dev', 'foo: bar +root: /path/to/single', + ], + + ]; + } + + public function managerGetWithDupLocationsTestValues() + { + return [ + [ + '@single.dev', 'foo: bar +root: /path/to/single', + ], + + [ + '@other.single.dev', 'foo: baz +root: /other/path/to/single', + ], + + [ + '@dup.single.dev', 'foo: dup +root: /dup/path/to/single', + ], + + ]; + } + + /** + * This test is just to ensure that our fixture data is being loaded + * accurately so that we can start writing tests. Its okay to remove + * rather than maintain this test once the suite is mature. + */ + public function testGetMultiple() + { + // First set of tests: get all aliases in the default location + + $all = $this->manager->getMultiple(); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@single.alternate,@single.dev,@wild.*,@wild.dev', implode(',', $allNames)); + + $all = $this->manager->getMultiple('@single'); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@single.alternate,@single.dev', implode(',', $allNames)); + + // Next set of tests: Get all aliases in the 'other' location + + $this->addAlternateLocation('other'); + + $all = $this->manager->getMultiple(); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@other.single.dev,@other.single.other,@single.alternate,@single.dev,@wild.*,@wild.dev', implode(',', $allNames)); + + $all = $this->manager->getMultiple('@other'); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@other.single.dev,@other.single.other', implode(',', $allNames)); + + // Add the 'dup' location and do some more tests + + $this->addAlternateLocation('dup'); + + $all = $this->manager->getMultiple(); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@dup.single.alternate,@dup.single.dev,@other.single.dev,@other.single.other,@single.alternate,@single.dev,@wild.*,@wild.dev', implode(',', $allNames)); + + $all = $this->manager->getMultiple('@dup'); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@dup.single.alternate,@dup.single.dev', implode(',', $allNames)); + + $all = $this->manager->getMultiple('@other'); + $allNames = array_keys($all); + sort($allNames); + + $this->assertEquals('@other.single.dev,@other.single.other', implode(',', $allNames)); + } + + /** + * @dataProvider managerGetTestValues + */ + public function testGet( + $aliasName, + $expected) + { + $alias = $this->manager->get($aliasName); + $actual = $this->renderAlias($alias); + $this->assertEquals($expected, $actual); + } + + /** + * @dataProvider managerGetWithOtherLocationTestValues + */ + public function testGetWithOtherLocation( + $aliasName, + $expected) + { + $this->addAlternateLocation('other'); + $this->testGet($aliasName, $expected); + } + + /** + * @dataProvider managerGetWithDupLocationsTestValues + */ + public function testGetWithDupLocations( + $aliasName, + $expected) + { + $this->addAlternateLocation('dup'); + $this->testGetWithOtherLocation($aliasName, $expected); + } + + protected function addAlternateLocation($fixtureDirName) + { + // Add another search location IN ADDITION to the one + // already added in the setup() mehtod. + $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/' . $fixtureDirName; + $this->manager->addSearchLocation($siteAliasFixtures); + } + + protected function renderAlias($alias) + { + if (!$alias) { + return false; + } + + return trim(Yaml::Dump($alias->export())); + } +} diff --git a/tests/fixtures/sitealiases/dup/single.site.yml b/tests/fixtures/sitealiases/dup/single.site.yml new file mode 100644 index 0000000..eb4634b --- /dev/null +++ b/tests/fixtures/sitealiases/dup/single.site.yml @@ -0,0 +1,8 @@ +default: dev +dev: + root: /dup/path/to/single +alternate: + root: /dup/alternate/path/to/single +common: + foo: dup +