Skip to content

Commit bc13be4

Browse files
Added command and removed SimpleArray version since the new command is just better
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent eda8d1b commit bc13be4

4 files changed

Lines changed: 123 additions & 98 deletions

File tree

src/Command.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command;
19+
20+
use de\codenamephp\deployer\command\runConfiguration\iRunConfiguration;
21+
use de\codenamephp\deployer\command\runConfiguration\SimpleContainer;
22+
23+
/**
24+
* Simple command where the binary is mandatory and arguments, env vars, sudo and a run configuration can be set
25+
*/
26+
final class Command implements iCommand {
27+
28+
/**
29+
* @param string $binary The binary to be executed, e.g. ls or composer
30+
* @param iRunConfiguration $runConfiguration The run configuration to use when running the command
31+
* @param array<string> $arguments Arguments to use when running the command, e.g. '-l', 'install', '--composer', '--name=value'
32+
* @param array<string,string> $envVars Env vars to set when running the command. They will be prepended to the binary with the key as name
33+
* @param bool $sudo Flag to run the command with sudo
34+
*/
35+
public function __construct(
36+
public string $binary,
37+
public iRunConfiguration $runConfiguration = new SimpleContainer(),
38+
public array $arguments = [],
39+
public array $envVars = [],
40+
public bool $sudo = false,
41+
) {}
42+
43+
public function getRunConfiguration() : iRunConfiguration {
44+
return $this->runConfiguration;
45+
}
46+
47+
public function __toString() : string {
48+
return implode(' ', array_filter([
49+
...array_map(static fn(string $name, string $value) : string => sprintf('%s=%s;', $name, $value), array_keys($this->envVars), $this->envVars),
50+
$this->sudo ? 'sudo' : '',
51+
$this->binary,
52+
...$this->arguments,
53+
]));
54+
}
55+
56+
}

src/SimpleArray.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/CommandTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\test;
19+
20+
use de\codenamephp\deployer\command\Command;
21+
use de\codenamephp\deployer\command\runConfiguration\iRunConfiguration;
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class CommandTest extends TestCase {
25+
26+
private Command $sut;
27+
28+
protected function setUp() : void {
29+
parent::setUp();
30+
31+
$this->sut = new Command('');
32+
}
33+
34+
public function test__construct() : void {
35+
$binary = 'some binary';
36+
$runConfiguration = $this->createMock(iRunConfiguration::class);
37+
$arguments = ['test', '--test'];
38+
$envVars = ['some' => 'env'];
39+
40+
$this->sut = new Command($binary, $runConfiguration, $arguments, $envVars, true);
41+
42+
self::assertSame($binary, $this->sut->binary);
43+
self::assertSame($runConfiguration, $this->sut->runConfiguration);
44+
self::assertSame($arguments, $this->sut->arguments);
45+
self::assertSame($envVars, $this->sut->envVars);
46+
self::assertTrue($this->sut->sudo);
47+
}
48+
49+
public function test__toString() : void {
50+
$this->sut->binary = 'some binary';
51+
$this->sut->arguments = ['bla' => 'test', '--test'];
52+
$this->sut->envVars = ['some' => 'env', 'more' => 'vars'];
53+
$this->sut->sudo = true;
54+
55+
self::assertEquals('some=env; more=vars; sudo some binary test --test', $this->sut->__toString());
56+
}
57+
58+
public function test__toString_withMinimalProperties() : void {
59+
$this->sut->binary = 'some binary';
60+
61+
self::assertEquals('some binary', $this->sut->__toString());
62+
}
63+
64+
public function testGetRunConfiguration() : void {
65+
self::assertSame($this->sut->runConfiguration, $this->sut->getRunConfiguration());
66+
}
67+
}

test/SimpleArrayTest.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)