Skip to content

Commit 9c65073

Browse files
Added task hider
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent 188b32c commit 9c65073

15 files changed

Lines changed: 963 additions & 194 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"phive-update": "phive update && git add tools/* phive.xml && git commit -m 'Updated phive dependencies'",
3838
"phpunit": "tools/phpunit.phar -c test/phpunit.dist.xml test/",
3939
"psalm": "tools/psalm --threads=10 --long-progress --no-diff",
40-
"composer-unused": "tools/composer-unused --no-progress --no-interaction --profile",
40+
"composer-unused": "tools/composer-unused --no-progress --no-interaction",
4141
"composer-require-checker": "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": [

phive.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
~ limitations under the License.
1616
-->
1717
<phive xmlns="https://phar.io/phive">
18-
<phar name="phpunit" version="^9.5" installed="9.5.11" location="./tools/phpunit.phar" copy="true"/>
19-
<phar name="psalm" version="^4.18" installed="4.18.1" location="./tools/psalm" copy="true"/>
18+
<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"/>
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"/>
22-
<phar name="infection" version="^0.20" installed="0.20.1" location="./tools/infection" copy="true"/>
22+
<phar name="infection" version="^0.26" installed="0.26.4" location="./tools/infection" copy="true"/>
2323
<phar name="php-cs-fixer" version="^2.19" installed="2.19.3" location="./tools/php-cs-fixer" copy="true"/>
2424
</phive>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\base\taskHider;
19+
20+
use de\codenamephp\deployer\base\taskMatcher\iTaskMatcher;
21+
use Deployer\Deployer;
22+
23+
/**
24+
* Iterates over the task list in the deployer instance and matches them against a task matcher. All matching tasks are hidden
25+
*/
26+
final class ByTaskListAndMatchers implements iTaskHider {
27+
28+
public Deployer $deployer;
29+
30+
public function __construct(public iTaskMatcher $taskMatcher, Deployer $deployer = null) {
31+
$this->deployer = $deployer ?? Deployer::get();
32+
}
33+
34+
public function hide() : void {
35+
foreach($this->deployer->tasks as $task) {
36+
if($this->taskMatcher->matches($task)) $task->hidden(true);
37+
}
38+
}
39+
40+
}

src/taskHider/iTaskHider.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\base\taskHider;
19+
20+
/**
21+
* Interface to hide tasks since Deployer comes with a lot of "built-in" task we don't necessarily want to clutter up our list (e.g. all the provision stuff)
22+
*
23+
* Implementations are supposed to use matcher and set tasks to hidden
24+
*/
25+
interface iTaskHider {
26+
27+
/**
28+
* Hides all tasks that should be hidden
29+
*
30+
* @return void
31+
*/
32+
public function hide() : void;
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\base\taskMatcher;
19+
20+
use Deployer\Task\Task;
21+
22+
/**
23+
* Matches the name of the task exactly (case sensitivity included) with the given task name
24+
*/
25+
final class ByExactTaskName implements iTaskMatcher {
26+
27+
public function __construct(public string $taskName) {}
28+
29+
public function matches(Task $task) : bool {
30+
return $task->getName() === $this->taskName;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\base\taskMatcher;
19+
20+
use Deployer\Task\Task;
21+
22+
/**
23+
* Matches the task name against a regular expression
24+
*/
25+
final class ByRegexTaskName implements iTaskMatcher {
26+
27+
public function __construct(public string $regex) {}
28+
29+
public function matches(Task $task) : bool {
30+
return preg_match($this->regex, (string) $task->getName()) === 1;
31+
}
32+
}

src/taskMatcher/iTaskMatcher.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\base\taskMatcher;
19+
20+
use Deployer\Task\Task;
21+
22+
/**
23+
* Interface to match tasks e.g by name
24+
*/
25+
interface iTaskMatcher {
26+
27+
/**
28+
* Checks if the given task matches the criteria (e.g. matches the name to a regex) and returns true if all criteria are true
29+
*
30+
* @param Task $task The task to match
31+
* @return bool True if the task matches all the criteria, false otherwise
32+
*/
33+
public function matches(Task $task) : bool;
34+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\base\test\taskHider;
19+
20+
use de\codenamephp\deployer\base\taskHider\ByTaskListAndMatchers;
21+
use de\codenamephp\deployer\base\taskMatcher\iTaskMatcher;
22+
use Deployer\Deployer;
23+
use Deployer\Task\Task;
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class ByTaskListAndMatchersTest extends TestCase {
27+
28+
private ByTaskListAndMatchers $sut;
29+
30+
protected function setUp() : void {
31+
parent::setUp();
32+
33+
$taskMatcher = $this->createMock(iTaskMatcher::class);
34+
$deployer = $this->createMock(Deployer::class);
35+
36+
$this->sut = new ByTaskListAndMatchers($taskMatcher, $deployer);
37+
}
38+
39+
public function test__construct() : void {
40+
$taskMatcher = $this->createMock(iTaskMatcher::class);
41+
$deployer = $this->createMock(Deployer::class);
42+
43+
$this->sut = new ByTaskListAndMatchers($taskMatcher, $deployer);
44+
45+
self::assertSame($taskMatcher, $this->sut->taskMatcher);
46+
self::assertSame($deployer, $this->sut->deployer);
47+
}
48+
49+
public function testHide() : void {
50+
$task1 = $this->createMock(Task::class);
51+
$task1->expects(self::once())->method('hidden')->with(true);
52+
$task2 = $this->createMock(Task::class);
53+
$task2->expects(self::never())->method('hidden');
54+
$task3 = $this->createMock(Task::class);
55+
$task3->expects(self::never())->method('hidden');
56+
$task4 = $this->createMock(Task::class);
57+
$task4->expects(self::once())->method('hidden')->with(true);
58+
$task5 = $this->createMock(Task::class);
59+
$task5->expects(self::once())->method('hidden')->with(true);
60+
61+
$this->sut->deployer = $this->createMock(Deployer::class);
62+
$this->sut->deployer->expects(self::once())->method('__get')->with('tasks')->willReturn([$task1, $task2, $task3, $task4, $task5]);
63+
64+
$this->sut->taskMatcher = $this->createMock(iTaskMatcher::class);
65+
$this->sut->taskMatcher
66+
->expects(self::exactly(5))
67+
->method('matches')
68+
->withConsecutive([$task1], [$task2], [$task3], [$task4], [$task5])
69+
->willReturnOnConsecutiveCalls(true, false, false, true, true);
70+
71+
$this->sut->hide();
72+
}
73+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\base\test\taskMatcher;
19+
20+
use de\codenamephp\deployer\base\taskMatcher\ByExactTaskName;
21+
use Deployer\Task\Task;
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class ByExactTaskNameTest extends TestCase {
25+
26+
private ByExactTaskName $sut;
27+
28+
protected function setUp() : void {
29+
parent::setUp();
30+
31+
$this->sut = new ByExactTaskName('');
32+
}
33+
34+
public function testMatches() : void {
35+
$this->sut->taskName = 'some task';
36+
37+
$task = $this->createMock(Task::class);
38+
$task->expects(self::once())->method('getName')->willReturn('some task');
39+
40+
self::assertTrue($this->sut->matches($task));
41+
}
42+
43+
public function testMatches_canReturnFalse_whenTaskNameDoesNotMatch() : void {
44+
$this->sut->taskName = 'some task';
45+
46+
$task = $this->createMock(Task::class);
47+
$task->expects(self::once())->method('getName')->willReturn('some other task');
48+
49+
self::assertFalse($this->sut->matches($task));
50+
}
51+
52+
public function test__construct() : void {
53+
$taskName = 'some task';
54+
55+
$this->sut = new ByExactTaskName($taskName);
56+
57+
self::assertSame($taskName, $this->sut->taskName);
58+
}
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\base\test\taskMatcher;
19+
20+
use de\codenamephp\deployer\base\taskMatcher\ByRegexTaskName;
21+
use Deployer\Task\Task;
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class ByRegexTaskNameTest extends TestCase {
25+
26+
private ByRegexTaskName $sut;
27+
28+
protected function setUp() : void {
29+
parent::setUp();
30+
31+
$this->sut = new ByRegexTaskName('');
32+
}
33+
34+
public function testMatches() : void {
35+
$this->sut->regex = '/match:.*/';
36+
37+
$task1 = $this->createMock(Task::class);
38+
$task1->expects(self::once())->method('getName')->willReturn('match:this');
39+
$task2 = $this->createMock(Task::class);
40+
$task2->expects(self::once())->method('getName')->willReturn('match:that');
41+
$task3 = $this->createMock(Task::class);
42+
$task3->expects(self::once())->method('getName')->willReturn('not:that');
43+
$task4 = $this->createMock(Task::class);
44+
$task4->expects(self::once())->method('getName')->willReturn(null);
45+
46+
self::assertTrue($this->sut->matches($task1));
47+
self::assertTrue($this->sut->matches($task2));
48+
self::assertFalse($this->sut->matches($task3));
49+
self::assertFalse($this->sut->matches($task4));
50+
}
51+
52+
public function test__construct() : void {
53+
$regex = 'some regex';
54+
55+
$this->sut = new ByRegexTaskName($regex);
56+
57+
self::assertSame($regex, $this->sut->regex);
58+
}
59+
}

0 commit comments

Comments
 (0)