Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F7722461
D1533.id3913.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
D1533.id3913.diff
View Options
Index: .gitignore
===================================================================
--- /dev/null
+++ .gitignore
@@ -0,0 +1,2 @@
+vendor/
+composer.lock
Index: composer.json
===================================================================
--- /dev/null
+++ composer.json
@@ -0,0 +1,22 @@
+{
+ "name": "keruald/commands",
+ "description": "Classes to build CLI commands",
+ "type": "library",
+ "license": "BSD-2-Clause",
+ "authors": [
+ {
+ "name": "Sébastien Santoro",
+ "email": "dereckson@espace-win.org"
+ }
+ ],
+ "autoload": {
+ "psr-4": {
+ "Keruald\\Commands\\": "src/",
+ "Keruald\\Commands\\Tests\\": "tests/"
+ }
+ },
+ "require": {},
+ "require-dev": {
+ "phpunit/phpunit": "^7"
+ }
+}
Index: src/Command.php
===================================================================
--- /dev/null
+++ src/Command.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Keruald\Commands;
+
+use Keruald\Commands\Display\Display;
+use Keruald\Commands\Display\OutputDisplay;
+
+abstract class Command {
+
+ /**
+ * @var int
+ */
+ private $argc;
+
+ /**
+ * @var array
+ */
+ private $argv;
+
+ /**
+ * @var \Keruald\Commands\Display\Display
+ */
+ protected $display;
+
+ public function __construct (
+ int $argc,
+ array $argv,
+ Display $display = null
+ ) {
+ $this->argc = $argc;
+ $this->argv = $argv;
+
+ if ($display === null) {
+ $display = self::getDefaultDisplay();
+ }
+ $this->display = $display;
+ }
+
+ public static function run (int $argc, array $argv) : int {
+ $command = new static($argc, $argv);
+
+ return $command->main();
+ }
+
+ ///
+ /// Getters and setters
+ ///
+
+ public function getArgc () : int {
+ return $this->argc;
+ }
+
+ public function setArgc (int $argc) : Command {
+ $this->argc = $argc;
+
+ return $this;
+ }
+
+ public function getArgv () : array {
+ return $this->argv;
+ }
+
+
+ public function setArgv (int $argv) : Command {
+ $this->argv = $argv;
+
+ return $this;
+ }
+
+ public function getCommandName () : string {
+ return $this->argv[0] ?? "";
+ }
+
+ public function getDisplay () : Display {
+ return $this->display;
+ }
+
+ public function setDisplay (Display $display) : Command {
+ $this->display = $display;
+
+ return $this;
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ private static function getDefaultDisplay () : Display {
+ return new OutputDisplay();
+ }
+
+ ///
+ /// Methods to implement
+ ///
+
+ public abstract function main () : int;
+
+}
Index: src/Display/ArrayDisplay.php
===================================================================
--- /dev/null
+++ src/Display/ArrayDisplay.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Keruald\Commands\Display;
+
+/**
+ * Class ArrayDisplay
+ *
+ * Intended to ease unit tests with a display capturing output in arrays.
+ *
+ * @package Keruald\Commands\Display
+ */
+class ArrayDisplay extends Display {
+
+ /**
+ * @var array
+ */
+ private $out = [];
+
+ /**
+ * @var array
+ */
+ private $error = [];
+
+ ///
+ /// Implement Display
+ ///
+
+ public function out (string $message) : void {
+ $this->out[] = $message;
+ }
+
+ public function error (string $message) : void {
+ $this->error[] = $message;
+ }
+
+ ///
+ /// Getters
+ ///
+
+ public function getOut () : array {
+ return $this->out;
+ }
+
+ public function getError () : array {
+ return $this->error;
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ public function clearOut () : ArrayDisplay {
+ $this->out = [];
+
+ return $this;
+ }
+
+ public function clearError () : ArrayDisplay {
+ $this->error = [];
+
+ return $this;
+ }
+
+ public function countOut () : int {
+ return count($this->out);
+ }
+
+ public function countError () : int {
+ return count($this->error);
+ }
+
+}
Index: src/Display/Display.php
===================================================================
--- /dev/null
+++ src/Display/Display.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Keruald\Commands\Display;
+
+abstract class Display {
+
+ abstract function out (string $message) : void;
+ abstract function error (string $message) : void;
+
+}
Index: src/Display/OutputDisplay.php
===================================================================
--- /dev/null
+++ src/Display/OutputDisplay.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace Keruald\Commands\Display;
+
+class OutputDisplay extends Display {
+
+ public function out (string $message) : void {
+ echo $message, "\n";
+ }
+
+ public function error (string $message) : void {
+ fwrite(STDERR, $message . "\n");
+ }
+
+}
Index: src/ExitCode.php
===================================================================
--- /dev/null
+++ src/ExitCode.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Keruald\Commands;
+
+class ExitCode {
+
+ public const SUCCESS = 0;
+ public const FAILURE = 1;
+ public const ILLEGAL_OPTION = 64;
+
+}
Index: tests/CommandTest.php
===================================================================
--- /dev/null
+++ tests/CommandTest.php
@@ -0,0 +1,57 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\Commands\Tests;
+
+use Keruald\Commands\Command;
+use Keruald\Commands\Display\ArrayDisplay;
+use Keruald\Commands\ExitCode;
+use PHPUnit\Framework\TestCase;
+
+class CommandTest extends TestCase {
+
+ /**
+ * @var Command
+ */
+ private $command;
+
+ /**
+ * @var ArrayDisplay
+ */
+ private $display;
+
+
+ public function setUp () {
+ $this->display = new ArrayDisplay();
+ $this->command = new SunsetCommand(1, ["sunset"], $this->display);
+ }
+
+ public function testGetArgc () {
+ $this->assertEquals(1, $this->command->getArgc());
+ }
+
+ public function testGetArgv () {
+ $this->assertEquals(["sunset"], $this->command->getArgv());
+ }
+
+ public function testGetCommandName() {
+ $this->assertEquals("sunset", $this->command->getCommandName());
+ }
+
+ public function testDisplayBeforeRun() {
+ $this->assertEmpty($this->display->getOut());
+ $this->assertEmpty($this->display->getError());
+ }
+
+ public function testDisplayAfterRun() {
+ $this->command->main();
+ $this->assertEquals(1, $this->display->countOut());
+ $this->assertEquals(0, $this->display->countError());
+ }
+
+ public function testReturnCode () {
+ $this->assertEquals(ExitCode::SUCCESS, $this->command->main());
+ }
+
+}
+
Index: tests/Display/ArrayDisplayTest.php
===================================================================
--- /dev/null
+++ tests/Display/ArrayDisplayTest.php
@@ -0,0 +1,65 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\Commands\Tests\Display;
+
+use Keruald\Commands\Display\ArrayDisplay;
+use PHPUnit\Framework\TestCase;
+
+class ArrayDisplayTest extends TestCase {
+
+ /**
+ * @var ArrayDisplay
+ */
+ private $display;
+
+ public function setUp () {
+ $this->display = new ArrayDisplay;
+ }
+
+ public function testOut () {
+ $this->display->out("Hello world!");
+
+ $this->assertEquals(["Hello world!"], $this->display->getOut());
+ $this->assertEquals([], $this->display->getError());
+
+ $this->assertEquals(1, $this->display->countOut());
+ $this->assertEquals(0, $this->display->countError());
+ }
+
+ public function testClearOut () {
+ $this->display->out("Lorem");
+ $this->display->out("Ipsum");
+ $this->display->out("Dolor");
+ $this->display->clearOut();
+
+ $this->assertEquals(0, $this->display->countOut());
+ }
+
+ public function testClearError () {
+ $this->display->error("Lorem");
+ $this->display->error("Ipsum");
+ $this->display->error("Dolor");
+ $this->display->clearError();
+
+ $this->assertEquals(0, $this->display->countError());
+ }
+
+ public function testCountOut () {
+ $this->display->out("Lorem");
+ $this->display->out("Ipsum");
+ $this->display->out("Dolor");
+
+ $this->assertEquals(3, $this->display->countOut());
+ }
+
+ public function testCountError () {
+ $this->display->error("Lorem");
+ $this->display->error("Ipsum");
+ $this->display->error("Dolor");
+
+ $this->assertEquals(3, $this->display->countError());
+ }
+
+
+}
Index: tests/Display/OutputDisplayTest.php
===================================================================
--- /dev/null
+++ tests/Display/OutputDisplayTest.php
@@ -0,0 +1,25 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\Commands\Tests\Display;
+
+use Keruald\Commands\Display\OutputDisplay;
+use PHPUnit\Framework\TestCase;
+
+class OutputDisplayTest extends TestCase {
+
+ /**
+ * @var OutputDisplay
+ */
+ private $display;
+
+ public function setUp () {
+ $this->display = new OutputDisplay;
+ }
+
+ public function testOut () {
+ $this->expectOutputString("Hello world!\n");
+ $this->display->out("Hello world!");
+ }
+
+}
Index: tests/HelloWorldCommand.php
===================================================================
--- /dev/null
+++ tests/HelloWorldCommand.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Keruald\Commands\Tests;
+
+use Keruald\Commands\Command;
+use Keruald\Commands\ExitCode;
+
+class HelloWorldCommand extends Command {
+
+ public function main () : int {
+ $this->display->out("Hello world!");
+
+ return ExitCode::SUCCESS;
+ }
+
+}
Index: tests/SunsetCommand.php
===================================================================
--- /dev/null
+++ tests/SunsetCommand.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Keruald\Commands\Tests;
+
+use Keruald\Commands\Command;
+use Keruald\Commands\ExitCode;
+
+class SunsetCommand extends Command {
+
+ public function main () : int {
+ $this->display->out(self::computeTodaySunsetTime());
+
+ return ExitCode::SUCCESS;
+ }
+
+ private static function computeTodaySunsetTime () {
+ return date_sunset(time());
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, May 2, 17:08 (18 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2625321
Default Alt Text
D1533.id3913.diff (10 KB)
Attached To
Mode
D1533: Create simple commands with argc, argv and display
Attached
Detach File
Event Timeline
Log In to Comment