Skip to content

Commit a0bfde8

Browse files
Merge pull request #15 from codenamephp/feature/7-noop-task
Added NoOp task
2 parents 76a6b48 + 083b6c1 commit a0bfde8

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

src/task/NoOp.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 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\task;
19+
20+
use Closure;
21+
use de\codenamephp\deployer\base\functions\All;
22+
use de\codenamephp\deployer\base\functions\iInfo;
23+
24+
/**
25+
* This task does nothing. It is used to create a task that does nothing but is not null.
26+
*/
27+
final class NoOp implements iTaskWithName, iTaskWithDescription {
28+
29+
/**
30+
* Builds the message that is printed when the task is executed and the printMessage flag is set to true.
31+
*
32+
* Gets the name of the task as argument and has to return the message as string.
33+
*
34+
* @var Closure(string $name) : string
35+
*/
36+
public Closure $getMessage;
37+
38+
public function __construct(public readonly string $name, public readonly bool $printMessage = false, public readonly iInfo $logging = new All()) {
39+
$this->getMessage = static fn(string $name) : string => "No operation task '{$name}' was executed";
40+
}
41+
42+
public function __invoke() : void {
43+
!$this->printMessage ?: $this->logging->info(($this->getMessage)($this->name));
44+
}
45+
46+
public function getDescription() : string {
47+
return 'This task has been disabled.';
48+
}
49+
50+
public function getName() : string {
51+
return $this->name;
52+
}
53+
}

test/task/NoOpTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 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\task;
19+
20+
use de\codenamephp\deployer\base\functions\All;
21+
use de\codenamephp\deployer\base\functions\iInfo;
22+
use de\codenamephp\deployer\base\task\NoOp;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class NoOpTest extends TestCase {
26+
27+
public function test__invoke() : void {
28+
$logging = $this->createMock(iInfo::class);
29+
$logging->expects(self::once())->method('info')->with("No operation task 'test' was executed");
30+
31+
$task = new NoOp('test', true, $logging);
32+
$task();
33+
}
34+
35+
public function test__invoke_whenPrintMessageIsFalse() : void {
36+
$logging = $this->createMock(iInfo::class);
37+
$logging->expects(self::never())->method('info');
38+
39+
$task = new NoOp('test', false, $logging);
40+
$task();
41+
}
42+
43+
public function testGetName() : void {
44+
$this->assertSame('test', (new NoOp('test'))->getName());
45+
}
46+
47+
public function testGetDescription() : void {
48+
$this->assertSame('This task has been disabled.', (new NoOp('test'))->getDescription());
49+
}
50+
51+
public function test__construct() : void {
52+
$task = new NoOp('test');
53+
54+
self::assertSame('test', $task->getName());
55+
self::assertFalse($task->printMessage);
56+
self::assertEquals(new All(), $task->logging);
57+
self::assertEquals(static fn(string $name) : string => "No operation task '{$name}' was executed", $task->getMessage);
58+
}
59+
60+
public function test__construct_withOptionalArguments() : void {
61+
$logging = $this->createMock(iInfo::class);
62+
63+
$task = new NoOp('test', true, $logging);
64+
65+
self::assertSame('test', $task->getName());
66+
self::assertTrue($task->printMessage);
67+
self::assertSame($logging, $task->logging);
68+
self::assertEquals(static fn(string $name) : string => "No operation task '{$name}' was executed", $task->getMessage);
69+
}
70+
71+
72+
}

0 commit comments

Comments
 (0)