Skip to content

Commit

Permalink
Merge pull request #3 from pascalbaljet/main
Browse files Browse the repository at this point in the history
Improve test suite
  • Loading branch information
onlime authored Mar 14, 2024
2 parents bb432ab + d040cc3 commit 78ad0cd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ jobs:
- name: Execute tests
run: |
vendor/bin/pest tests/Feature
vendor/bin/pest tests/Unit
vendor/bin/pest
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</report>
</coverage>
<testsuites>
<testsuite name="Feature">
<directory>./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
Expand Down
8 changes: 4 additions & 4 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;

class Formatter
Expand Down Expand Up @@ -53,7 +52,8 @@ public function getHeader(): string
$queryLog = DB::getRawQueryLog();
$times = Arr::pluck($queryLog, 'time');
$totalTime = $this->time(array_sum($times));
$ip = Request::ip();
$request = $this->app['request'];
$ip = $request->ip();

// TODO: datetime information should be replaced by lowest query timestamp, see https://github.com/laravel/framework/pull/37514
$data = [
Expand All @@ -63,10 +63,10 @@ public function getHeader(): string
'user' => $username,
'guard' => Auth::check() ? Auth::getDefaultDriver() : '',
'env' => $this->app->environment(),
'agent' => Request::userAgent() ?? PHP_SAPI,
'agent' => $request->userAgent() ?? PHP_SAPI,
'ip' => $ip,
'host' => gethostbyaddr($ip),
'referer' => Request::header('referer'),
'referer' => $request->header('referer'),
];
$headers = Arr::only($data, $headerFields);

Expand Down
17 changes: 9 additions & 8 deletions tests/Unit/FormatterTest.php → tests/Feature/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
$config->shouldReceive('headerFields')->once()->withNoArgs()
->andReturn(explode(',', 'origin,datetime,status,user,env,agent,ip,host,referer'));
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->times(2)->with('request')->andReturn($request);
$app->shouldReceive('offsetGet')->times(3)->with('request')->andReturn($request);
$request->shouldReceive('method')->once()->withNoArgs()->andReturn('DELETE');
$request->shouldReceive('fullUrl')->once()->withNoArgs()
->andReturn('http://example.com/test');
$request->shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
$request->shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
$request->shouldReceive('header')->once()->with('referer')->andReturn('');

$now = '2015-03-04 08:12:07';
Carbon::setTestNow($now);
Expand All @@ -32,9 +35,6 @@
]);
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');
\Illuminate\Support\Facades\Request::shouldReceive('header')->once()->with('referer')->andReturn('');

$formatter = new Formatter($app, $config);
$result = $formatter->getHeader();
Expand Down Expand Up @@ -65,17 +65,18 @@
$config->shouldReceive('headerFields')->once()->withNoArgs()
->andReturn(explode(',', 'origin,datetime,status,user,env,agent,ip,host,referer'));
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->once()->with('request')->andReturn($request);
$app->shouldReceive('offsetGet')->twice()->with('request')->andReturn($request);
$request->shouldReceive('server')->once()->with('argv', [])->andReturn('php artisan test');
$request->shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
$request->shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
$request->shouldReceive('header')->once()->with('referer')->andReturn('');

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

DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([]);
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');
\Illuminate\Support\Facades\Request::shouldReceive('header')->once()->with('referer')->andReturn('');

$formatter = new Formatter($app, $config);
$result = $formatter->getHeader();
Expand Down
File renamed without changes.

0 comments on commit 78ad0cd

Please sign in to comment.