Skip to content

Commit b2cc78f

Browse files
Merge pull request #16 from codenamephp/feature/addOutput
Added ability to show output
2 parents f17ab1d + 8bfa5cc commit b2cc78f

File tree

7 files changed

+121
-6
lines changed

7 files changed

+121
-6
lines changed

.idea/copyright/Apache2.xml

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

.idea/copyright/profiles_settings.xml

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

src/Task/AbstractCrontabCommand.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
<?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+
*/
217

318
namespace de\codenamephp\deployer\crontab\Task;
419

20+
use de\codenamephp\deployer\base\functions\All;
21+
use de\codenamephp\deployer\base\functions\iWriteln;
522
use de\codenamephp\deployer\base\task\iTask;
623
use de\codenamephp\deployer\command\runner\iRunner;
724
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
@@ -19,7 +36,8 @@ abstract class AbstractCrontabCommand implements iTask {
1936
public function __construct(
2037
public readonly ?string $user = null,
2138
public readonly CrontabCommandFactoryInterface $crontabCommandFactory = new WithBinaryFromDeployer(),
22-
public readonly iRunner $commandRunner = new WithDeployerFunctions()
39+
public readonly iRunner $commandRunner = new WithDeployerFunctions(),
40+
public readonly iWriteln $writeln = new All()
2341
) {}
2442

2543
/**
@@ -34,6 +52,7 @@ final public function getOptionsWithUser() : array {
3452
}
3553

3654
final public function __invoke() : void {
37-
$this->commandRunner->run($this->crontabCommandFactory->build($this->getOptionsWithUser()));
55+
$output = $this->commandRunner->run($this->crontabCommandFactory->build($this->getOptionsWithUser()));
56+
!$this instanceof HasOutputInteface ?: $this->writeln->writeln(PHP_EOL . $output);
3857
}
3958
}

src/Task/HasOutputInteface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\crontab\Task;
19+
20+
/**
21+
* Interface for tasks that have output that should be printed to the console. This is just a marking interface to be able to filter tasks that have output
22+
*/
23+
interface HasOutputInteface { }

src/Task/Show.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?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+
*/
217

318
namespace de\codenamephp\deployer\crontab\Task;
419

@@ -10,7 +25,7 @@
1025
*
1126
* @psalm-api
1227
*/
13-
final class Show extends AbstractCrontabCommand implements iTaskWithName, iTaskWithDescription, HasOptionsInterface {
28+
final class Show extends AbstractCrontabCommand implements iTaskWithName, iTaskWithDescription, HasOptionsInterface, HasOutputInteface {
1429

1530
public const NAME = 'crontab:show';
1631

test/Task/AbstractCrontabCommandTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
<?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+
*/
217

318
namespace de\codenamephp\deployer\crontab\test\Task;
419

20+
use de\codenamephp\deployer\base\functions\iWriteln;
521
use de\codenamephp\deployer\command\iCommand;
622
use de\codenamephp\deployer\command\runner\iRunner;
723
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
824
use de\codenamephp\deployer\crontab\Command\CrontabCommandFactoryInterface;
925
use de\codenamephp\deployer\crontab\Command\WithBinaryFromDeployer;
1026
use de\codenamephp\deployer\crontab\Task\AbstractCrontabCommand;
1127
use de\codenamephp\deployer\crontab\Task\HasOptionsInterface;
28+
use de\codenamephp\deployer\crontab\Task\HasOutputInteface;
1229
use Mockery;
1330
use PHPUnit\Framework\TestCase;
1431

@@ -56,7 +73,6 @@ public function test__construct_withOptionalArguments() : void {
5673
}
5774

5875
public function test__invoke() : void {
59-
$user = 'some user';
6076
$command = $this->createMock(iCommand::class);
6177

6278
$crontabCommandFactory = $this->createMock(CrontabCommandFactoryInterface::class);
@@ -65,7 +81,21 @@ public function test__invoke() : void {
6581
$commandRunner = $this->createMock(iRunner::class);
6682
$commandRunner->expects(self::once())->method('run')->with($command);
6783

68-
$sut = $this->getMockForAbstractClass(AbstractCrontabCommand::class, [$user, $crontabCommandFactory, $commandRunner]);
84+
$writeln = $this->createMock(iWriteln::class);
85+
$writeln->expects(self::never())->method('writeln');
86+
87+
$sut = $this->getMockForAbstractClass(AbstractCrontabCommand::class, ['some user', $crontabCommandFactory, $commandRunner, $writeln]);
88+
$sut->__invoke();
89+
}
90+
91+
public function test__invoke_canOutput() : void {
92+
$commandRunner = $this->createMock(iRunner::class);
93+
$commandRunner->expects(self::once())->method('run')->willReturn('some output');
94+
95+
$writeln = $this->createMock(iWriteln::class);
96+
$writeln->expects(self::once())->method('writeln')->with(PHP_EOL . 'some output');
97+
98+
$sut = Mockery::mock(AbstractCrontabCommand::class, HasOutputInteface::class, ['some user', $this->createMock(CrontabCommandFactoryInterface::class), $commandRunner, $writeln])->makePartial();
6999
$sut->__invoke();
70100
}
71101
}

test/Task/ShowTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
<?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+
*/
217

318
namespace de\codenamephp\deployer\crontab\test\Task;
419

20+
use de\codenamephp\deployer\base\functions\iWriteln;
521
use de\codenamephp\deployer\command\runner\iRunner;
622
use de\codenamephp\deployer\crontab\Command\CrontabCommandFactoryInterface;
723
use de\codenamephp\deployer\crontab\Task\Show;
@@ -25,6 +41,9 @@ public function test__invoke() : void {
2541
$crontabCommandFactory = $this->createMock(CrontabCommandFactoryInterface::class);
2642
$crontabCommandFactory->expects(self::once())->method('build')->with(['-l']);
2743

28-
(new Show(crontabCommandFactory: $crontabCommandFactory, commandRunner: $this->createMock(iRunner::class)))->__invoke();
44+
$writeln = $this->createMock(iWriteln::class);
45+
$writeln->expects(self::once())->method('writeln');
46+
47+
(new Show(crontabCommandFactory: $crontabCommandFactory, commandRunner: $this->createMock(iRunner::class), writeln: $writeln))->__invoke();
2948
}
3049
}

0 commit comments

Comments
 (0)