Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Mar 11, 2024
1 parent eb1e196 commit 2a46789
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 136 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"illuminate/container": "^10.15|^11.0"
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"laravel/pint": "^1.14",
"mockery/mockery": "^1.0",
"laravel/framework": "^10.15|^11.0",
"laravel/pint": "^1.14"
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.2"
},
"autoload": {
"psr-4": {
Expand All @@ -38,7 +38,7 @@
"extra": {
"laravel": {
"providers": [
"Onlime\\LaravelSqlReporter\\Providers\\EventServiceProvider"
"Onlime\\LaravelSqlReporter\\Providers\\SqlReporterServiceProvider"
]
},
"thanks": {
Expand All @@ -50,4 +50,4 @@
"symfony/thanks": "Star packages you use running 'composer thanks' command"
}
}
}
}
6 changes: 3 additions & 3 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getHeader(): string
$formatted[] = $this->separatorLine();
$maxKeyLength = max(array_map('strlen', array_keys($headers)));
foreach ($headers as $key => $value) {
$formatted[] = '-- '.Str::padRight(Str::ucfirst($key).':', $maxKeyLength + 2).$value;
$formatted[] = trim('-- '.Str::padRight(Str::ucfirst($key).':', $maxKeyLength + 2).$value);
}
$formatted[] = $this->separatorLine();

Expand All @@ -106,8 +106,8 @@ protected function time(float $time): string
protected function originLine(): string
{
return $this->app->runningInConsole()
? '(console) '.$this->getArtisanLine()
: '(request) '.$this->getRequestLine();
? '(console) '.$this->getArtisanLine()
: '(request) '.$this->getRequestLine();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Onlime\LaravelSqlReporter\Listeners;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Onlime\LaravelSqlReporter\SqlLogger;

class SqlQueryLogSubscriber
class LogSqlQuery
{
/**
* Create the event listener.
Expand All @@ -17,24 +16,10 @@ public function __construct(
) {
}

/**
* Register the listeners for the subscriber.
*/
public function subscribe(Dispatcher $events): void
{
$events->listen(
[
RequestHandled::class,
CommandFinished::class,
],
[self::class, 'handle']
);
}

/**
* Handle the event.
*/
public function handle($event): void
public function handle(CommandFinished|RequestHandled $event): void
{
$this->logger->log();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

namespace Onlime\LaravelSqlReporter\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Onlime\LaravelSqlReporter\Config;
use Onlime\LaravelSqlReporter\Listeners\SqlQueryLogSubscriber;
use Onlime\LaravelSqlReporter\Listeners\LogSqlQuery;

class EventServiceProvider extends ServiceProvider
class SqlReporterServiceProvider extends ServiceProvider
{
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
SqlQueryLogSubscriber::class,
];

protected Config $config;

/**
Expand All @@ -30,8 +24,7 @@ public function register()
$this->mergeConfigFrom($this->configFileLocation(), 'sql-reporter');

if ($this->config->queriesEnabled()) {
// registering subscriber(s) from $this->subscribe
parent::register();
Event::listen([CommandFinished::class, RequestHandled::class], LogSqlQuery::class);
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/FeatureTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit;

use Carbon\Carbon;
use Mockery;
use Onlime\LaravelSqlReporter\Providers\SqlReporterServiceProvider;
use Orchestra\Testbench\TestCase;

class FeatureTestCase extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
Carbon::setTestNow();
}

protected function getPackageProviders($app)
{
return [SqlReporterServiceProvider::class];
}
}
9 changes: 5 additions & 4 deletions tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function it_formats_header_in_valid_way_when_running_via_http()
['raw_query' => 'foo', 'time' => 1.23],
['raw_query' => 'bar', 'time' => 4.56],
]);
Auth::shouldReceive('check')->once()->withNoArgs()->andReturn(false);
Auth::shouldReceive('user')->once()->withNoArgs()->andReturn(null);
\Illuminate\Support\Facades\Request::shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
\Illuminate\Support\Facades\Request::shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
Expand All @@ -50,12 +51,12 @@ public function it_formats_header_in_valid_way_when_running_via_http()
-- Datetime: {$now}
-- Origin: (request) DELETE http://example.com/test
-- Status: Executed 2 queries in 5.79ms
-- User:
-- User:
-- Env: testing
-- Agent: Mozilla/5.0
-- Ip: 127.0.0.1
-- Host: localhost
-- Referer:
-- Referer:
-- --------------------------------------------------
EOT;

Expand Down Expand Up @@ -93,12 +94,12 @@ public function it_formats_header_in_valid_way_when_running_in_console()
-- Datetime: {$now}
-- Origin: (console) php artisan test
-- Status: Executed 0 queries in 0ms
-- User:
-- User:
-- Env: testing
-- Agent: Mozilla/5.0
-- Ip: 127.0.0.1
-- Host: localhost
-- Referer:
-- Referer:
-- --------------------------------------------------
EOT;

Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/LogSqlQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Unit;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Mockery;
use Onlime\LaravelSqlReporter\Listeners\LogSqlQuery;
use Onlime\LaravelSqlReporter\SqlLogger;

class LogSqlQueryTest extends UnitTestCase
{
/**
* @var SqlLogger
*/
private $logger;

protected function setUp(): void
{
$this->logger = Mockery::spy(SqlLogger::class);
}

/** @test */
public function it_can_handle_the_command_finished_event()
{
$listener = new LogSqlQuery($this->logger);
$listener->handle(Mockery::mock(CommandFinished::class));

$this->logger->shouldHaveReceived('log')->once();
}

/** @test */
public function it_can_handle_the_request_handled_event()
{
$listener = new LogSqlQuery($this->logger);
$listener->handle(Mockery::mock(RequestHandled::class));

$this->logger->shouldHaveReceived('log')->once();
}
}
26 changes: 0 additions & 26 deletions tests/Unit/SqlQueryLogSubscriberTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Illuminate\Container\Container;
use Mockery;
use Onlime\LaravelSqlReporter\Config;
use Onlime\LaravelSqlReporter\Providers\EventServiceProvider;
use Onlime\LaravelSqlReporter\Providers\SqlReporterServiceProvider;
use Onlime\LaravelSqlReporter\SqlLogger;

class EventServiceProviderTest extends UnitTestCase
class SqlReporterServiceProviderTest extends UnitTestCase
{
/** @test */
public function it_merges_config_and_publishes_when_nothing_should_be_logged()
Expand All @@ -19,7 +19,7 @@ public function it_merges_config_and_publishes_when_nothing_should_be_logged()

$app->shouldReceive('make')->once()->with(Config::class)->andReturn($config);

$provider = Mockery::mock(EventServiceProvider::class)->makePartial()
$provider = Mockery::mock(SqlReporterServiceProvider::class)->makePartial()
->shouldAllowMockingProtectedMethods();
$provider->__construct($app);

Expand Down
Loading

0 comments on commit 2a46789

Please sign in to comment.