2021-01-25 16:19:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-02-09 14:30:06 +00:00
|
|
|
namespace Illuminate\Tests\Testing;
|
2021-01-25 16:19:43 +00:00
|
|
|
|
|
|
|
|
use Illuminate\Container\Container;
|
|
|
|
|
use Illuminate\Testing\ParallelTesting;
|
2023-10-26 22:09:14 +08:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2021-01-25 16:19:43 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class ParallelTestingTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
Container::setInstance(new Container);
|
|
|
|
|
|
|
|
|
|
$_SERVER['LARAVEL_PARALLEL_TESTING'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 22:09:14 +08:00
|
|
|
#[DataProvider('callbacks')]
|
2025-09-02 01:45:37 +03:00
|
|
|
public function testCallbacks($callback): void
|
2021-01-25 16:19:43 +00:00
|
|
|
{
|
|
|
|
|
$parallelTesting = new ParallelTesting(Container::getInstance());
|
|
|
|
|
$caller = 'call'.ucfirst($callback).'Callbacks';
|
|
|
|
|
|
|
|
|
|
$state = false;
|
|
|
|
|
$parallelTesting->{$caller}($this);
|
|
|
|
|
$this->assertFalse($state);
|
|
|
|
|
|
|
|
|
|
$parallelTesting->{$callback}(function ($token, $testCase = null) use ($callback, &$state) {
|
|
|
|
|
if (in_array($callback, ['setUpTestCase', 'tearDownTestCase'])) {
|
|
|
|
|
$this->assertSame($this, $testCase);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertNull($testCase);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 15:46:19 +01:00
|
|
|
$this->assertSame('1', (string) $token);
|
2021-01-25 16:19:43 +00:00
|
|
|
$state = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$parallelTesting->{$caller}($this);
|
|
|
|
|
$this->assertFalse($state);
|
|
|
|
|
|
|
|
|
|
$parallelTesting->resolveTokenUsing(function () {
|
2021-10-28 21:42:35 +02:00
|
|
|
return '1';
|
2021-01-25 16:19:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$parallelTesting->{$caller}($this);
|
|
|
|
|
$this->assertTrue($state);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 01:45:37 +03:00
|
|
|
public function testOptions(): void
|
2021-01-25 16:19:43 +00:00
|
|
|
{
|
|
|
|
|
$parallelTesting = new ParallelTesting(Container::getInstance());
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($parallelTesting->option('recreate_databases'));
|
2021-07-27 22:02:24 +09:00
|
|
|
$this->assertFalse($parallelTesting->option('without_databases'));
|
2021-01-25 16:19:43 +00:00
|
|
|
|
|
|
|
|
$parallelTesting->resolveOptionsUsing(function ($option) {
|
2021-03-18 22:27:31 +01:00
|
|
|
return $option === 'recreate_databases';
|
2021-01-25 16:19:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($parallelTesting->option('recreate_caches'));
|
2021-07-27 22:02:24 +09:00
|
|
|
$this->assertFalse($parallelTesting->option('without_databases'));
|
2021-01-25 16:19:43 +00:00
|
|
|
$this->assertTrue($parallelTesting->option('recreate_databases'));
|
2021-07-27 22:02:24 +09:00
|
|
|
|
|
|
|
|
$parallelTesting->resolveOptionsUsing(function ($option) {
|
|
|
|
|
return $option === 'without_databases';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($parallelTesting->option('without_databases'));
|
2021-01-25 16:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-02 01:45:37 +03:00
|
|
|
public function testToken(): void
|
2021-01-25 16:19:43 +00:00
|
|
|
{
|
|
|
|
|
$parallelTesting = new ParallelTesting(Container::getInstance());
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($parallelTesting->token());
|
|
|
|
|
|
|
|
|
|
$parallelTesting->resolveTokenUsing(function () {
|
2021-10-28 21:42:35 +02:00
|
|
|
return '1';
|
2021-01-25 16:19:43 +00:00
|
|
|
});
|
|
|
|
|
|
2022-02-22 15:46:19 +01:00
|
|
|
$this->assertSame('1', (string) $parallelTesting->token());
|
2021-01-25 16:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 05:10:22 +08:00
|
|
|
public static function callbacks()
|
2021-01-25 16:19:43 +00:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['setUpProcess'],
|
|
|
|
|
['setUpTestCase'],
|
2021-02-18 15:11:09 +00:00
|
|
|
['setUpTestDatabase'],
|
2025-12-07 17:08:42 +01:00
|
|
|
['setUpTestDatabaseBeforeMigrating'],
|
2021-01-25 16:19:43 +00:00
|
|
|
['tearDownTestCase'],
|
|
|
|
|
['tearDownProcess'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 15:31:03 +01:00
|
|
|
protected function tearDown(): void
|
2021-01-25 16:19:43 +00:00
|
|
|
{
|
|
|
|
|
Container::setInstance(null);
|
|
|
|
|
|
|
|
|
|
unset($_SERVER['LARAVEL_PARALLEL_TESTING']);
|
2026-01-05 18:12:30 +01:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
2021-01-25 16:19:43 +00:00
|
|
|
}
|
|
|
|
|
}
|