Spaces:
No application file
No application file
File size: 1,859 Bytes
d2897cd |
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 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Test\Hooks;
use Mautic\CoreBundle\Test\EnvLoader;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\AfterTestHook;
use PHPUnit\Runner\BeforeFirstTestHook;
/**
* This extension allows you to run an arbitrary test after every test in the current suite which is
* extremely helpful when hunting down a functional test that causes your test fail.
* When your test fails, the execution is stopped immediately and the name of problematic test is written to STDOUT.
*
* Example of usage: `MAUTIC_TEST_EXECUTE_TEST_AFTER="Fully\\Qualified\\Class\\NameTest" bin/phpunit`.
*/
class MauticExtension implements AfterTestHook, BeforeFirstTestHook
{
public function executeAfterTest(string $test, float $time): void
{
$testClass = getenv('MAUTIC_TEST_EXECUTE_TEST_AFTER');
if (false === $testClass) {
return;
}
$testSuite = new TestSuite();
$testSuite->addTestSuite($testClass);
$result = $testSuite->run();
if (!$result->wasSuccessful()) {
$failures = array_map(fn (TestFailure $testFailure) => $testFailure->getExceptionAsString(), array_merge($result->failures(), $result->errors()));
exit(sprintf('The previous test was: "%s". Your test errored with: %s', $test, implode(PHP_EOL, $failures)));
}
}
/**
* To set a custom table prefix clear test cache and run.
*
* Example usage: `MAUTIC_DB_PREFIX='custom_prefix_' php bin/phpunit`
*/
public function executeBeforeFirstTest(): void
{
EnvLoader::load();
$prefix = false === getenv('MAUTIC_DB_PREFIX') ? 'test_' : getenv('MAUTIC_DB_PREFIX');
define('MAUTIC_TABLE_PREFIX', $prefix);
echo 'using db prefix "'.$prefix.'"'.PHP_EOL;
}
}
|