From a11de7b7599f9dfdcc8caf85e665c300c3e5c9c1 Mon Sep 17 00:00:00 2001 From: girorme Date: Sun, 22 Sep 2019 19:41:39 -0300 Subject: [PATCH 1/4] Add phpunit and refactor Util to improve reusability and test --- .gitignore | 2 ++ composer.json | 17 ++++++++++-- phpunit.xml | 29 +++++++++++++++++++ src/Os.php | 20 ++++++++++++++ src/Util.php | 18 ++++++------ tests/src/UtilTest.php | 63 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 phpunit.xml create mode 100644 src/Os.php create mode 100644 tests/src/UtilTest.php diff --git a/.gitignore b/.gitignore index 3a9875b..103e692 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /vendor/ composer.lock +.phpunit.result.* +build/ diff --git a/composer.json b/composer.json index 3aa20f4..a164760 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,13 @@ { "autoload": { - "psr-4": {"SuperGiggle\\": "src/"} + "psr-4": { + "SuperGiggle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "SuperGiggle\\": "tests/src/" + } }, "name": "roger-sei/super-giggle", "description": "SuperGiggle checks the coding standards for a specific commit change", @@ -11,5 +18,11 @@ "license": "MIT", "bin": [ "bin/super-giggle" - ] + ], + "require-dev": { + "phpunit/phpunit": "^8" + }, + "scripts": { + "test": "phpunit --testdox --colors=always" + } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ff5edb5 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,29 @@ + + + + + tests + + + + + src/ + + + + + + + + + + diff --git a/src/Os.php b/src/Os.php new file mode 100644 index 0000000..9b4a55a --- /dev/null +++ b/src/Os.php @@ -0,0 +1,20 @@ +os->isWindows() === true) { $path = str_replace('\\', '/', __DIR__ . '/../vendor/bin/phpcs.bat'); } @@ -98,13 +100,13 @@ public static function getPhpCsBinary(): string /** - * Checks the OS and returns true if Windows system is found. + * Set current operating system * - * @return boolean + * @return void */ - public static function isWindows(): bool + public function setOs($os): void { - return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'); + $this->os = $os; } diff --git a/tests/src/UtilTest.php b/tests/src/UtilTest.php new file mode 100644 index 0000000..1210bce --- /dev/null +++ b/tests/src/UtilTest.php @@ -0,0 +1,63 @@ +assertInstanceOf(Util::class, $util); + } + + + /** + * Test if directory is ok to windows platform + * + * @test + * + * @return void + */ + public function phpCsBinaryOnWindows() + { + $os = $this->createMock(Os::class); + $os->method('isWindows') + ->willReturn(true); + + $util = new Util(); + $util->setOs($os); + + $this->assertStringContainsString('/../vendor/bin/phpcs.bat', $util->getPhpCsBinary()); + } + + + /** + * Test if directory is ok to linux platform + * + * @test + * + * @return void + */ + public function phpCsBinaryOnLinux() + { + $os = $this->createMock(Os::class); + + $util = new Util(); + $util->setOs($os); + + $this->assertStringContainsString('/../vendor/bin/phpcs', $util->getPhpCsBinary()); + } + + +} From 5e11aeeaa8044d8eaf670968a569dd8a541af6db Mon Sep 17 00:00:00 2001 From: girorme Date: Mon, 23 Sep 2019 01:08:58 -0300 Subject: [PATCH 2/4] *In progress - Refactor Main to accept utils and add unit-tests --- bin/super-giggle | 6 ++++-- src/Main.php | 24 +++++++++++++++++++++--- src/Util.php | 8 +++++--- tests/src/MainTest.php | 0 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 tests/src/MainTest.php diff --git a/bin/super-giggle b/bin/super-giggle index 2fc4242..bd44f12 100755 --- a/bin/super-giggle +++ b/bin/super-giggle @@ -19,10 +19,12 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php') === true) { use SuperGiggle\Main; use SuperGiggle\Util; -$opts = Util::parseArgs(); +$util = new Util; +$opts = $util->parseArgs(); if (isset($opts['help'])) { - Util::printUsage(); + $util->printUsage(); } $phpcs = new Main(); +$phpcs->setUtil($util); $phpcs->run($opts); diff --git a/src/Main.php b/src/Main.php index 74502af..60bc03b 100644 --- a/src/Main.php +++ b/src/Main.php @@ -16,8 +16,6 @@ namespace SuperGiggle; -use SuperGiggle\Util; - class Main { @@ -42,6 +40,13 @@ class Main */ private $options = []; + /** + * Util class + * + * @var Util + */ + private $util; + /** * Indicates whether it has found error or not. * @@ -68,6 +73,19 @@ public function __construct() } + /** + * Set util class. + * + * @param Util $util Util class. + * + * @return void + */ + private function setUtil(Util $util): void + { + $this->util = $util; + } + + /** * Helper to display a message and exit. * @@ -195,7 +213,7 @@ private function parsePHPCSErrors(string $file): array $php = $this->options['php']; $phpcs = $this->options['phpcs']; $warnings = $this->options['warnings']; - $execString = Util::isWindows() === true ? "$phpcs --report=json --standard=$stndr $file $warnings" : + $execString = $this->util->isWindows() === true ? "$phpcs --report=json --standard=$stndr $file $warnings" : "$php $phpcs --report=json --standard=$stndr '$file' $warnings"; $response = shell_exec($execString); diff --git a/src/Util.php b/src/Util.php index bd3d93c..fb17e49 100644 --- a/src/Util.php +++ b/src/Util.php @@ -19,7 +19,7 @@ class Util { - protected $os; + private $os; /** @@ -100,11 +100,13 @@ public function getPhpCsBinary(): string /** - * Set current operating system + * Set current operating system. + * + * @param Os $os Os class. * * @return void */ - public function setOs($os): void + public function setOs(Os $os): void { $this->os = $os; } diff --git a/tests/src/MainTest.php b/tests/src/MainTest.php new file mode 100644 index 0000000..e69de29 From 9cec4d6123c3622d3f732970e2df794cfe494e78 Mon Sep 17 00:00:00 2001 From: girorme Date: Sun, 29 Sep 2019 15:35:00 -0300 Subject: [PATCH 3/4] Add var docs --- README.md | 5 +++++ src/Main.php | 2 +- src/Util.php | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5d3cc3..8943c16 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ But at the same time you still WANT to implement **good practices** and **standa --standard The name or path of the coding standard. Defaults to PSR12. ``` +## Run tests +```bash +$ composer test +``` + ### License ``` diff --git a/src/Main.php b/src/Main.php index 95efd90..eb7afdf 100644 --- a/src/Main.php +++ b/src/Main.php @@ -80,7 +80,7 @@ public function __construct() * * @return void */ - private function setUtil(Util $util): void + public function setUtil(Util $util): void { $this->util = $util; } diff --git a/src/Util.php b/src/Util.php index 891e1c9..499500c 100644 --- a/src/Util.php +++ b/src/Util.php @@ -19,6 +19,11 @@ class Util { + /** + * Os util + * + * @var Os + */ private $os; From b95c13b4eec262638fadf7155f4fcf803e1b5445 Mon Sep 17 00:00:00 2001 From: girorme Date: Sun, 29 Sep 2019 15:44:37 -0300 Subject: [PATCH 4/4] Change README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8943c16..c885570 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ But at the same time you still WANT to implement **good practices** and **standa ``` ## Run tests -```bash +``` $ composer test ```