Skip to content

Commit

Permalink
Merge pull request #7 from girorme/feature/add-phpunit
Browse files Browse the repository at this point in the history
Add phpunit
  • Loading branch information
roger-sei committed Sep 29, 2019
2 parents 4b5780a + b95c13b commit 36d79e0
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor/
composer.lock
.phpunit.result.*
build/
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
6 changes: 4 additions & 2 deletions bin/super-giggle
Original file line number Diff line number Diff line change
Expand Up @@ -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);
17 changes: 15 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -11,5 +18,11 @@
"license": "MIT",
"bin": [
"bin/super-giggle"
]
],
"require-dev": {
"phpunit/phpunit": "^8"
},
"scripts": {
"test": "phpunit --testdox --colors=always"
}
}
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="SupperGiggle Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
24 changes: 21 additions & 3 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace SuperGiggle;

use SuperGiggle\Util;

class Main
{

Expand All @@ -42,6 +40,13 @@ class Main
*/
private $options = [];

/**
* Util class
*
* @var Util
*/
private $util;

/**
* Indicates whether it has found error or not.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/Os.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace SuperGiggle;

class Os
{


/**
* Is the current platform windows?
*
* @return boolean
*/
public function isWindows()
{
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
}


}
25 changes: 17 additions & 8 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
class Util
{

/**
* Os util
*
* @var Os
*/
private $os;


/**
* Parse args and return in a friendly format
*
* @return array
*/
public static function parseArgs(): array
public function parseArgs(): array
{
$alloweds = [
'commit::',
Expand Down Expand Up @@ -56,7 +63,7 @@ public static function parseArgs(): array
*
* @return void
*/
public static function printUsage(): void
public function printUsage(): void
{
echo " Usage: \033[0;35msuper-giggle [--commit]\033[0m\n\n";
$options = [
Expand Down Expand Up @@ -86,11 +93,11 @@ public static function printUsage(): void
*
* @return string
*/
public static function getPhpCsBinary(): string
public function getPhpCsBinary(): string
{
$path = __DIR__ . '/../vendor/bin/phpcs';

if (self::isWindows() === true) {
if ($this->os->isWindows() === true) {
$path = str_replace('\\', '/', __DIR__ . '/../vendor/bin/phpcs.bat');
}

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


Expand Down
Empty file added tests/src/MainTest.php
Empty file.
63 changes: 63 additions & 0 deletions tests/src/UtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace SuperGiggle;

use PHPUnit\Framework\TestCase;

class UtilTest extends TestCase
{


/**
* Define if the object can be constructed
*
* @test
*
* @return void
*/
public function objectCanBeConstructed()
{
$util = new Util();
$this->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());
}


}

0 comments on commit 36d79e0

Please sign in to comment.