SIGN IN SIGN UP
laravel / framework UNCLAIMED

Laravel is a web application framework with expressive, elegant syntax.

<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
use Illuminate\Database\Schema\PostgresBuilder;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class DatabasePostgresBuilderTest extends TestCase
{
[9.x] Adds support for Parallel Testing (#35778) * Adds parallel testing * Fixes "Class MysqlBuilder" not found * Updates wording when drivers do not support create/drop databases * Avoids "getenv" and "putenv" in favour of $_SERVER * Moves "createDatabaseIfNotExists" to "createDatabase" * Adds support to "Postgres" * Fixes CS * Uses a database per process regardless of the used database testing trait * Apply fixes from StyleCI * Removes unused catch * Refactors into service providers * Apply fixes from StyleCI * Removes token resolver from the public API * Renames Parallel Testing register callbacks * Adds "setUp" process and "tearDown" test case * Updates Parallel Testing facade * Suffixes _test_ on storage too * Fixes missing envs for process callbacks * Apply fixes from StyleCI * Ensures "drops" only happen when needed * Apply fixes from StyleCI * Adds support for SQLite file databases * Adds tests for SQLite file databases * Lower case create/drop statements * Adds support for SQL Server * Apply fixes from StyleCI * Adds tests against Parallel Testing callbacks * Adds tests for parallel console output * Apply fixes from StyleCI * Removes unused lines in tests * Calls Parallel Testing setUp callbacks after refresh app only * Cleans folders for parallel testing * Makes test databases persist * Apply fixes from StyleCI * Resolve parallel testing callbacks from container * Apply fixes from StyleCI * Update TestDatabases.php * Update ParallelTesting.php * Renames "refresh-databases" to "recreate-databases" Co-authored-by: Nuno Maduro <nunomaduro@users.noreply.github.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
2021-01-13 15:20:19 +01:00
public function testCreateDatabase()
{
$connection = m::mock(Connection::class);
$grammar = new PostgresGrammar($connection);
[9.x] Adds support for Parallel Testing (#35778) * Adds parallel testing * Fixes "Class MysqlBuilder" not found * Updates wording when drivers do not support create/drop databases * Avoids "getenv" and "putenv" in favour of $_SERVER * Moves "createDatabaseIfNotExists" to "createDatabase" * Adds support to "Postgres" * Fixes CS * Uses a database per process regardless of the used database testing trait * Apply fixes from StyleCI * Removes unused catch * Refactors into service providers * Apply fixes from StyleCI * Removes token resolver from the public API * Renames Parallel Testing register callbacks * Adds "setUp" process and "tearDown" test case * Updates Parallel Testing facade * Suffixes _test_ on storage too * Fixes missing envs for process callbacks * Apply fixes from StyleCI * Ensures "drops" only happen when needed * Apply fixes from StyleCI * Adds support for SQLite file databases * Adds tests for SQLite file databases * Lower case create/drop statements * Adds support for SQL Server * Apply fixes from StyleCI * Adds tests against Parallel Testing callbacks * Adds tests for parallel console output * Apply fixes from StyleCI * Removes unused lines in tests * Calls Parallel Testing setUp callbacks after refresh app only * Cleans folders for parallel testing * Makes test databases persist * Apply fixes from StyleCI * Resolve parallel testing callbacks from container * Apply fixes from StyleCI * Update TestDatabases.php * Update ParallelTesting.php * Renames "refresh-databases" to "recreate-databases" Co-authored-by: Nuno Maduro <nunomaduro@users.noreply.github.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
2021-01-13 15:20:19 +01:00
$connection->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8');
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('statement')->once()->with(
'create database "my_temporary_database" encoding "utf8"'
)->andReturn(true);
$builder = $this->getBuilder($connection);
$builder->createDatabase('my_temporary_database');
}
public function testDropDatabaseIfExists()
{
$connection = m::mock(Connection::class);
$grammar = new PostgresGrammar($connection);
[9.x] Adds support for Parallel Testing (#35778) * Adds parallel testing * Fixes "Class MysqlBuilder" not found * Updates wording when drivers do not support create/drop databases * Avoids "getenv" and "putenv" in favour of $_SERVER * Moves "createDatabaseIfNotExists" to "createDatabase" * Adds support to "Postgres" * Fixes CS * Uses a database per process regardless of the used database testing trait * Apply fixes from StyleCI * Removes unused catch * Refactors into service providers * Apply fixes from StyleCI * Removes token resolver from the public API * Renames Parallel Testing register callbacks * Adds "setUp" process and "tearDown" test case * Updates Parallel Testing facade * Suffixes _test_ on storage too * Fixes missing envs for process callbacks * Apply fixes from StyleCI * Ensures "drops" only happen when needed * Apply fixes from StyleCI * Adds support for SQLite file databases * Adds tests for SQLite file databases * Lower case create/drop statements * Adds support for SQL Server * Apply fixes from StyleCI * Adds tests against Parallel Testing callbacks * Adds tests for parallel console output * Apply fixes from StyleCI * Removes unused lines in tests * Calls Parallel Testing setUp callbacks after refresh app only * Cleans folders for parallel testing * Makes test databases persist * Apply fixes from StyleCI * Resolve parallel testing callbacks from container * Apply fixes from StyleCI * Update TestDatabases.php * Update ParallelTesting.php * Renames "refresh-databases" to "recreate-databases" Co-authored-by: Nuno Maduro <nunomaduro@users.noreply.github.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
2021-01-13 15:20:19 +01:00
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('statement')->once()->with(
'drop database if exists "my_database_a"'
)->andReturn(true);
$builder = $this->getBuilder($connection);
$builder->dropDatabaseIfExists('my_database_a');
}
public function testHasTableWhenSchemaUnqualifiedAndSearchPathMissing()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn(null);
$connection->shouldReceive('getConfig')->with('schema')->andReturn(null);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileTableExists')->andReturn('sql');
$connection->shouldReceive('scalar')->with('sql')->andReturn(1);
$connection->shouldReceive('getTablePrefix');
$builder = $this->getBuilder($connection);
$this->assertTrue($builder->hasTable('foo'));
$this->assertTrue($builder->hasTable('public.foo'));
}
public function testHasTableWhenSchemaUnqualifiedAndSearchPathFilled()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('myapp,public');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileTableExists')->andReturn('sql');
$connection->shouldReceive('scalar')->with('sql')->andReturn(1);
$connection->shouldReceive('getTablePrefix');
$builder = $this->getBuilder($connection);
$this->assertTrue($builder->hasTable('foo'));
$this->assertTrue($builder->hasTable('myapp.foo'));
}
public function testHasTableWhenSchemaUnqualifiedAndSearchPathFallbackFilled()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn(null);
$connection->shouldReceive('getConfig')->with('schema')->andReturn(['myapp', 'public']);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileTableExists')->andReturn('sql');
$connection->shouldReceive('scalar')->with('sql')->andReturn(1);
$connection->shouldReceive('getTablePrefix');
$builder = $this->getBuilder($connection);
$this->assertTrue($builder->hasTable('foo'));
$this->assertTrue($builder->hasTable('myapp.foo'));
}
public function testHasTableWhenSchemaUnqualifiedAndSearchPathIsUserVariable()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('username')->andReturn('foouser');
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('$user');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileTableExists')->andReturn('sql');
$connection->shouldReceive('scalar')->with('sql')->andReturn(1);
$connection->shouldReceive('getTablePrefix');
$builder = $this->getBuilder($connection);
$this->assertTrue($builder->hasTable('foo'));
$this->assertTrue($builder->hasTable('foouser.foo'));
}
public function testHasTableWhenSchemaQualifiedAndSearchPathMismatches()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileTableExists')->andReturn('sql');
$connection->shouldReceive('scalar')->with('sql')->andReturn(1);
$connection->shouldReceive('getTablePrefix');
$builder = $this->getBuilder($connection);
$this->assertTrue($builder->hasTable('myapp.foo'));
}
public function testHasTableWhenDatabaseAndSchemaQualifiedAndSearchPathMismatches()
{
$this->expectException(\InvalidArgumentException::class);
$connection = $this->getConnection();
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$builder = $this->getBuilder($connection);
$builder->hasTable('mydatabase.myapp.foo');
}
public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathMissing()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn(null);
$connection->shouldReceive('getConfig')->with('schema')->andReturn(null);
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileColumns')->with(null, 'foo')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'some_column']]);
$connection->shouldReceive('getTablePrefix');
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]);
$builder = $this->getBuilder($connection);
$builder->getColumnListing('foo');
}
public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathFilled()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('myapp,public');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileColumns')->with(null, 'foo')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'some_column']]);
$connection->shouldReceive('getTablePrefix');
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]);
$builder = $this->getBuilder($connection);
$builder->getColumnListing('foo');
}
public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathIsUserVariable()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('username')->andReturn('foouser');
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('$user');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileColumns')->with(null, 'foo')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'some_column']]);
$connection->shouldReceive('getTablePrefix');
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]);
$builder = $this->getBuilder($connection);
$builder->getColumnListing('foo');
}
public function testGetColumnListingWhenSchemaQualifiedAndSearchPathMismatches()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$grammar->shouldReceive('compileColumns')->with('myapp', 'foo')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'some_column']]);
$connection->shouldReceive('getTablePrefix');
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]);
$builder = $this->getBuilder($connection);
$builder->getColumnListing('myapp.foo');
}
public function testGetColumnWhenDatabaseAndSchemaQualifiedAndSearchPathMismatches()
{
$this->expectException(\InvalidArgumentException::class);
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
$grammar = m::mock(PostgresGrammar::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$builder = $this->getBuilder($connection);
$builder->getColumnListing('mydatabase.myapp.foo');
}
public function testDropAllTablesWhenSearchPathIsString()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$grammar->shouldReceive('compileTables')->andReturn('sql');
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'public', 'schema_qualified_name' => 'public.users']]);
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'public', 'schema_qualified_name' => 'public.users']]);
$grammar->shouldReceive('compileDropAllTables')->with(['public.users'])->andReturn('drop table "public"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "public"."users" cascade');
$builder = $this->getBuilder($connection);
$builder->dropAllTables();
}
public function testDropAllTablesWhenSearchPathIsStringOfMany()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('username')->andReturn('foouser');
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('"$user", public, foo_bar-Baz.Áüõß');
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'foouser', 'schema_qualified_name' => 'foouser.users']]);
$grammar->shouldReceive('compileTables')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'foouser', 'schema_qualified_name' => 'foouser.users']]);
$grammar->shouldReceive('compileDropAllTables')->with(['foouser.users'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);
$builder->dropAllTables();
}
public function testDropAllTablesWhenSearchPathIsArrayOfMany()
{
$connection = $this->getConnection();
$connection->shouldReceive('getConfig')->with('username')->andReturn('foouser');
$connection->shouldReceive('getConfig')->with('search_path')->andReturn([
'$user',
'"dev"',
"'test'",
'spaced schema',
]);
$connection->shouldReceive('getConfig')->with('dont_drop')->andReturn(['foo']);
$grammar = m::mock(PostgresGrammar::class);
$processor = m::mock(PostgresProcessor::class);
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'foouser', 'schema_qualified_name' => 'foouser.users']]);
$grammar->shouldReceive('compileTables')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'foouser', 'schema_qualified_name' => 'foouser.users']]);
$grammar->shouldReceive('compileDropAllTables')->with(['foouser.users'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);
$builder->dropAllTables();
}
protected function getConnection()
{
return m::mock(Connection::class);
}
protected function getBuilder($connection)
{
return new PostgresBuilder($connection);
}
protected function getGrammar()
{
return new PostgresGrammar;
}
}