Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias manager tests #5

Merged
merged 2 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/SiteAliasFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/SiteAliasManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct($aliasLoader = null, $root = '')
public function setReferenceData($data)
{
$this->aliasLoader->setReferenceData($data);
return $this;
}

/**
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions tests/SiteAliasFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)));
}
}
203 changes: 203 additions & 0 deletions tests/SiteAliasManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php
namespace Consolidation\SiteAlias;

use Consolidation\SiteAlias\Util\YamlDataFileLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

class SiteAliasManagerTest extends TestCase
{
use FixtureFactory;

protected $manager;

/**
* Set up for tests
*/
public function setUp()
{
$root = $this->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()));
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/sitealiases/dup/single.site.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default: dev
dev:
root: /dup/path/to/single
alternate:
root: /dup/alternate/path/to/single
common:
foo: dup