Skip to content

Commit 958386a

Browse files
authored
[8.x] Adds parallel testing (#36034)
* Adds parallel testing * Adds paratest to testing component's "suggest" section
1 parent e720279 commit 958386a

32 files changed

+1479
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"ext-posix": "Required to use all features of the queue worker.",
130130
"ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0).",
131131
"aws/aws-sdk-php": "Required to use the SQS queue driver, DynamoDb failed job storage and SES mail driver (^3.155).",
132+
"brianium/paratest": "Required to run tests in parallel (^6.0).",
132133
"doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6|^3.0).",
133134
"filp/whoops": "Required for friendly error pages in development (^2.8).",
134135
"fakerphp/faker": "Required to use the eloquent factory builder (^1.9.1).",

src/Illuminate/Database/Schema/Builder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ public static function morphUsingUuids()
9494
return static::defaultMorphKeyType('uuid');
9595
}
9696

97+
/**
98+
* Create a database in the schema.
99+
*
100+
* @param string $name
101+
* @return bool
102+
*/
103+
public function createDatabase($name)
104+
{
105+
throw new LogicException('This database driver does not support creating databases.');
106+
}
107+
108+
/**
109+
* Drop a database from the schema if the database exists.
110+
*
111+
* @param string $name
112+
* @return bool
113+
*/
114+
public function dropDatabaseIfExists($name)
115+
{
116+
throw new LogicException('This database driver does not support dropping databases.');
117+
}
118+
97119
/**
98120
* Determine if the given table exists.
99121
*

src/Illuminate/Database/Schema/Grammars/Grammar.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Query\Expression;
1010
use Illuminate\Database\Schema\Blueprint;
1111
use Illuminate\Support\Fluent;
12+
use LogicException;
1213
use RuntimeException;
1314

1415
abstract class Grammar extends BaseGrammar
@@ -27,6 +28,29 @@ abstract class Grammar extends BaseGrammar
2728
*/
2829
protected $fluentCommands = [];
2930

31+
/**
32+
* Compile a create database command.
33+
*
34+
* @param string $name
35+
* @param \Illuminate\Database\Connection $connection
36+
* @return string
37+
*/
38+
public function compileCreateDatabase($name, $connection)
39+
{
40+
throw new LogicException('This database driver does not support creating databases.');
41+
}
42+
43+
/**
44+
* Compile a drop database if exists command.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
public function compileDropDatabaseIfExists($name)
50+
{
51+
throw new LogicException('This database driver does not support dropping databases.');
52+
}
53+
3054
/**
3155
* Compile a rename column command.
3256
*

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ class MySqlGrammar extends Grammar
2626
*/
2727
protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
2828

29+
/**
30+
* Compile a create database command.
31+
*
32+
* @param string $name
33+
* @param \Illuminate\Database\Connection $connection
34+
* @return string
35+
*/
36+
public function compileCreateDatabase($name, $connection)
37+
{
38+
return sprintf(
39+
'create database %s default character set %s default collate %s',
40+
$this->wrapValue($name),
41+
$this->wrapValue($connection->getConfig('charset')),
42+
$this->wrapValue($connection->getConfig('collation')),
43+
);
44+
}
45+
46+
/**
47+
* Compile a drop database if exists command.
48+
*
49+
* @param string $name
50+
* @return string
51+
*/
52+
public function compileDropDatabaseIfExists($name)
53+
{
54+
return sprintf(
55+
'drop database if exists %s',
56+
$this->wrapValue($name)
57+
);
58+
}
59+
2960
/**
3061
* Compile the query to determine the list of tables.
3162
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ class PostgresGrammar extends Grammar
3535
*/
3636
protected $fluentCommands = ['Comment'];
3737

38+
/**
39+
* Compile a create database command.
40+
*
41+
* @param string $name
42+
* @param \Illuminate\Database\Connection $connection
43+
* @return string
44+
*/
45+
public function compileCreateDatabase($name, $connection)
46+
{
47+
return sprintf(
48+
'create database %s encoding %s',
49+
$this->wrapValue($name),
50+
$this->wrapValue($connection->getConfig('charset')),
51+
);
52+
}
53+
54+
/**
55+
* Compile a drop database if exists command.
56+
*
57+
* @param string $name
58+
* @return string
59+
*/
60+
public function compileDropDatabaseIfExists($name)
61+
{
62+
return sprintf(
63+
'drop database if exists %s',
64+
$this->wrapValue($name)
65+
);
66+
}
67+
3868
/**
3969
* Compile the query to determine if a table exists.
4070
*

src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ class SqlServerGrammar extends Grammar
2828
*/
2929
protected $serials = ['tinyInteger', 'smallInteger', 'mediumInteger', 'integer', 'bigInteger'];
3030

31+
/**
32+
* Compile a create database command.
33+
*
34+
* @param string $name
35+
* @param \Illuminate\Database\Connection $connection
36+
* @return string
37+
*/
38+
public function compileCreateDatabase($name, $connection)
39+
{
40+
return sprintf(
41+
'create database %s',
42+
$this->wrapValue($name),
43+
);
44+
}
45+
46+
/**
47+
* Compile a drop database if exists command.
48+
*
49+
* @param string $name
50+
* @return string
51+
*/
52+
public function compileDropDatabaseIfExists($name)
53+
{
54+
return sprintf(
55+
'drop database if exists %s',
56+
$this->wrapValue($name)
57+
);
58+
}
59+
3160
/**
3261
* Compile the query to determine if a table exists.
3362
*

src/Illuminate/Database/Schema/MySqlBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@
44

55
class MySqlBuilder extends Builder
66
{
7+
/**
8+
* Create a database in the schema.
9+
*
10+
* @param string $name
11+
* @return bool
12+
*/
13+
public function createDatabase($name)
14+
{
15+
return $this->connection->statement(
16+
$this->grammar->compileCreateDatabase($name, $this->connection)
17+
);
18+
}
19+
20+
/**
21+
* Drop a database from the schema if the database exists.
22+
*
23+
* @param string $name
24+
* @return bool
25+
*/
26+
public function dropDatabaseIfExists($name)
27+
{
28+
return $this->connection->statement(
29+
$this->grammar->compileDropDatabaseIfExists($name)
30+
);
31+
}
32+
733
/**
834
* Determine if the given table exists.
935
*

src/Illuminate/Database/Schema/PostgresBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@
44

55
class PostgresBuilder extends Builder
66
{
7+
/**
8+
* Create a database in the schema.
9+
*
10+
* @param string $name
11+
* @return bool
12+
*/
13+
public function createDatabase($name)
14+
{
15+
return $this->connection->statement(
16+
$this->grammar->compileCreateDatabase($name, $this->connection)
17+
);
18+
}
19+
20+
/**
21+
* Drop a database from the schema if the database exists.
22+
*
23+
* @param string $name
24+
* @return bool
25+
*/
26+
public function dropDatabaseIfExists($name)
27+
{
28+
return $this->connection->statement(
29+
$this->grammar->compileDropDatabaseIfExists($name)
30+
);
31+
}
32+
733
/**
834
* Determine if the given table exists.
935
*

src/Illuminate/Database/Schema/SQLiteBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@
22

33
namespace Illuminate\Database\Schema;
44

5+
use Illuminate\Support\Facades\File;
6+
57
class SQLiteBuilder extends Builder
68
{
9+
/**
10+
* Create a database in the schema.
11+
*
12+
* @param string $name
13+
* @return bool
14+
*/
15+
public function createDatabase($name)
16+
{
17+
return File::put($name, '') !== false;
18+
}
19+
20+
/**
21+
* Drop a database from the schema if the database exists.
22+
*
23+
* @param string $name
24+
* @return bool
25+
*/
26+
public function dropDatabaseIfExists($name)
27+
{
28+
return File::exists($name)
29+
? File::delete($name)
30+
: true;
31+
}
32+
733
/**
834
* Drop all tables from the database.
935
*

src/Illuminate/Database/Schema/SqlServerBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@
44

55
class SqlServerBuilder extends Builder
66
{
7+
/**
8+
* Create a database in the schema.
9+
*
10+
* @param string $name
11+
* @return bool
12+
*/
13+
public function createDatabase($name)
14+
{
15+
return $this->connection->statement(
16+
$this->grammar->compileCreateDatabase($name, $this->connection)
17+
);
18+
}
19+
20+
/**
21+
* Drop a database from the schema if the database exists.
22+
*
23+
* @param string $name
24+
* @return bool
25+
*/
26+
public function dropDatabaseIfExists($name)
27+
{
28+
return $this->connection->statement(
29+
$this->grammar->compileDropDatabaseIfExists($name)
30+
);
31+
}
32+
733
/**
834
* Drop all tables from the database.
935
*

src/Illuminate/Foundation/Providers/FoundationServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Support\AggregateServiceProvider;
77
use Illuminate\Support\Facades\URL;
8+
use Illuminate\Testing\ParallelTestingServiceProvider;
89
use Illuminate\Validation\ValidationException;
910

1011
class FoundationServiceProvider extends AggregateServiceProvider
@@ -16,6 +17,7 @@ class FoundationServiceProvider extends AggregateServiceProvider
1617
*/
1718
protected $providers = [
1819
FormRequestServiceProvider::class,
20+
ParallelTestingServiceProvider::class,
1921
];
2022

2123
/**

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Console\Application as Artisan;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Support\Facades\Facade;
10+
use Illuminate\Support\Facades\ParallelTesting;
1011
use Illuminate\Support\Str;
1112
use Mockery;
1213
use Mockery\Exception\InvalidCountException;
@@ -81,6 +82,8 @@ protected function setUp(): void
8182

8283
if (! $this->app) {
8384
$this->refreshApplication();
85+
86+
ParallelTesting::callSetUpTestCaseCallbacks($this);
8487
}
8588

8689
$this->setUpTraits();
@@ -152,6 +155,8 @@ protected function tearDown(): void
152155
if ($this->app) {
153156
$this->callBeforeApplicationDestroyedCallbacks();
154157

158+
ParallelTesting::callTearDownTestCaseCallbacks($this);
159+
155160
$this->app->flush();
156161

157162
$this->app = null;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Illuminate\Support\Facades;
4+
5+
/**
6+
* @method static void setUpProcess(callable $callback)
7+
* @method static void setUpTestCase(callable $callback)
8+
* @method static void tearDownProcess(callable $callback)
9+
* @method static void tearDownTestCase(callable $callback)
10+
* @method static int|false token()
11+
*
12+
* @see \Illuminate\Testing\ParallelTesting
13+
*/
14+
class ParallelTesting extends Facade
15+
{
16+
/**
17+
* Get the registered name of the component.
18+
*
19+
* @return string
20+
*/
21+
protected static function getFacadeAccessor()
22+
{
23+
return \Illuminate\Testing\ParallelTesting::class;
24+
}
25+
}

src/Illuminate/Support/Facades/Schema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
/**
66
* @method static \Illuminate\Database\Schema\Builder create(string $table, \Closure $callback)
7+
* @method static \Illuminate\Database\Schema\Builder createDatabase(string $name)
78
* @method static \Illuminate\Database\Schema\Builder disableForeignKeyConstraints()
89
* @method static \Illuminate\Database\Schema\Builder drop(string $table)
10+
* @method static \Illuminate\Database\Schema\Builder dropDatabaseIfExists(string $name)
911
* @method static \Illuminate\Database\Schema\Builder dropIfExists(string $table)
1012
* @method static \Illuminate\Database\Schema\Builder enableForeignKeyConstraints()
1113
* @method static \Illuminate\Database\Schema\Builder rename(string $from, string $to)

0 commit comments

Comments
 (0)