Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Model/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public function getHeadCommit()
public function getSshUrl($app = '', $instance = '')
{
$urls = $this->getSshUrls();
$instances = $this->getSshInstanceURLs($app, $urls);
if ($instance !== '' && $instance !== null) {
$instances = $this->getSshInstanceURLs($app, $urls);
if (isset($instances[$instance])) {
return $instances[$instance];
}
Expand All @@ -132,6 +132,9 @@ public function getSshUrl($app = '', $instance = '')
if (isset($urls[$app])) {
return $urls[$app];
}
if (!empty($instances)) {
return reset($instances);
}

// Fall back to the legacy SSH URL.
return $this->constructLegacySshUrl();
Expand Down
102 changes: 102 additions & 0 deletions tests/Model/EnvironmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Platformsh\Client\Tests\Model;

use Platformsh\Client\Model\Environment;

class EnvironmentTest extends \PHPUnit_Framework_TestCase
{
public function testGetSshUrl()
{
$multiApp = [
'ssh' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2' => ['href' => 'ssh://[email protected]'],
];
$haMultiAppWithInstanceDefault = [
'ssh' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:0' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:2' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:0' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:2' => ['href' => 'ssh://[email protected]'],
];
$haMultiAppNoInstanceDefault = [
'ssh' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:0' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app1:2' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:0' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:1' => ['href' => 'ssh://[email protected]'],
'pf:ssh:app2:2' => ['href' => 'ssh://[email protected]'],
];

/** @var array{'_links': string[], 'app': string, 'instance': string, 'result': string|false}[] $cases */
$cases = [
[
'_links' => $multiApp,
'app' => 'app1',
'instance' => '',
'result' => '[email protected]',
],
[
'_links' => $multiApp,
'app' => 'app1',
'instance' => '1',
'result' => false,
],
[
'_links' => $haMultiAppWithInstanceDefault,
'app' => 'app1',
'instance' => '',
'result' => '[email protected]',
],
[
'_links' => $haMultiAppWithInstanceDefault,
'app' => 'app1',
'instance' => '0',
'result' => '[email protected]',
],
[
'_links' => $haMultiAppNoInstanceDefault,
'app' => 'app1',
'instance' => '',
'result' => '[email protected]',
],
[
'_links' => $haMultiAppNoInstanceDefault,
'app' => 'app1',
'instance' => '1',
'result' => '[email protected]',
],
[
'_links' => $haMultiAppNoInstanceDefault,
'app' => 'app1',
'instance' => '3',
'result' => false,
],
[
'_links' => $haMultiAppNoInstanceDefault,
'app' => 'app2',
'instance' => '',
'result' => '[email protected]',
],
];
foreach ($cases as $i => $case) {
$environment = new Environment(['id' => 'main', 'status' => 'active', '_links' => $case['_links']], 'https://example.com/projects/foo');
if ($case['result'] === false) {
try {
$environment->getSshUrl($case['app'], $case['instance']);
} catch (\InvalidArgumentException $e) {
$this->assertContains('SSH URL not found for instance', $e->getMessage(), "case $i");
}
continue;
}
$result = $environment->getSshUrl($case['app'], $case['instance']);
$this->assertEquals($case['result'], $result, "case $i");
}
}
}