Skip to content

Commit

Permalink
Merge pull request #16 from codenamephp/feature/addOutput
Browse files Browse the repository at this point in the history
Added ability to show output
  • Loading branch information
bastianschwarz committed Mar 19, 2023
2 parents f17ab1d + 8bfa5cc commit b2cc78f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .idea/copyright/Apache2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions src/Task/AbstractCrontabCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* 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;
Expand All @@ -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()
) {}

/**
Expand 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);
}
}
23 changes: 23 additions & 0 deletions src/Task/HasOutputInteface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* 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 { }
17 changes: 16 additions & 1 deletion src/Task/Show.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* 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;

Expand All @@ -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';

Expand Down
34 changes: 32 additions & 2 deletions test/Task/AbstractCrontabCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* 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;
use de\codenamephp\deployer\crontab\Command\CrontabCommandFactoryInterface;
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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
21 changes: 20 additions & 1 deletion test/Task/ShowTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* 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;
Expand All @@ -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();
}
}

0 comments on commit b2cc78f

Please sign in to comment.