Skip to content

Commit

Permalink
Merge pull request #8 from phug-php/1.x
Browse files Browse the repository at this point in the history
Provide cleanup tools
  • Loading branch information
kylekatarnls authored Nov 24, 2019
2 parents 87b1b40 + 21179dd commit adf17f5
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 67 deletions.
7 changes: 3 additions & 4 deletions bin/phug-dev
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use Phug\DevTool\Application;
//Check for autoload file
$cwd = getcwd();
$path = "$cwd/vendor/autoload.php";
if (file_exists($path)) {

require $path;
} else {

if (!file_exists($path)) {
echo "Composer's autoload.php not found in $cwd.";
exit(1);
}

require $path;

(new Application)->run();
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@
"source": "https://github.com/phug-php/dev-tool",
"docs": "http://phug-lang.com/docs"
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=5.5.0",
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0",
"phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0",
"squizlabs/php_codesniffer": "^2.8",
"symfony/console": "^3.2",
"codeclimate/php-test-reporter": "^0.4.0"
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0 || ^8.0",
"phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0 || ^7.0",
"squizlabs/php_codesniffer": "^2.8 || ^3.0",
"symfony/console": "^3.2 || ^4.0 || ^5.0"
},
"autoload": {
"psr-4": {
"": "./src"
}
},
"bin": ["bin/phug-dev"],
"suggest": {}
"bin": ["bin/phug-dev"]
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.6/phpunit.xsd"
colors="true"
bootstrap="./vendor/autoload.php"
bootstrap="./tests/bootstrap.php"
forceCoversAnnotation="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
33 changes: 31 additions & 2 deletions src/Phug/DevTool/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phug\DevTool\Command\CodeStyleFixCommand;
use Phug\DevTool\Command\CoverageCheckCommand;
use Phug\DevTool\Command\CoverageReportCommand;
use Phug\DevTool\Command\CoverageReportPrepareCommand;
use Phug\DevTool\Command\InstallCommand;
use Phug\DevTool\Command\UnitTestsRunCommand;
use RuntimeException;
Expand All @@ -31,6 +32,7 @@ protected function configure()
$this->add(new CodeStyleFixCommand());
$this->add(new CoverageCheckCommand());
$this->add(new CoverageReportCommand());
$this->add(new CoverageReportPrepareCommand());
$this->add(new InstallCommand());
$this->add(new UnitTestsRunCommand());
}
Expand Down Expand Up @@ -142,9 +144,36 @@ public function runCodeStyleFixer(array $arguments = null)
return $this->runVendorCommand('phpcbf', $arguments);
}

public function runCoverageReporter(array $arguments = null)
public function runCoverageReporterPreparation()
{
return $this->runVendorCommand('test-reporter', $arguments);
$url = 'https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64';

return $this->runShellCommand(implode(' && ', [
"curl -L $url > ./cc-test-reporter",
'chmod +x ./cc-test-reporter',
'./cc-test-reporter before-build',
]));
}

public function runCoverageReporter()
{
$clover = file_exists('clover.xml');
$coverage = file_exists('coverage.xml');

if (!$clover && !$coverage) {
return 0; // No report to send
} elseif (!$clover && $coverage) {
copy('coverage.xml', 'clover.xml');
} elseif (!$coverage && $clover) {
copy('clover.xml', 'coverage.xml');
}

return $this->runShellCommand(implode(' && ', [
'bash <(curl -s https://codecov.io/bash)',
'./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT',
'composer require codacy/coverage',
'vendor/bin/codacycoverage clover coverage.xml',
]));
}

public function run(InputInterface $input = null, OutputInterface $output = null)
Expand Down
8 changes: 1 addition & 7 deletions src/Phug/DevTool/Command/CoverageReportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$xmlFile = realpath($input->getArgument('input-file'));

$phpVersion = $input->getOption('php-version');

if (!empty($phpVersion)) {
Expand All @@ -49,10 +47,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}

$this->getApplication()->runVendorCommand('test-reporter', [
'--coverage-report' => $xmlFile,
]);

return 0;
return $this->getApplication()->runCoverageReporter();
}
}
38 changes: 38 additions & 0 deletions src/Phug/DevTool/Command/CoverageReportPrepareCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Phug\DevTool\Command;

use Phug\DevTool\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CoverageReportPrepareCommand extends AbstractCommand
{
protected function configure()
{
$this->setName('coverage:report:prepare')
->setHelp('This command install the coverage report utils.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$phpVersion = $input->getOption('php-version');

if (!empty($phpVersion)) {
if (!preg_match('/^'.preg_quote($phpVersion).'(\D.*)?$/', PHP_VERSION)) {
$output->writeln(
'Test report ignored since PHP version ('.PHP_VERSION.')'.
' does not match '.$phpVersion.'.'
);

return 0;
}
$output->writeln(
'<fg=green>Proceed test report since PHP version ('.PHP_VERSION.') '.
'matches '.$phpVersion.'.</>'
);
}

return $this->getApplication()->runCoverageReporterPreparation();
}
}
65 changes: 65 additions & 0 deletions src/Phug/DevTool/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Phug\DevTool;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends PHPUnitTestCase
{
/**
* @var string
*/
protected $tempDirectory;

/**
* @var string[]
*/
protected $tempDirectoryFiles;

/**
* @before
*/
public function saveTempDirectoryFilesList()
{
$this->tempDirectory = $this->tempDirectory ?: sys_get_temp_dir();

$this->tempDirectoryFiles = scandir($this->tempDirectory);
}

/**
* @after
*/
public function cleanupTempDirectory()
{
$files = scandir($this->tempDirectory);

foreach (array_diff($files, $this->tempDirectoryFiles) as $file) {
$this->removeFile($this->tempDirectory.DIRECTORY_SEPARATOR.$file);
}
}

protected function removeFile($file)
{
if (is_dir($file)) {
$this->emptyDirectory($file);
rmdir($file);

return;
}

unlink($file);
}

protected function emptyDirectory($dir)
{
if (!is_dir($dir)) {
return;
}

foreach (scandir($dir) as $file) {
if ($file !== '.' && $file !== '..') {
$this->removeFile($dir.'/'.$file);
}
}
}
}
35 changes: 16 additions & 19 deletions tests/Phug/DevTool/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Phug\Test\DevTool;

use PHPUnit\Framework\TestCase;
use Phug\DevTool\Application;
use Phug\DevTool\TestCase;
use RuntimeException;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -105,14 +106,20 @@ public function testRunVendorCommand()
}

/**
* @covers ::getShellCommandPath
* @expectedException \RuntimeException
* @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found
* @covers ::getShellCommandPath
*/
public function testGetShellCommandPathException()
{
$app = new Application();
$app->runVendorCommand('doNotExists');
$message = null;

try {
$app = new Application();
$app->runVendorCommand('doNotExists');
} catch (RuntimeException $exception) {
$message = $exception->getMessage();
}

self::assertSame('The given command [vendor/bin/doNotExists] was not found', $message);
}

/**
Expand All @@ -139,7 +146,7 @@ public function testBatPath()
public function testRunUnitTests()
{
$app = new Application();
self::expectOutputRegex('/^PHPUnit/');
self::expectOutputRegex('/^PHPUnit/m');
$code = $app->runUnitTests(['--version']);

self::assertSame(0, $code);
Expand Down Expand Up @@ -167,17 +174,6 @@ public function testRunCodeStyleFixer()
self::assertSame(0, $app->runCodeStyleFixer(['--version']));
}

/**
* @covers ::runCoverageReporter
*/
public function testRunCoverageReporter()
{
$app = new Application();

self::expectOutputRegex('/Code Climate PHP Test Reporter/');
self::assertSame(0, $app->runCoverageReporter(['--version']));
}

/**
* @covers ::run
*/
Expand All @@ -187,8 +183,9 @@ public function testRun()
$buffer = new BufferedOutput();
$app = new Application();
$app->setAutoExit(false);
$status = $app->run($input, $buffer);

self::assertSame(0, $app->run($input, $buffer));
self::assertTrue($status === 0 || $status === 255);
self::assertSame('Code looks great. Go on!', trim($buffer->fetch()));
}
}
25 changes: 4 additions & 21 deletions tests/Phug/DevTool/Command/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,18 @@ public function testExecute()
foreach (glob(__DIR__.'/../../../app/vendor/bin/*') as $file) {
chmod($file, 0777);
}
file_put_contents('coverage.xml', '<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1482856255">
<project timestamp="1482856255">
<package name="Phug\DevTool">
<file name="src/Phug/DevTool/Application.php">
<class name="Application" namespace="Phug\DevTool">
<metrics complexity="26" methods="17" coveredmethods="17" conditionals="0" coveredconditionals="0" statements="49" coveredstatements="49" elements="66" coveredelements="66"/>
</class>
<line num="20" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="1"/>
<line num="22" type="stmt" count="1"/>
<line num="24" type="stmt" count="1"/>
<line num="25" type="stmt" count="1"/>
</file>
</package>
</project>
</coverage>');
$input = new StringInput('check');
$buffer = new BufferedOutput();
$app = new Application();
$app->setAutoExit(false);
$code = $app->run($input, $buffer);
ob_start();
$app->run($input, $buffer);
ob_end_clean();
if (file_exists('coverage.xml')) {
unlink('coverage.xml');
}
chdir($cwd);

self::assertSame(0, $code);
$checkStyle = version_compare(PHP_VERSION, '5.6.0') >= 0;
$expectedPattern = $checkStyle ? '/Code looks great\. Go on!/' : '/Code Coverage/';
self::assertRegExp($expectedPattern, $buffer->fetch());
self::assertTrue(strpos($buffer->fetch(), 'Error: Code coverage files not found. Please run `unit-tests:run`') !== false);
}
}
Loading

0 comments on commit adf17f5

Please sign in to comment.