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/README.md b/README.md index d5d3cc3..c885570 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 +``` +$ composer test +``` + ### License ``` 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/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/Main.php b/src/Main.php index b97826a..eb7afdf 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 + */ + public function setUtil(Util $util): void + { + $this->util = $util; + } + + /** * Helper to display a message and exit. * @@ -196,7 +214,7 @@ private function parsePHPCSErrors(string $file): array $phpcs = $this->options['phpcs']; $warnings = $this->options['warnings']; $phpVersion = (empty($this->options['php-version']) === true) ? '' : "--runtime-set php_version {$this->options['php-version']}"; - $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 $phpVersion"; $response = shell_exec($execString); 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'); } @@ -99,13 +106,15 @@ public static function getPhpCsBinary(): string /** - * Checks the OS and returns true if Windows system is found. + * Set current operating system. + * + * @param Os $os Os class. * - * @return boolean + * @return void */ - public static function isWindows(): bool + public function setOs(Os $os): void { - return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'); + $this->os = $os; } diff --git a/tests/src/MainTest.php b/tests/src/MainTest.php new file mode 100644 index 0000000..e69de29 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()); + } + + +}