diff --git a/tests/Model/EnvironmentTest.php b/tests/Model/EnvironmentTest.php new file mode 100644 index 00000000..774c5637 --- /dev/null +++ b/tests/Model/EnvironmentTest.php @@ -0,0 +1,99 @@ + ['href' => 'ssh://projectid-envmachinename--app1@ssh.example.com'], + 'pf:ssh:app2' => ['href' => 'ssh://projectid-envmachinename--app2@ssh.example.com'], + ]; + $haMultiAppWithInstanceDefault = [ + 'pf:ssh:app1' => ['href' => 'ssh://projectid-envmachinename--app1--2@ssh.example.com'], + 'pf:ssh:app1:0' => ['href' => 'ssh://projectid-envmachinename--app1--0@ssh.example.com'], + 'pf:ssh:app1:1' => ['href' => 'ssh://projectid-envmachinename--app1--1@ssh.example.com'], + 'pf:ssh:app1:2' => ['href' => 'ssh://projectid-envmachinename--app1--2@ssh.example.com'], + 'pf:ssh:app2' => ['href' => 'ssh://projectid-envmachinename--app1--2@ssh.example.com'], + 'pf:ssh:app2:0' => ['href' => 'ssh://projectid-envmachinename--app2--0@ssh.example.com'], + 'pf:ssh:app2:1' => ['href' => 'ssh://projectid-envmachinename--app2--1@ssh.example.com'], + 'pf:ssh:app2:2' => ['href' => 'ssh://projectid-envmachinename--app2--2@ssh.example.com'], + ]; + $haMultiAppNoInstanceDefault = [ + 'pf:ssh:app1:0' => ['href' => 'ssh://projectid-envmachinename--app1--0@ssh.example.com'], + 'pf:ssh:app1:1' => ['href' => 'ssh://projectid-envmachinename--app1--1@ssh.example.com'], + 'pf:ssh:app1:2' => ['href' => 'ssh://projectid-envmachinename--app1--2@ssh.example.com'], + 'pf:ssh:app2:0' => ['href' => 'ssh://projectid-envmachinename--app2--0@ssh.example.com'], + 'pf:ssh:app2:1' => ['href' => 'ssh://projectid-envmachinename--app2--1@ssh.example.com'], + 'pf:ssh:app2:2' => ['href' => 'ssh://projectid-envmachinename--app2--2@ssh.example.com'], + ]; + + /** @var array{'_links': string[], 'app': string, 'instance': string, 'result': string|false}[] $cases */ + $cases = [ + [ + '_links' => $multiApp, + 'app' => 'app1', + 'instance' => '', + 'result' => 'projectid-envmachinename--app1@ssh.example.com', + ], + [ + '_links' => $multiApp, + 'app' => 'app1', + 'instance' => '1', + 'result' => false, + ], + [ + '_links' => $haMultiAppWithInstanceDefault, + 'app' => 'app1', + 'instance' => '', + 'result' => 'projectid-envmachinename--app1--2@ssh.example.com', + ], + [ + '_links' => $haMultiAppWithInstanceDefault, + 'app' => 'app1', + 'instance' => '0', + 'result' => 'projectid-envmachinename--app1--0@ssh.example.com', + ], + [ + '_links' => $haMultiAppNoInstanceDefault, + 'app' => 'app1', + 'instance' => '', + 'result' => 'projectid-envmachinename--app1--0@ssh.example.com', + ], + [ + '_links' => $haMultiAppNoInstanceDefault, + 'app' => 'app1', + 'instance' => '1', + 'result' => 'projectid-envmachinename--app1--1@ssh.example.com', + ], + [ + '_links' => $haMultiAppNoInstanceDefault, + 'app' => 'app1', + 'instance' => '3', + 'result' => false, + ], + [ + '_links' => $haMultiAppNoInstanceDefault, + 'app' => 'app2', + 'instance' => '', + 'result' => 'projectid-envmachinename--app2--0@ssh.example.com', + ], + ]; + foreach ($cases as $i => $case) { + $environment = new Environment(['_links' => $case['_links']]); + 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"); + } + } +}