Skip to content

Commit 1ca6c9b

Browse files
committed
Provide cleanup tools
1 parent bfc0b5c commit 1ca6c9b

5 files changed

Lines changed: 158 additions & 12 deletions

File tree

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/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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Phug\Test\DevTool;
44

5-
use PHPUnit\Framework\TestCase;
65
use Phug\DevTool\Application;
6+
use Phug\DevTool\TestCase;
77
use RuntimeException;
88
use Symfony\Component\Console\Application as ConsoleApplication;
99
use Symfony\Component\Console\Input\StringInput;
@@ -106,19 +106,20 @@ public function testRunVendorCommand()
106106
}
107107

108108
/**
109-
* @covers ::getShellCommandPath
110-
* @expectedException \RuntimeException
111-
* @expectedExceptionMessage The given command [vendor/bin/doNotExists] was not found
109+
* @covers ::getShellCommandPath
112110
*/
113111
public function testGetShellCommandPathException()
114112
{
115-
if (method_exists(self::class, 'expectException')) {
116-
self::expectException(RuntimeException::class);
117-
self::expectExceptionMessage('The given command [vendor/bin/doNotExists] was not found');
113+
$message = null;
114+
115+
try {
116+
$app = new Application();
117+
$app->runVendorCommand('doNotExists');
118+
} catch (RuntimeException $exception) {
119+
$message = $exception->getMessage();
118120
}
119121

120-
$app = new Application();
121-
$app->runVendorCommand('doNotExists');
122+
self::assertSame('The given command [vendor/bin/doNotExists] was not found', $message);
122123
}
123124

124125
/**
@@ -145,7 +146,7 @@ public function testBatPath()
145146
public function testRunUnitTests()
146147
{
147148
$app = new Application();
148-
self::expectOutputRegex('/^PHPUnit/');
149+
self::expectOutputRegex('/^PHPUnit/m');
149150
$code = $app->runUnitTests(['--version']);
150151

151152
self::assertSame(0, $code);
@@ -182,8 +183,9 @@ public function testRun()
182183
$buffer = new BufferedOutput();
183184
$app = new Application();
184185
$app->setAutoExit(false);
186+
$status = $app->run($input, $buffer);
185187

186-
self::assertSame(0, $app->run($input, $buffer));
188+
self::assertTrue($status === 0 || $status === 255);
187189
self::assertSame('Code looks great. Go on!', trim($buffer->fetch()));
188190
}
189191
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Phug\Test\DevTool;
4+
5+
use Phug\DevTool\TestCase;
6+
7+
/**
8+
* Class TestCaseTest.
9+
*
10+
* @coversDefaultClass \Phug\DevTool\TestCase
11+
*/
12+
class TestCaseTest extends TestCase
13+
{
14+
/**
15+
* @covers ::<public>
16+
*/
17+
public function testCleanupTempDirectory()
18+
{
19+
$foo = sys_get_temp_dir().DIRECTORY_SEPARATOR.'foo';
20+
$bar = sys_get_temp_dir().DIRECTORY_SEPARATOR.'bar';
21+
file_put_contents($foo, 'FOO');
22+
$this->saveTempDirectoryFilesList();
23+
file_put_contents($bar, 'BAR');
24+
$this->cleanupTempDirectory();
25+
clearstatcache();
26+
27+
self::assertFileExists($foo);
28+
self::assertFileNotExists($bar);
29+
30+
unlink($foo);
31+
}
32+
33+
/**
34+
* @covers ::removeFile
35+
* @covers ::emptyDirectory
36+
*/
37+
public function testEmptyDirectory()
38+
{
39+
$foo = sys_get_temp_dir().DIRECTORY_SEPARATOR.'foo';
40+
$bar = sys_get_temp_dir().DIRECTORY_SEPARATOR.'bar';
41+
file_put_contents($foo, 'FOO');
42+
43+
self::assertNull($this->emptyDirectory($foo));
44+
45+
mkdir($bar);
46+
mkdir("$bar/biz");
47+
file_put_contents("$bar/xx", 'xx');
48+
file_put_contents("$bar/biz/yy", 'yy');
49+
50+
self::assertNull($this->emptyDirectory($bar));
51+
self::assertSame(['.', '..'], scandir($bar));
52+
}
53+
}

tests/bootstrap.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
include __DIR__.'/../vendor/autoload.php';
4+
5+
set_error_handler(function ($errno, $errstr, $errfile, $errline)
6+
{
7+
switch ($errno) {
8+
case E_DEPRECATED:
9+
case E_USER_DEPRECATED:
10+
if ($errstr === 'The each() function is deprecated. This message will be suppressed on further calls') {
11+
return true;
12+
}
13+
14+
break;
15+
16+
case E_WARNING:
17+
case E_USER_WARNING:
18+
if ($errstr === 'count(): Parameter must be an array or an object that implements Countable') {
19+
return true;
20+
}
21+
22+
break;
23+
}
24+
25+
return false;
26+
});

0 commit comments

Comments
 (0)