Skip to content

Commit 2f00814

Browse files
Made deployer functions and host checks constructor arguments so they can be tested
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent 3999c35 commit 2f00814

7 files changed

Lines changed: 45 additions & 27 deletions

File tree

.idea/runConfigurations/Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
"sort-packages": true
3535
},
3636
"scripts": {
37-
"phive-update": "phive update && git add tools/* phive.xml && git commit -m 'Updated phive dependencies'",
38-
"phpunit": "tools/phpunit.phar -c test/phpunit.dist.xml test/",
39-
"psalm": "tools/psalm --threads=10 --long-progress --no-diff",
40-
"composer-unused": "tools/composer-unused --no-progress --no-interaction",
41-
"composer-require-checker": "tools/composer-require-checker --no-interaction",
37+
"phive-update": "XDEBUG_MODE=off phive update && git add tools/* phive.xml && git commit -m 'Updated phive dependencies'",
38+
"phpunit": "XDEBUG_MODE=coverage tools/phpunit.phar -c test/phpunit.dist.xml test/",
39+
"psalm": "XDEBUG_MODE=off tools/psalm --threads=10 --long-progress --no-diff",
40+
"composer-unused": "XDEBUG_MODE=off tools/composer-unused --no-progress --no-interaction",
41+
"composer-require-checker": "XDEBUG_MODE=off tools/composer-require-checker --no-interaction",
4242
"infection": "XDEBUG_MODE=coverage tools/infection --min-msi=100 --min-covered-msi=100 --threads=4 --no-progress --show-mutations",
4343
"ci-all": [
4444
"@phpunit",

phive.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-->
1717
<phive xmlns="https://phar.io/phive">
1818
<phar name="phpunit" version="^9.5" installed="9.5.13" location="./tools/phpunit.phar" copy="true"/>
19-
<phar name="psalm" version="^4.18" installed="4.20.0" location="./tools/psalm" copy="true"/>
19+
<phar name="psalm" version="^4.20" installed="4.20.0" location="./tools/psalm" copy="true"/>
2020
<phar name="composer-unused" version="^0.7" installed="0.7.12" location="./tools/composer-unused" copy="true"/>
2121
<phar name="composer-require-checker" version="^2.1.0" installed="2.1.0" location="./tools/composer-require-checker" copy="true"/>
2222
<phar name="infection" version="^0.26" installed="0.26.4" location="./tools/infection" copy="true"/>

src/task/media/Pull.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@
3030
*/
3131
final class Pull implements iTask {
3232

33-
public iDownload $deployerFunctions;
34-
3533
/**
3634
* @var array<iTransferable>
3735
*/
3836
private array $transferables;
3937

40-
public function __construct(iTransferable ...$transferables) {
41-
$this->transferables = $transferables;
42-
$this->deployerFunctions = new All();
38+
/**
39+
* @param array<iTransferable> $transferables The transferables to pull
40+
* @param iDownload $deployerFunctions Executes the download
41+
*/
42+
public function __construct(array $transferables, public iDownload $deployerFunctions = new All()) {
43+
$this->setTransferables(...$transferables);
4344
}
4445

4546
/**

src/task/media/Push.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use de\codenamephp\deployer\base\functions\iUpload;
2222
use de\codenamephp\deployer\base\hostCheck\DoNotRunOnProduction;
2323
use de\codenamephp\deployer\base\hostCheck\iHostCheck;
24+
use de\codenamephp\deployer\base\hostCheck\SkippableByOption;
2425
use de\codenamephp\deployer\base\task\iTask;
2526
use de\codenamephp\deployer\base\transferable\iTransferable;
2627
use de\codenamephp\deployer\base\UnsafeOperationException;
@@ -33,19 +34,20 @@
3334
*/
3435
final class Push implements iTask {
3536

36-
public iUpload $deployerFunctions;
37-
38-
public iHostCheck $hostCheck;
39-
4037
/**
4138
* @var array<iTransferable>
4239
*/
4340
private array $transferables;
4441

45-
public function __construct(iTransferable ...$transferables) {
46-
$this->transferables = $transferables;
47-
$this->deployerFunctions = new All();
48-
$this->hostCheck = new DoNotRunOnProduction();
42+
/**
43+
* @param array<iTransferable> $transferables The transferables to push
44+
* @param iUpload $deployerFunctions Executes the upload
45+
* @param iHostCheck $hostCheck Makes sure that the task is not accidentally executed on production
46+
*/
47+
public function __construct(array $transferables,
48+
public iUpload $deployerFunctions = new All(),
49+
public iHostCheck $hostCheck = new SkippableByOption(new DoNotRunOnProduction())) {
50+
$this->setTransferables(...$transferables);
4951
}
5052

5153
/**

test/task/media/PullTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@ final class PullTest extends TestCase {
3030
protected function setUp() : void {
3131
parent::setUp();
3232

33-
$this->sut = new Pull();
33+
$deployerFunctions = $this->createMock(iDownload::class);
34+
35+
$this->sut = new Pull([], $deployerFunctions);
3436
}
3537

3638
public function test__construct() : void {
3739
$transferable1 = $this->createMock(iTransferable::class);
3840
$transferable2 = $this->createMock(iTransferable::class);
39-
$this->sut = new Pull($transferable1, $transferable2);
41+
$deployerFunctions = $this->createMock(iDownload::class);
42+
43+
$this->sut = new Pull([$transferable1, $transferable2], $deployerFunctions);
4044

4145
self::assertSame([$transferable1, $transferable2], $this->sut->getTransferables());
46+
self::assertSame($deployerFunctions, $this->sut->deployerFunctions);
47+
}
48+
49+
public function test__construct_withoutOptionalParameters() : void {
50+
$this->sut = new Pull([]);
51+
52+
self::assertSame([], $this->sut->getTransferables());
4253
self::assertInstanceOf(All::class, $this->sut->deployerFunctions);
4354
}
4455

test/task/media/PushTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
namespace de\codenamephp\deployer\base\test\task\media;
1919

20-
use de\codenamephp\deployer\base\functions\All;
2120
use de\codenamephp\deployer\base\functions\iUpload;
22-
use de\codenamephp\deployer\base\hostCheck\DoNotRunOnProduction;
2321
use de\codenamephp\deployer\base\hostCheck\iHostCheck;
2422
use de\codenamephp\deployer\base\task\media\Push;
2523
use de\codenamephp\deployer\base\transferable\iTransferable;
@@ -32,18 +30,23 @@ final class PushTest extends TestCase {
3230
protected function setUp() : void {
3331
parent::setUp();
3432

35-
$this->sut = new Push();
33+
$deployerFunctions = $this->createMock(iUpload::class);
34+
$hostCheck = $this->createMock(iHostCheck::class);
35+
36+
$this->sut = new Push([], $deployerFunctions, $hostCheck);
3637
}
3738

3839
public function test__construct() : void {
3940
$transferables1 = $this->createMock(iTransferable::class);
4041
$transferables2 = $this->createMock(iTransferable::class);
42+
$deployerFunctions = $this->createMock(iUpload::class);
43+
$hostCheck = $this->createMock(iHostCheck::class);
4144

42-
$this->sut = new Push($transferables1, $transferables2);
45+
$this->sut = new Push([$transferables1, $transferables2], $deployerFunctions, $hostCheck);
4346

4447
self::assertSame([$transferables1, $transferables2], $this->sut->getTransferables());
45-
self::assertInstanceOf(All::class, $this->sut->deployerFunctions);
46-
self::assertInstanceOf(DoNotRunOnProduction::class, $this->sut->hostCheck);
48+
self::assertSame($deployerFunctions, $this->sut->deployerFunctions);
49+
self::assertSame($hostCheck, $this->sut->hostCheck);
4750
}
4851

4952
public function test__invoke() : void {

0 commit comments

Comments
 (0)