Skip to content

Commit

Permalink
Remove authenticated user information for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mnabialek committed Sep 9, 2018
1 parent 8176fd9 commit 5fad648
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 104 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ All notable changes to this project will be documented in this file.
## [2.2.0] - 2018-09-09
### Added
- Possibility to use custom entry format
- Possibility to insert authenticated user information into entry

## [2.1.0] - 2018-07-17
### Added
Expand Down
4 changes: 0 additions & 4 deletions publish/config/sql_logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
* - [query_time] - how long query was executed
* - [query] - query itself
* - [separator] - extra separator line to make it easier to see where next query starts
* - [user_*] - column for user. If you want to get id of user use [user_id], if you want to
* get user email user [user_email] and so on. Text after underscore should
* match column name from database ($user->email). If there is no authenticated
* user then instead of value - sign will be used
* - \n - new line separator.
*/
'entry_format' => env('SQL_LOGGER_FORMAT_ENTRY_FORMAT', "/* [origin]\n Query [query_nr] - [datetime] [[query_time]] */\n[query]\n[separator]\n"),
Expand Down
6 changes: 1 addition & 5 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ public function getLine(SqlQuery $query)
'\n' => PHP_EOL,
];

$entry = str_replace(array_keys($replace), array_values($replace), $this->config->entryFormat());

return preg_replace_callback('(\[user_(\w*)\])', function ($matches) {
return ($user = $this->app['auth']->user()) ? $user->{$matches[1]} : '-';
}, $entry);
return str_replace(array_keys($replace), array_values($replace), $this->config->entryFormat());
}

/**
Expand Down
94 changes: 0 additions & 94 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Mnabialek\LaravelSqlLogger\Formatter;
use Mnabialek\LaravelSqlLogger\Objects\SqlQuery;
use Mockery;
use stdClass;

class FormatterTest extends UnitTestCase
{
Expand Down Expand Up @@ -92,99 +91,6 @@ public function it_formats_line_in_valid_way_when_custom_entry_format_was_used()
{$sql};
/*==================================================*/
EOT;

$this->assertSame($expected, $result);
}

/** @test */
public function it_adds_user_info_when_user_is_logged_and_user_info_requested()
{
$config = Mockery::mock(Config::class);
$app = Mockery::mock(Container::class, ArrayAccess::class);
$config->shouldReceive('useSeconds')->once()->withNoArgs()->andReturn(false);
$config->shouldReceive('newLinesToSpaces')->once()->withNoArgs()->andReturn(true);
$config->shouldReceive('entryFormat')->once()->withNoArgs()
->andReturn("[separator]\n[query_nr] : [datetime] [query_time]\nUser id: [user_id], e-mail: [user_email], name: [user_name]\n[origin]\n[query]\n[separator]\n");
$app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(false);
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->times(2)->with('request')->andReturn($request);
$request->shouldReceive('method')->once()->withNoArgs()->andReturn('DELETE');
$request->shouldReceive('fullUrl')->once()->withNoArgs()
->andReturn('http://example.com/test');
$authManager = Mockery::mock(stdClass::class);
$app->shouldReceive('offsetGet')->with('auth')->andReturn($authManager);
$user = (object) ['id' => 15, 'email' => 'sample-user-email@example.com', 'name' => 'Test user'];
$authManager->shouldReceive('user')->andReturn($user);

$now = '2015-03-04 08:12:07';
Carbon::setTestNow($now);

$query = Mockery::mock(SqlQuery::class);
$number = 434;
$time = 617.24;
$sql = 'SELECT * FROM somewhere';
$query->shouldReceive('number')->once()->withNoArgs()->andReturn($number);
$query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql);
$query->shouldReceive('time')->once()->withNoArgs()->andReturn($time);

$formatter = new Formatter($app, $config);
$result = $formatter->getLine($query);

$expected = <<<EOT
/*==================================================*/
{$number} : {$now} {$time}ms
User id: 15, e-mail: sample-user-email@example.com, name: Test user
Origin (request): DELETE http://example.com/test
{$sql};
/*==================================================*/
EOT;

$this->assertSame($expected, $result);
}

/** @test */
public function it_adds_user_info_when_user_is_not_logged_and_user_info_requested()
{
$config = Mockery::mock(Config::class);
$app = Mockery::mock(Container::class, ArrayAccess::class);
$config->shouldReceive('useSeconds')->once()->withNoArgs()->andReturn(false);
$config->shouldReceive('newLinesToSpaces')->once()->withNoArgs()->andReturn(true);
$config->shouldReceive('entryFormat')->once()->withNoArgs()
->andReturn("[separator]\n[query_nr] : [datetime] [query_time]\nUser id: [user_id], e-mail: [user_email], name: [user_name]\n[origin]\n[query]\n[separator]\n");
$app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(false);
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->times(2)->with('request')->andReturn($request);
$request->shouldReceive('method')->once()->withNoArgs()->andReturn('DELETE');
$request->shouldReceive('fullUrl')->once()->withNoArgs()
->andReturn('http://example.com/test');
$authManager = Mockery::mock(stdClass::class);
$app->shouldReceive('offsetGet')->with('auth')->andReturn($authManager);
$authManager->shouldReceive('user')->andReturn(null);

$now = '2015-03-04 08:12:07';
Carbon::setTestNow($now);

$query = Mockery::mock(SqlQuery::class);
$number = 434;
$time = 617.24;
$sql = 'SELECT * FROM somewhere';
$query->shouldReceive('number')->once()->withNoArgs()->andReturn($number);
$query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql);
$query->shouldReceive('time')->once()->withNoArgs()->andReturn($time);

$formatter = new Formatter($app, $config);
$result = $formatter->getLine($query);

$expected = <<<EOT
/*==================================================*/
{$number} : {$now} {$time}ms
User id: -, e-mail: -, name: -
Origin (request): DELETE http://example.com/test
{$sql};
/*==================================================*/
EOT;

$this->assertSame($expected, $result);
Expand Down

0 comments on commit 5fad648

Please sign in to comment.