Skip to content

Commit

Permalink
Merge branch 'master' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Mar 14, 2022
2 parents 46c6e78 + 62716d3 commit 8523eec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 83 deletions.
35 changes: 10 additions & 25 deletions src/DbProfilerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@ class DbProfilerServiceProvider extends ServiceProvider
{
/**
* The query counter.
*
* @var int
*/
private $counter = 1;
private int $counter = 1;

/**
* Register the service provider.
*
* @return void
*/
public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/db-profiler.php', 'db-profiler');
}

/**
* Boot the service provider.
*
* @return void
*
* @noinspection ForgottenDebugOutputInspection
*/
public function boot()
public function boot(): void
{
if (!$this->isEnabled()) {
return;
Expand All @@ -50,10 +44,8 @@ public function boot()

/**
* Check whether database profiling is enabled or not.
*
* @return bool
*/
private function isEnabled()
private function isEnabled(): bool
{
if (!$this->app->isLocal() && !config('db-profiler.force')) {
return false;
Expand All @@ -66,22 +58,15 @@ private function isEnabled()

/**
* Apply query bindings to the given SQL query.
*
* @param string $sql
* @param array $bindings
* @return string
*/
private function applyQueryBindings(string $sql, array $bindings)
private function applyQueryBindings(string $sql, array $bindings): string
{
$bindings = collect($bindings)->map(function ($binding) {
switch (gettype($binding)) {
case 'boolean':
return (int) $binding;
case 'string':
return "'{$binding}'";
default:
return $binding;
}
return match (gettype($binding)) {
'boolean' => (int) $binding,
'string' => "'{$binding}'",
default => $binding,
};
})->toArray();

return Str::replaceArray('?', $bindings, $sql);
Expand Down
8 changes: 2 additions & 6 deletions tests/ConsoleProfilingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ class ConsoleProfilingTest extends TestCase
{
/**
* Define whether the app is running in console or not.
*
* @return bool
*/
protected function runningInConsole()
protected function runningInConsole(): bool
{
return true;
}

/**
* Emulate the "vvv" flag set.
*
* @return $this
*/
protected function withVvv()
protected function withVvv(): self
{
$_SERVER['argv']['-vvv'] = true;

Expand Down
8 changes: 2 additions & 6 deletions tests/HttpProfilingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ class HttpProfilingTest extends TestCase
{
/**
* Define whether the app is running in console or not.
*
* @return bool
*/
protected function runningInConsole()
protected function runningInConsole(): bool
{
return false;
}

/**
* Emulate the "vvv" flag set.
*
* @return $this
*/
protected function withVvv()
protected function withVvv(): self
{
Request::merge(['vvv' => true]);

Expand Down
49 changes: 11 additions & 38 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase

/**
* The emulated environment.
*
* @var string
*/
private $env;
private string $env;

/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
Expand All @@ -44,10 +40,8 @@ protected function setUp(): void

/**
* Setup the database.
*
* @return void
*/
private function setUpDatabase()
private function setUpDatabase(): void
{
config(['database.default' => 'testing']);
config(['database.connections.testing.foreign_key_constraints' => true]);
Expand All @@ -61,10 +55,8 @@ private function setUpDatabase()

/**
* Emulate the local environment.
*
* @return $this
*/
protected function local()
protected function local(): self
{
$this->env = 'local';

Expand All @@ -73,10 +65,8 @@ protected function local()

/**
* Emulate the non-local environment.
*
* @return $this
*/
protected function notLocal()
protected function notLocal(): self
{
$this->env = 'production';

Expand All @@ -85,24 +75,18 @@ protected function notLocal()

/**
* Define whether the app is running in console or not.
*
* @return bool
*/
abstract protected function runningInConsole();
abstract protected function runningInConsole(): bool;

/**
* Emulate the "vvv" flag set.
*
* @return $this
*/
abstract protected function withVvv();
abstract protected function withVvv(): self;

/**
* Emulate the app boot.
*
* @return void
*/
protected function boot()
protected function boot(): void
{
$app = mock(Application::class);
$app->expects('isLocal')->andReturn($this->env == 'local');
Expand All @@ -116,10 +100,8 @@ protected function boot()

/**
* Assert that the database profiler is activated.
*
* @return void
*/
protected function assertDbProfilerActivated()
protected function assertDbProfilerActivated(): void
{
/** @var \Illuminate\Database\Connection $connection */
$connection = DB::connection();
Expand All @@ -129,10 +111,8 @@ protected function assertDbProfilerActivated()

/**
* Assert that the database profiler is not activated.
*
* @return void
*/
protected function assertDbProfilerNotActivated()
protected function assertDbProfilerNotActivated(): void
{
/** @var \Illuminate\Database\Connection $connection */
$connection = DB::connection();
Expand All @@ -142,10 +122,8 @@ protected function assertDbProfilerNotActivated()

/**
* Assert that the database queries are dumped.
*
* @return void
*/
protected function assertDbQueriesDumped()
protected function assertDbQueriesDumped(): void
{
$queries = collect([
'[1]: select * from "posts"',
Expand Down Expand Up @@ -194,11 +172,8 @@ protected function assertDbQueriesDumped()

/**
* Prepare the query pattern for mocking.
*
* @param string $query
* @return string
*/
private function prepareQueryPattern(string $query)
private function prepareQueryPattern(string $query): string
{
$query = preg_quote($query, '/');

Expand All @@ -207,8 +182,6 @@ private function prepareQueryPattern(string $query)

/**
* Clean up the testing environment before the next test.
*
* @return void
*/
protected function tearDown(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
return new class extends Migration
{
/**
* Run the migration.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand All @@ -24,11 +22,9 @@ public function up()

/**
* Rollback the migration.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::drop('posts');
}
}
};

0 comments on commit 8523eec

Please sign in to comment.