From 06c879246233e5f858d2d836f7516b2db02d3588 Mon Sep 17 00:00:00 2001 From: Bastian Schwarz Date: Sun, 19 Mar 2023 13:42:55 +0100 Subject: [PATCH 1/3] Added new interface to mark tasks to have output and have the abstract command output the command output if the interface is detected --- src/Task/AbstractCrontabCommand.php | 23 ++++++++++++++-- src/Task/HasOutputInteface.php | 23 ++++++++++++++++ test/Task/AbstractCrontabCommandTest.php | 34 ++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/Task/HasOutputInteface.php diff --git a/src/Task/AbstractCrontabCommand.php b/src/Task/AbstractCrontabCommand.php index 307284b..5744afe 100644 --- a/src/Task/AbstractCrontabCommand.php +++ b/src/Task/AbstractCrontabCommand.php @@ -1,7 +1,24 @@ . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ namespace de\codenamephp\deployer\crontab\Task; +use de\codenamephp\deployer\base\functions\All; +use de\codenamephp\deployer\base\functions\iWriteln; use de\codenamephp\deployer\base\task\iTask; use de\codenamephp\deployer\command\runner\iRunner; use de\codenamephp\deployer\command\runner\WithDeployerFunctions; @@ -19,7 +36,8 @@ abstract class AbstractCrontabCommand implements iTask { public function __construct( public readonly ?string $user = null, public readonly CrontabCommandFactoryInterface $crontabCommandFactory = new WithBinaryFromDeployer(), - public readonly iRunner $commandRunner = new WithDeployerFunctions() + public readonly iRunner $commandRunner = new WithDeployerFunctions(), + public readonly iWriteln $writeln = new All() ) {} /** @@ -34,6 +52,7 @@ final public function getOptionsWithUser() : array { } final public function __invoke() : void { - $this->commandRunner->run($this->crontabCommandFactory->build($this->getOptionsWithUser())); + $output = $this->commandRunner->run($this->crontabCommandFactory->build($this->getOptionsWithUser())); + !$this instanceof HasOutputInteface ?: $this->writeln->writeln(PHP_EOL . $output); } } \ No newline at end of file diff --git a/src/Task/HasOutputInteface.php b/src/Task/HasOutputInteface.php new file mode 100644 index 0000000..0c1ca10 --- /dev/null +++ b/src/Task/HasOutputInteface.php @@ -0,0 +1,23 @@ +. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace de\codenamephp\deployer\crontab\Task; + +/** + * 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 + */ +interface HasOutputInteface { } \ No newline at end of file diff --git a/test/Task/AbstractCrontabCommandTest.php b/test/Task/AbstractCrontabCommandTest.php index 70e7b25..096c0f5 100644 --- a/test/Task/AbstractCrontabCommandTest.php +++ b/test/Task/AbstractCrontabCommandTest.php @@ -1,7 +1,23 @@ . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ namespace de\codenamephp\deployer\crontab\test\Task; +use de\codenamephp\deployer\base\functions\iWriteln; use de\codenamephp\deployer\command\iCommand; use de\codenamephp\deployer\command\runner\iRunner; use de\codenamephp\deployer\command\runner\WithDeployerFunctions; @@ -9,6 +25,7 @@ use de\codenamephp\deployer\crontab\Command\WithBinaryFromDeployer; use de\codenamephp\deployer\crontab\Task\AbstractCrontabCommand; use de\codenamephp\deployer\crontab\Task\HasOptionsInterface; +use de\codenamephp\deployer\crontab\Task\HasOutputInteface; use Mockery; use PHPUnit\Framework\TestCase; @@ -56,7 +73,6 @@ public function test__construct_withOptionalArguments() : void { } public function test__invoke() : void { - $user = 'some user'; $command = $this->createMock(iCommand::class); $crontabCommandFactory = $this->createMock(CrontabCommandFactoryInterface::class); @@ -65,7 +81,21 @@ public function test__invoke() : void { $commandRunner = $this->createMock(iRunner::class); $commandRunner->expects(self::once())->method('run')->with($command); - $sut = $this->getMockForAbstractClass(AbstractCrontabCommand::class, [$user, $crontabCommandFactory, $commandRunner]); + $writeln = $this->createMock(iWriteln::class); + $writeln->expects(self::never())->method('writeln'); + + $sut = $this->getMockForAbstractClass(AbstractCrontabCommand::class, ['some user', $crontabCommandFactory, $commandRunner, $writeln]); + $sut->__invoke(); + } + + public function test__invoke_canOutput() : void { + $commandRunner = $this->createMock(iRunner::class); + $commandRunner->expects(self::once())->method('run')->willReturn('some output'); + + $writeln = $this->createMock(iWriteln::class); + $writeln->expects(self::once())->method('writeln')->with(PHP_EOL . 'some output'); + + $sut = Mockery::mock(AbstractCrontabCommand::class, HasOutputInteface::class, ['some user', $this->createMock(CrontabCommandFactoryInterface::class), $commandRunner, $writeln])->makePartial(); $sut->__invoke(); } } From 8a7b1f24700323ec59a250a111ef522cc7dc5160 Mon Sep 17 00:00:00 2001 From: Bastian Schwarz Date: Sun, 19 Mar 2023 13:43:10 +0100 Subject: [PATCH 2/3] Added copyright profile to phpstorm --- .idea/copyright/Apache2.xml | 6 ++++++ .idea/copyright/profiles_settings.xml | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 .idea/copyright/Apache2.xml create mode 100644 .idea/copyright/profiles_settings.xml diff --git a/.idea/copyright/Apache2.xml b/.idea/copyright/Apache2.xml new file mode 100644 index 0000000..ddf769e --- /dev/null +++ b/.idea/copyright/Apache2.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..53a32f1 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file From 8bfa5ccb55bc93a940338ddf5220ce848fb5b466 Mon Sep 17 00:00:00 2001 From: Bastian Schwarz Date: Sun, 19 Mar 2023 13:46:39 +0100 Subject: [PATCH 3/3] Added output interface to show task --- src/Task/Show.php | 17 ++++++++++++++++- test/Task/ShowTest.php | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Task/Show.php b/src/Task/Show.php index cdd2125..0143f2f 100644 --- a/src/Task/Show.php +++ b/src/Task/Show.php @@ -1,4 +1,19 @@ . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ namespace de\codenamephp\deployer\crontab\Task; @@ -10,7 +25,7 @@ * * @psalm-api */ -final class Show extends AbstractCrontabCommand implements iTaskWithName, iTaskWithDescription, HasOptionsInterface { +final class Show extends AbstractCrontabCommand implements iTaskWithName, iTaskWithDescription, HasOptionsInterface, HasOutputInteface { public const NAME = 'crontab:show'; diff --git a/test/Task/ShowTest.php b/test/Task/ShowTest.php index 9ec54ea..94a122e 100644 --- a/test/Task/ShowTest.php +++ b/test/Task/ShowTest.php @@ -1,7 +1,23 @@ . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ namespace de\codenamephp\deployer\crontab\test\Task; +use de\codenamephp\deployer\base\functions\iWriteln; use de\codenamephp\deployer\command\runner\iRunner; use de\codenamephp\deployer\crontab\Command\CrontabCommandFactoryInterface; use de\codenamephp\deployer\crontab\Task\Show; @@ -25,6 +41,9 @@ public function test__invoke() : void { $crontabCommandFactory = $this->createMock(CrontabCommandFactoryInterface::class); $crontabCommandFactory->expects(self::once())->method('build')->with(['-l']); - (new Show(crontabCommandFactory: $crontabCommandFactory, commandRunner: $this->createMock(iRunner::class)))->__invoke(); + $writeln = $this->createMock(iWriteln::class); + $writeln->expects(self::once())->method('writeln'); + + (new Show(crontabCommandFactory: $crontabCommandFactory, commandRunner: $this->createMock(iRunner::class), writeln: $writeln))->__invoke(); } }