Skip to content

Commit 313ecdc

Browse files
committed
Refactor ImportTest and add YamlRecipeTest
1 parent b94b957 commit 313ecdc

2 files changed

Lines changed: 83 additions & 37 deletions

File tree

tests/src/Import/ImportTest.php

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,37 @@
55
namespace Deployer\Import;
66

77
use Deployer\Deployer;
8+
use Deployer\Exception\Exception;
89
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Console\Application;
11+
use Symfony\Component\Console\Input\Input;
12+
use Symfony\Component\Console\Output\Output;
913

1014
class ImportTest extends TestCase
1115
{
12-
private $previousInput;
13-
private $previousOutput;
16+
private Deployer $deployer;
1417

1518
public function setUp(): void
1619
{
17-
$deployer = Deployer::get();
18-
$this->previousInput = $deployer->input;
19-
$this->previousOutput = $deployer->output;
20+
$console = new Application();
21+
$input = $this->createStub(Input::class);
22+
$output = $this->createStub(Output::class);
23+
24+
$this->deployer = new Deployer($console);
25+
$this->deployer['input'] = $input;
26+
$this->deployer['output'] = $output;
2027
}
2128

2229
public function tearDown(): void
2330
{
24-
Deployer::get()->input = $this->previousInput;
25-
Deployer::get()->output = $this->previousOutput;
31+
unset($this->deployer);
2632
}
2733

28-
public function testImporterIgnoresYamlHiddenKeys(): void
34+
public function testUnknownFileFormatThrows(): void
2935
{
30-
$data = <<<EOL
31-
.base: &base
32-
remote_user: foo
33-
labels:
34-
stage: production
35-
36-
hosts:
37-
acceptance:
38-
<<: *base
39-
labels:
40-
stage: acceptance
41-
42-
production:
43-
<<: *base
44-
remote_user: bar
45-
46-
production.beta:
47-
<<: *base
48-
# test.yaml
49-
EOL;
50-
51-
Import::import("data:text/yaml,$data");
52-
self::assertTrue(Deployer::get()->hosts->has('production'));
53-
self::assertTrue(Deployer::get()->hosts->has('acceptance'));
54-
self::assertTrue(Deployer::get()->hosts->has('production.beta'));
55-
self::assertEquals('acceptance', Deployer::get()->hosts->get('acceptance')->getLabels()['stage']);
56-
self::assertEquals('production', Deployer::get()->hosts->get('production')->getLabels()['stage']);
57-
self::assertEquals('foo', Deployer::get()->hosts->get('acceptance')->getRemoteUser());
58-
self::assertEquals('bar', Deployer::get()->hosts->get('production')->getRemoteUser());
36+
$this->expectException(Exception::class);
37+
$this->expectExceptionMessage('Unknown file format');
38+
39+
Import::import('file.txt');
5940
}
6041
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Deployer\Import;
6+
7+
use Deployer\Deployer;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Console\Application;
10+
use Symfony\Component\Console\Input\Input;
11+
use Symfony\Component\Console\Output\Output;
12+
13+
class YamlRecipeTest extends TestCase
14+
{
15+
private Deployer $deployer;
16+
17+
public function setUp(): void
18+
{
19+
$console = new Application();
20+
$input = $this->createStub(Input::class);
21+
$output = $this->createStub(Output::class);
22+
23+
$this->deployer = new Deployer($console);
24+
$this->deployer['input'] = $input;
25+
$this->deployer['output'] = $output;
26+
}
27+
28+
public function tearDown(): void
29+
{
30+
unset($this->deployer);
31+
}
32+
33+
public function testImporterIgnoresYamlHiddenKeys(): void
34+
{
35+
$data = <<<EOL
36+
.base: &base
37+
remote_user: foo
38+
labels:
39+
stage: production
40+
41+
hosts:
42+
acceptance:
43+
<<: *base
44+
labels:
45+
stage: acceptance
46+
47+
production:
48+
<<: *base
49+
remote_user: bar
50+
51+
production.beta:
52+
<<: *base
53+
# test.yaml
54+
EOL;
55+
56+
Import::import("data:text/yaml,$data");
57+
self::assertTrue($this->deployer->hosts->has('production'));
58+
self::assertTrue($this->deployer->hosts->has('acceptance'));
59+
self::assertTrue($this->deployer->hosts->has('production.beta'));
60+
self::assertEquals('acceptance', $this->deployer->hosts->get('acceptance')->getLabels()['stage']);
61+
self::assertEquals('production', $this->deployer->hosts->get('production')->getLabels()['stage']);
62+
self::assertEquals('foo', $this->deployer->hosts->get('acceptance')->getRemoteUser());
63+
self::assertEquals('bar', $this->deployer->hosts->get('production')->getRemoteUser());
64+
}
65+
}

0 commit comments

Comments
 (0)