Skip to content

Commit

Permalink
STDOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Mar 31, 2023
1 parent 4b56c78 commit d8b4bfc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 70 deletions.
6 changes: 0 additions & 6 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ protected function getGrammar(): Grammar

abstract protected function columnize(string $sql): string;

/**
* @param string $value
* @return string
*/
protected function parameterize(string $value): string
{
return $this->quoteString($this->escape($value));
}

/**
* @param string $sql
* @param string[] $columnQuotedIdentifiers
* @return string
*/
protected function replaceColumnQuotedIdentifiers(string $sql, array $columnQuotedIdentifiers): string
{
Expand Down
8 changes: 0 additions & 8 deletions src/Dumpers/PdoDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@

class PdoDumper extends Dumper
{
/**
* @param string $sql
* @return string
*/
protected function columnize(string $sql): string
{
return $sql;
}

/**
* @param string $value
* @return string
*/
protected function parameterize(string $value): string
{
return $this->pdo ? $this->pdo->quote($value) : parent::parameterize(addslashes($value));
Expand Down
1 change: 0 additions & 1 deletion src/EloquentDumperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ private function registerBuilderMicro($method, Closure $closure): void
}

/**
* @param string $name
* @return mixed
*/
private function getConfig(string $name)
Expand Down
15 changes: 14 additions & 1 deletion src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@

class ConsoleOutput implements OutputInterface
{
private static $callable;

public static function setEchoFunction(callable $callable): void
{
static::$callable = $callable;
}

public function output(string $sql): void
{
echo "\n".Dumper::format($sql)."\n";
$callable = is_callable(static::$callable) ? static::$callable : [static::class, 'defaultEchoFunction'];
$callable(Dumper::format($sql));
}

private static function defaultEchoFunction($sql): void
{
fwrite(STDOUT, $sql);
}
}
29 changes: 0 additions & 29 deletions tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class DumperTest extends TestCase
{
/**
* @dataProvider simpleProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_get_simple_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -46,9 +43,6 @@ public static function simpleProvider(): array

/**
* @dataProvider whereRawProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_get_where_raw_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -94,9 +88,6 @@ public static function whereRawProvider(): array

/**
* @dataProvider inProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_get_condition_in_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -126,9 +117,6 @@ public static function inProvider(): array

/**
* @dataProvider withoutQuoteGrammarProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_convert_to_without_quote_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -158,9 +146,6 @@ public static function withoutQuoteGrammarProvider(): array

/**
* @dataProvider mySQLGrammarProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_convert_to_mysql_version_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -190,9 +175,6 @@ public static function mySQLGrammarProvider(): array

/**
* @dataProvider sqliteGrammarProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_convert_to_sqlite_version_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -222,9 +204,6 @@ public static function sqliteGrammarProvider(): array

/**
* @dataProvider postgresGrammarProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_convert_to_postgres_version_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -254,9 +233,6 @@ public static function postgresGrammarProvider(): array

/**
* @dataProvider sqlServerGrammarProvider
*
* @param Builder $query
* @param string $expected
*/
public function test_it_should_convert_to_sqlserver_version_sql(Builder $query, string $expected): void
{
Expand Down Expand Up @@ -284,11 +260,6 @@ public static function sqlServerGrammarProvider(): array
]];
}

/**
* @param string $expected
* @param Dumper $dumper
* @param Builder $query
*/
private function assertSql(string $expected, Dumper $dumper, Builder $query): void
{
self::assertEquals(
Expand Down
48 changes: 23 additions & 25 deletions tests/EloquentDumperServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Orchestra\Testbench\TestCase;
use org\bovigo\vfs\vfsStream;
use Recca0120\EloquentDumper\EloquentDumperServiceProvider;
use Recca0120\EloquentDumper\Output\ConsoleOutput;

class EloquentDumperServiceProviderTest extends TestCase
{
Expand All @@ -17,6 +18,9 @@ class EloquentDumperServiceProviderTest extends TestCase
*/
public function test_dump_sql(string $grammar, string $excepted, string $exceptedOutput): void
{
ConsoleOutput::setEchoFunction(static function ($sql) {
echo $sql;
});
$this->setGrammar($grammar);
$query = User::where('name', 'foo')->where('password', 'bar');
$sql = $query->toRawSql();
Expand All @@ -40,23 +44,25 @@ public function test_log_sql(string $grammar, string $excepted): void

public static function sqlProvider(): array
{
return [[
'mysql',
'select * from `users` where `name` = \'foo\' and `password` = \'bar\'',
'select * from `users` where `name` = \'foo\' and `password` = \'bar\'',
], [
'sqlite',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
], [
'pgsql',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
], [
'sqlsrv',
'select * from [users] where [name] = \'foo\' and [password] = \'bar\'',
'select * from [users] where [name] = \'foo\' and [password] = \'bar\'',
]];
return [
[
'mysql',
'select * from `users` where `name` = \'foo\' and `password` = \'bar\'',
'select * from `users` where `name` = \'foo\' and `password` = \'bar\'',
], [
'sqlite',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
], [
'pgsql',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
'select * from "users" where "name" = \'foo\' and "password" = \'bar\'',
], [
'sqlsrv',
'select * from [users] where [name] = \'foo\' and [password] = \'bar\'',
'select * from [users] where [name] = \'foo\' and [password] = \'bar\'',
],
];
}

protected function getEnvironmentSetUp($app)
Expand All @@ -76,19 +82,11 @@ protected function getPackageProviders($app): array
return [EloquentDumperServiceProvider::class];
}

/**
* @param string $grammar
* @return void
*/
private function setGrammar(string $grammar): void
{
$this->app['config']->set('eloquent-dumper.grammar', $grammar);
}

/**
* @param Builder $query
* @return string
*/
private function dumpSql(Builder $query): string
{
ob_start();
Expand Down

0 comments on commit d8b4bfc

Please sign in to comment.