Skip to content

Commit adf17f5

Browse files
authored
Merge pull request #8 from phug-php/1.x
Provide cleanup tools
2 parents 87b1b40 + 21179dd commit adf17f5

13 files changed

Lines changed: 244 additions & 67 deletions

File tree

bin/phug-dev

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use Phug\DevTool\Application;
66
//Check for autoload file
77
$cwd = getcwd();
88
$path = "$cwd/vendor/autoload.php";
9-
if (file_exists($path)) {
10-
11-
require $path;
12-
} else {
139

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

15+
require $path;
16+
1817
(new Application)->run();

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@
2525
"source": "https://github.com/phug-php/dev-tool",
2626
"docs": "http://phug-lang.com/docs"
2727
},
28-
"minimum-stability": "stable",
28+
"minimum-stability": "dev",
29+
"prefer-stable": true,
2930
"require": {
3031
"php": ">=5.5.0",
31-
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0",
32-
"phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0",
33-
"squizlabs/php_codesniffer": "^2.8",
34-
"symfony/console": "^3.2",
35-
"codeclimate/php-test-reporter": "^0.4.0"
32+
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^7.0 || ^8.0",
33+
"phpunit/php-code-coverage": "^2.2 || ^4.0 || ^5.2 || ^6.0 || ^7.0",
34+
"squizlabs/php_codesniffer": "^2.8 || ^3.0",
35+
"symfony/console": "^3.2 || ^4.0 || ^5.0"
3636
},
3737
"autoload": {
3838
"psr-4": {
3939
"": "./src"
4040
}
4141
},
42-
"bin": ["bin/phug-dev"],
43-
"suggest": {}
42+
"bin": ["bin/phug-dev"]
4443
}

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.6/phpunit.xsd"
44
colors="true"
5-
bootstrap="./vendor/autoload.php"
5+
bootstrap="./tests/bootstrap.php"
66
forceCoversAnnotation="true"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"

src/Phug/DevTool/Application.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Phug\DevTool\Command\CodeStyleFixCommand;
88
use Phug\DevTool\Command\CoverageCheckCommand;
99
use Phug\DevTool\Command\CoverageReportCommand;
10+
use Phug\DevTool\Command\CoverageReportPrepareCommand;
1011
use Phug\DevTool\Command\InstallCommand;
1112
use Phug\DevTool\Command\UnitTestsRunCommand;
1213
use RuntimeException;
@@ -31,6 +32,7 @@ protected function configure()
3132
$this->add(new CodeStyleFixCommand());
3233
$this->add(new CoverageCheckCommand());
3334
$this->add(new CoverageReportCommand());
35+
$this->add(new CoverageReportPrepareCommand());
3436
$this->add(new InstallCommand());
3537
$this->add(new UnitTestsRunCommand());
3638
}
@@ -142,9 +144,36 @@ public function runCodeStyleFixer(array $arguments = null)
142144
return $this->runVendorCommand('phpcbf', $arguments);
143145
}
144146

145-
public function runCoverageReporter(array $arguments = null)
147+
public function runCoverageReporterPreparation()
146148
{
147-
return $this->runVendorCommand('test-reporter', $arguments);
149+
$url = 'https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64';
150+
151+
return $this->runShellCommand(implode(' && ', [
152+
"curl -L $url > ./cc-test-reporter",
153+
'chmod +x ./cc-test-reporter',
154+
'./cc-test-reporter before-build',
155+
]));
156+
}
157+
158+
public function runCoverageReporter()
159+
{
160+
$clover = file_exists('clover.xml');
161+
$coverage = file_exists('coverage.xml');
162+
163+
if (!$clover && !$coverage) {
164+
return 0; // No report to send
165+
} elseif (!$clover && $coverage) {
166+
copy('coverage.xml', 'clover.xml');
167+
} elseif (!$coverage && $clover) {
168+
copy('clover.xml', 'coverage.xml');
169+
}
170+
171+
return $this->runShellCommand(implode(' && ', [
172+
'bash <(curl -s https://codecov.io/bash)',
173+
'./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT',
174+
'composer require codacy/coverage',
175+
'vendor/bin/codacycoverage clover coverage.xml',
176+
]));
148177
}
149178

150179
public function run(InputInterface $input = null, OutputInterface $output = null)

src/Phug/DevTool/Command/CoverageReportCommand.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ protected function configure()
3030

3131
protected function execute(InputInterface $input, OutputInterface $output)
3232
{
33-
$xmlFile = realpath($input->getArgument('input-file'));
34-
3533
$phpVersion = $input->getOption('php-version');
3634

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

52-
$this->getApplication()->runVendorCommand('test-reporter', [
53-
'--coverage-report' => $xmlFile,
54-
]);
55-
56-
return 0;
50+
return $this->getApplication()->runCoverageReporter();
5751
}
5852
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Phug\DevTool\Command;
4+
5+
use Phug\DevTool\AbstractCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class CoverageReportPrepareCommand extends AbstractCommand
10+
{
11+
protected function configure()
12+
{
13+
$this->setName('coverage:report:prepare')
14+
->setHelp('This command install the coverage report utils.');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
$phpVersion = $input->getOption('php-version');
20+
21+
if (!empty($phpVersion)) {
22+
if (!preg_match('/^'.preg_quote($phpVersion).'(\D.*)?$/', PHP_VERSION)) {
23+
$output->writeln(
24+
'Test report ignored since PHP version ('.PHP_VERSION.')'.
25+
' does not match '.$phpVersion.'.'
26+
);
27+
28+
return 0;
29+
}
30+
$output->writeln(
31+
'<fg=green>Proceed test report since PHP version ('.PHP_VERSION.') '.
32+
'matches '.$phpVersion.'.</>'
33+
);
34+
}
35+
36+
return $this->getApplication()->runCoverageReporterPreparation();
37+
}
38+
}

src/Phug/DevTool/TestCase.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Phug\DevTool;
4+
5+
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
6+
7+
class TestCase extends PHPUnitTestCase
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $tempDirectory;
13+
14+
/**
15+
* @var string[]
16+
*/
17+
protected $tempDirectoryFiles;
18+
19+
/**
20+
* @before
21+
*/
22+
public function saveTempDirectoryFilesList()
23+
{
24+
$this->tempDirectory = $this->tempDirectory ?: sys_get_temp_dir();
25+
26+
$this->tempDirectoryFiles = scandir($this->tempDirectory);
27+
}
28+
29+
/**
30+
* @after
31+
*/
32+
public function cleanupTempDirectory()
33+
{
34+
$files = scandir($this->tempDirectory);
35+
36+
foreach (array_diff($files, $this->tempDirectoryFiles) as $file) {
37+
$this->removeFile($this->tempDirectory.DIRECTORY_SEPARATOR.$file);
38+
}
39+
}
40+
41+
protected function removeFile($file)
42+
{
43+
if (is_dir($file)) {
44+
$this->emptyDirectory($file);
45+
rmdir($file);
46+
47+
return;
48+
}
49+
50+
unlink($file);
51+
}
52+
53+
protected function emptyDirectory($dir)
54+
{
55+
if (!is_dir($dir)) {
56+
return;
57+
}
58+
59+
foreach (scandir($dir) as $file) {
60+
if ($file !== '.' && $file !== '..') {
61+
$this->removeFile($dir.'/'.$file);
62+
}
63+
}
64+
}
65+
}

tests/Phug/DevTool/ApplicationTest.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Phug\Test\DevTool;
44

5-
use PHPUnit\Framework\TestCase;
65
use Phug\DevTool\Application;
6+
use Phug\DevTool\TestCase;
7+
use RuntimeException;
78
use Symfony\Component\Console\Application as ConsoleApplication;
89
use Symfony\Component\Console\Input\StringInput;
910
use Symfony\Component\Console\Output\BufferedOutput;
@@ -105,14 +106,20 @@ public function testRunVendorCommand()
105106
}
106107

107108
/**
108-
* @covers ::getShellCommandPath
109-
* @expectedException \RuntimeException
110-
* @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found
109+
* @covers ::getShellCommandPath
111110
*/
112111
public function testGetShellCommandPathException()
113112
{
114-
$app = new Application();
115-
$app->runVendorCommand('doNotExists');
113+
$message = null;
114+
115+
try {
116+
$app = new Application();
117+
$app->runVendorCommand('doNotExists');
118+
} catch (RuntimeException $exception) {
119+
$message = $exception->getMessage();
120+
}
121+
122+
self::assertSame('The given command [vendor/bin/doNotExists] was not found', $message);
116123
}
117124

118125
/**
@@ -139,7 +146,7 @@ public function testBatPath()
139146
public function testRunUnitTests()
140147
{
141148
$app = new Application();
142-
self::expectOutputRegex('/^PHPUnit/');
149+
self::expectOutputRegex('/^PHPUnit/m');
143150
$code = $app->runUnitTests(['--version']);
144151

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

170-
/**
171-
* @covers ::runCoverageReporter
172-
*/
173-
public function testRunCoverageReporter()
174-
{
175-
$app = new Application();
176-
177-
self::expectOutputRegex('/Code Climate PHP Test Reporter/');
178-
self::assertSame(0, $app->runCoverageReporter(['--version']));
179-
}
180-
181177
/**
182178
* @covers ::run
183179
*/
@@ -187,8 +183,9 @@ public function testRun()
187183
$buffer = new BufferedOutput();
188184
$app = new Application();
189185
$app->setAutoExit(false);
186+
$status = $app->run($input, $buffer);
190187

191-
self::assertSame(0, $app->run($input, $buffer));
188+
self::assertTrue($status === 0 || $status === 255);
192189
self::assertSame('Code looks great. Go on!', trim($buffer->fetch()));
193190
}
194191
}

tests/Phug/DevTool/Command/CheckCommandTest.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,18 @@ public function testExecute()
3636
foreach (glob(__DIR__.'/../../../app/vendor/bin/*') as $file) {
3737
chmod($file, 0777);
3838
}
39-
file_put_contents('coverage.xml', '<?xml version="1.0" encoding="UTF-8"?>
40-
<coverage generated="1482856255">
41-
<project timestamp="1482856255">
42-
<package name="Phug\DevTool">
43-
<file name="src/Phug/DevTool/Application.php">
44-
<class name="Application" namespace="Phug\DevTool">
45-
<metrics complexity="26" methods="17" coveredmethods="17" conditionals="0" coveredconditionals="0" statements="49" coveredstatements="49" elements="66" coveredelements="66"/>
46-
</class>
47-
<line num="20" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="1"/>
48-
<line num="22" type="stmt" count="1"/>
49-
<line num="24" type="stmt" count="1"/>
50-
<line num="25" type="stmt" count="1"/>
51-
</file>
52-
</package>
53-
</project>
54-
</coverage>');
5539
$input = new StringInput('check');
5640
$buffer = new BufferedOutput();
5741
$app = new Application();
5842
$app->setAutoExit(false);
59-
$code = $app->run($input, $buffer);
43+
ob_start();
44+
$app->run($input, $buffer);
45+
ob_end_clean();
6046
if (file_exists('coverage.xml')) {
6147
unlink('coverage.xml');
6248
}
6349
chdir($cwd);
6450

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

0 commit comments

Comments
 (0)