2 namespace Drush\SiteAlias;
4 use PHPUnit\Framework\TestCase;
6 use Consolidation\SiteAlias\Util\YamlDataFileLoader;
7 use Consolidation\SiteAlias\AliasRecord;
9 class SiteAliasFileLoaderTest extends TestCase
11 use \Drush\FixtureFactory;
12 use \Drush\FunctionUtils;
16 $this->sut = new SiteAliasFileLoader();
18 $ymlLoader = new YamlDataFileLoader();
19 $this->sut->addLoader('yml', $ymlLoader);
22 public function testLoadSingleAliasFile()
24 $siteAliasFixtures = $this->fixturesDir() . '/sitealiases/single';
25 $this->assertTrue(is_dir($siteAliasFixtures));
26 $this->assertTrue(is_file($siteAliasFixtures . '/simple.site.yml'));
27 $this->assertTrue(is_file($siteAliasFixtures . '/single.site.yml'));
29 $this->sut->addSearchLocation($siteAliasFixtures);
31 // Look for a simple alias with no environments defined
32 $name = new SiteAliasName('simple');
33 $result = $this->callProtected('loadSingleAliasFile', [$name]);
34 $this->assertEquals(AliasRecord::class, get_class($result));
35 $this->assertEquals('/path/to/simple', $result->get('root'));
37 // Look for a single alias without an environment specified.
38 $name = new SiteAliasName('single');
39 $result = $this->callProtected('loadSingleAliasFile', [$name]);
40 $this->assertTrue($result instanceof AliasRecord);
41 $this->assertEquals('/path/to/single', $result->get('root'));
42 $this->assertEquals('bar', $result->get('foo'));
44 // Same test, but with environment explicitly requested.
45 $name = new SiteAliasName('single', 'alternate');
46 $result = $this->callProtected('loadSingleAliasFile', [$name]);
47 $this->assertTrue($result instanceof AliasRecord);
48 $this->assertEquals('/alternate/path/to/single', $result->get('root'));
49 $this->assertEquals('bar', $result->get('foo'));
51 // Try to fetch an alias that does not exist.
52 $name = new SiteAliasName('missing');
53 $result = $this->callProtected('loadSingleAliasFile', [$name]);
54 $this->assertFalse($result);
57 public function testLoadLegacy()
59 $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/legacy');
62 public function testLoad()
64 $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/single');
65 $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/group');
67 // Look for a simple alias with no environments defined
68 $name = new SiteAliasName('simple');
69 $result = $this->sut->load($name);
70 $this->assertTrue($result instanceof AliasRecord);
71 $this->assertEquals('/path/to/simple', $result->get('root'));
73 // Look for a single alias without an environment specified.
74 $name = new SiteAliasName('single');
75 $result = $this->sut->load($name);
76 $this->assertTrue($result instanceof AliasRecord);
77 $this->assertEquals('/path/to/single', $result->get('root'));
78 $this->assertEquals('bar', $result->get('foo'));
80 // Same test, but with environment explicitly requested.
81 $name = new SiteAliasName('single', 'alternate');
82 $result = $this->sut->load($name);
83 $this->assertTrue($result instanceof AliasRecord);
84 $this->assertEquals('/alternate/path/to/single', $result->get('root'));
85 $this->assertEquals('bar', $result->get('foo'));
87 // Try to fetch an alias that does not exist.
88 $name = new SiteAliasName('missing');
89 $result = $this->sut->load($name);
90 $this->assertFalse($result);
92 // Try to fetch an alias that does not exist.
93 $name = new SiteAliasName('missing');
94 $result = $this->sut->load($name);
95 $this->assertFalse($result);
98 public function testLoadAll()
100 $this->sut->addSearchLocation($this->fixturesDir() . '/sitealiases/single');
102 $all = $this->sut->loadAll();
103 $this->assertEquals('@single.single.alternate,@single.single.dev', implode(',', array_keys($all)));