forked from getkirby/kirby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpUnitExtension.php
74 lines (64 loc) · 1.96 KB
/
PhpUnitExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Kirby;
use Kirby\Cms\App;
use Kirby\Cms\Core;
use Kirby\Filesystem\Dir;
use PHPUnit\Event\TestRunner\ExecutionFinished;
use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
/**
* PHPUnit extension to bootstrap the tests and
* manage the temporary directory during testing
*
* @author Lukas Bestle <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
final class PhpUnitExtension implements Extension
{
public function bootstrap(
Configuration $configuration,
Facade $facade,
ParameterCollection $parameters
): void {
// ensure the temp directory is there
Dir::make(KIRBY_TMP_DIR);
// delete the temp directory again after the tests
$facade->registerSubscriber(new TempCleanupSubscriber());
}
public static function init(): void
{
// determine a unique path to a temporary directory
$tmpDir = __DIR__ . '/tmp';
// when running via ParaTest, use a separate directory for each process
if (getenv('UNIQUE_TEST_TOKEN') !== false) {
$tmpDir .= '/' . getenv('UNIQUE_TEST_TOKEN');
}
define('KIRBY_TMP_DIR', $tmpDir);
define('KIRBY_TESTING', true);
// disable Whoops for all tests that don't need it
// to reduce the impact of memory leaks
App::$enableWhoops = false;
// prevent PHPUnit tests from accessing files outside the repo
Core::$indexRoot = '/dev/null';
}
}
/**
* PHPUnit event subscriber to be executed after all tests have been run
*
* @author Lukas Bestle <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
final class TempCleanupSubscriber implements ExecutionFinishedSubscriber
{
public function notify(ExecutionFinished $event): void
{
Dir::remove(KIRBY_TMP_DIR);
}
}