Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uniqueness of macroable objects on grammars #26461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Illuminate\Database;

use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Query\Expression;

abstract class Grammar
{
use Macroable;

/**
* The grammar table prefix.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Query\JsonExpression;

class MySqlGrammar extends Grammar
{
use Macroable;

/**
* The grammar specific operators.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Traits\Macroable;

class PostgresGrammar extends Grammar
{
use Macroable;

/**
* All of the available clause operators.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Traits\Macroable;

class SQLiteGrammar extends Grammar
{
use Macroable;

/**
* The components that make up a select clause.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Illuminate\Support\Arr;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Traits\Macroable;

class SqlServerGrammar extends Grammar
{
use Macroable;

/**
* All of the available clause operators.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Schema\Blueprint;

class MySqlGrammar extends Grammar
{
use Macroable;

/**
* The possible column modifiers.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Illuminate\Database\Schema\Grammars;

use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Schema\Blueprint;

class PostgresGrammar extends Grammar
{
use Macroable;

/**
* If this Grammar supports schema changes wrapped in a transaction.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Illuminate\Support\Fluent;
use Doctrine\DBAL\Schema\Index;
use Illuminate\Database\Connection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Schema\Blueprint;

class SQLiteGrammar extends Grammar
{
use Macroable;

/**
* The possible column modifiers.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Illuminate\Database\Schema\Grammars;

use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Schema\Blueprint;

class SqlServerGrammar extends Grammar
{
use Macroable;

/**
* If this Grammar supports schema changes wrapped in a transaction.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/Database/DatabaseGrammarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Illuminate\Tests\Database;

use PHPUnit\Framework\TestCase;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SqlServerSchemaGrammar;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as PostgresSchemaGrammar;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as MySqlSchemaGrammar;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SQLiteSchemaGrammar;
use Illuminate\Database\Query\Grammars\MySqlGrammar as MySqlQueryGrammar;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as SQLiteQueryGrammar;
use Illuminate\Database\Query\Grammars\SqlServerGrammar as SqlServerQueryGrammar;
use Illuminate\Database\Query\Grammars\PostgresGrammar as PostgresQueryGrammar;

class DatabaseGrammarTest extends TestCase
{
public function testMacroableFunctionsAreUniquePerGrammarQueryClass()
{
// compileReplace macro.
MySqlQueryGrammar::macro('compileReplace', function () {
return MySqlQueryGrammar::class;
});

SQLiteQueryGrammar::macro('compileReplace', function () {
return SQLiteQueryGrammar::class;
});

PostgresQueryGrammar::macro('compileReplace', function () {
return PostgresQueryGrammar::class;
});

SqlServerQueryGrammar::macro('compileReplace', function () {
return SqlServerQueryGrammar::class;
});

$this->assertSame(MySqlQueryGrammar::class, (new MySqlQueryGrammar())->compileReplace());
$this->assertSame(SqlServerQueryGrammar::class, (new SqlServerQueryGrammar())->compileReplace());
$this->assertSame(PostgresQueryGrammar::class, (new PostgresQueryGrammar())->compileReplace());
$this->assertSame(SQLiteQueryGrammar::class, (new SQLiteQueryGrammar())->compileReplace());
}

public function testMacroableFunctionsAreUniquePerGrammarSchemaClass()
{
// compileReplace macro.
MySqlSchemaGrammar::macro('compileReplace', function () {
return MySqlSchemaGrammar::class;
});

SQLiteSchemaGrammar::macro('compileReplace', function () {
return SQLiteSchemaGrammar::class;
});

PostgresSchemaGrammar::macro('compileReplace', function () {
return PostgresSchemaGrammar::class;
});

SqlServerSchemaGrammar::macro('compileReplace', function () {
return SqlServerSchemaGrammar::class;
});

$this->assertSame(MySqlSchemaGrammar::class, (new MySqlSchemaGrammar())->compileReplace());
$this->assertSame(SqlServerSchemaGrammar::class, (new SqlServerSchemaGrammar())->compileReplace());
$this->assertSame(PostgresSchemaGrammar::class, (new PostgresSchemaGrammar())->compileReplace());
$this->assertSame(SQLiteSchemaGrammar::class, (new SQLiteSchemaGrammar())->compileReplace());
}
}