Skip to content

Commit ccd3b3f

Browse files
committed
adding unit tests
1 parent 29d9359 commit ccd3b3f

File tree

11 files changed

+956
-6
lines changed

11 files changed

+956
-6
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22
/vendor/
33
/.idea
44
/composer.lock
5-
6-
# TODO: fix tests and add them to repo
7-
/tests
8-
/phpunit.xml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $ php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider"
6565

6666
### How does this package differ from `mnabialek/laravel-sql-logger` ?
6767

68-
This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) and basically, it does the same thing: Logging your SQL queries. Here's the difference:
68+
This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) and basically does the same thing: Logging your SQL queries. Here's the difference:
6969
7070
- Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events.
7171
- This allows us to include much more information about the whole query executions like total query count, total execution time, and very detailed header information like origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname.

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
3+
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
5+
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
6+
<coverage processUncoveredFiles="true">
7+
<include>
8+
<directory suffix=".php">./src</directory>
9+
</include>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="LaravelSqlReporter">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/Formatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getHeader()
6969
'origin' => $this->originLine(),
7070
'status' => sprintf('Executed %s queries in %s', count($queryLog), $totalTime),
7171
'user' => Auth::user()?->username(),
72-
'env' => App::environment(),
72+
'env' => $this->app->environment(),
7373
'agent' => Request::userAgent() ?? PHP_SAPI,
7474
'ip' => $ip,
7575
'host' => gethostbyaddr($ip),

tests/ConfigTest.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Onlime\LaravelSqlReporter\Tests;
4+
5+
use Illuminate\Contracts\Config\Repository;
6+
use Onlime\LaravelSqlReporter\Config;
7+
use Mockery;
8+
9+
class ConfigTest extends UnitTestCase
10+
{
11+
/**
12+
* @var Repository|\Mockery\Mock
13+
*/
14+
protected $repository;
15+
16+
/**
17+
* @var Config|\Mockery\Mock
18+
*/
19+
protected $config;
20+
21+
protected function setUp(): void
22+
{
23+
$this->repository = Mockery::mock(Repository::class);
24+
$this->config = new Config($this->repository);
25+
}
26+
27+
/** @test */
28+
public function it_returns_valid_values_for_queriesEnabled()
29+
{
30+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.enabled')
31+
->andReturn(1);
32+
$this->assertTrue($this->config->queriesEnabled());
33+
34+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.enabled')
35+
->andReturn(0);
36+
$this->assertFalse($this->config->queriesEnabled());
37+
}
38+
39+
/** @test */
40+
public function it_returns_valid_value_for_slowLogTime()
41+
{
42+
$value = 700.0;
43+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.min_exec_time')
44+
->andReturn($value);
45+
$this->assertSame($value, $this->config->queriesMinExecTime());
46+
}
47+
48+
/** @test */
49+
public function it_returns_valid_values_for_queriesOverrideLog()
50+
{
51+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.override_log')
52+
->andReturn(1);
53+
$this->assertTrue($this->config->queriesOverrideLog());
54+
55+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.override_log')
56+
->andReturn(0);
57+
$this->assertFalse($this->config->queriesOverrideLog());
58+
}
59+
60+
/** @test */
61+
public function it_returns_valid_value_for_logDirectory()
62+
{
63+
$value = 'sample directory';
64+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.directory')
65+
->andReturn($value);
66+
$this->assertSame($value, $this->config->logDirectory());
67+
}
68+
69+
/** @test */
70+
public function it_returns_valid_values_for_useSeconds()
71+
{
72+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.use_seconds')
73+
->andReturn(1);
74+
$this->assertTrue($this->config->useSeconds());
75+
76+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.use_seconds')
77+
->andReturn(0);
78+
$this->assertFalse($this->config->useSeconds());
79+
}
80+
81+
/** @test */
82+
public function it_returns_valid_values_for_consoleSuffix()
83+
{
84+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.console_log_suffix')
85+
->andReturn('-artisan');
86+
$this->assertSame('-artisan', $this->config->consoleSuffix());
87+
88+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.console_log_suffix')
89+
->andReturn('');
90+
$this->assertSame('', $this->config->consoleSuffix());
91+
}
92+
93+
/** @test */
94+
public function it_returns_valid_value_for_queriesPattern()
95+
{
96+
$value = 'sample pattern';
97+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.pattern')
98+
->andReturn($value);
99+
$this->assertSame($value, $this->config->queriesPattern());
100+
}
101+
102+
/** @test */
103+
public function it_returns_valid_file_extension()
104+
{
105+
$value = '.sql';
106+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.general.extension')
107+
->andReturn($value);
108+
$this->assertSame($value, $this->config->fileExtension());
109+
}
110+
111+
/** @test */
112+
public function it_returns_valid_queries_file_name()
113+
{
114+
$value = '[Y-m-d]-sample';
115+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.file_name')
116+
->andReturn($value);
117+
$this->assertSame($value, $this->config->queriesFileName());
118+
}
119+
120+
/** @test */
121+
public function it_returns_valid_value_for_entryFormat()
122+
{
123+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.formatting.entry_format')
124+
->andReturn('[sample]/[example]/foo');
125+
$this->assertSame('[sample]/[example]/foo', $this->config->entryFormat());
126+
}
127+
}

tests/EventServiceProviderTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Onlime\LaravelSqlReporter\Tests;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Database\DatabaseManager;
7+
use Onlime\LaravelSqlReporter\Config;
8+
use Onlime\LaravelSqlReporter\Providers\EventServiceProvider;
9+
use Onlime\LaravelSqlReporter\SqlLogger;
10+
use Mockery;
11+
12+
class EventServiceProviderTest extends UnitTestCase
13+
{
14+
/** @test */
15+
public function it_merges_config_and_publishes_when_nothing_should_be_logged()
16+
{
17+
$app = Mockery::mock(Container::class, \ArrayAccess::class);
18+
Container::setInstance($app);
19+
$config = Mockery::mock(Config::class);
20+
21+
$app->shouldReceive('make')->once()->with(Config::class)->andReturn($config);
22+
23+
$provider = Mockery::mock(EventServiceProvider::class)->makePartial()
24+
->shouldAllowMockingProtectedMethods();
25+
$provider->__construct($app);
26+
27+
$baseDir = '/some/sample/directory';
28+
29+
// $app->shouldReceive('configFileLocation')->atLeast()->once()
30+
// ->withNoArgs()->andReturn($baseDir . '/sql-reporter.php');
31+
32+
$configFile = realpath(__DIR__ . '/../config/sql-reporter.php');
33+
$provider->shouldReceive('mergeConfigFrom')->once()->with(
34+
$configFile,
35+
'sql-reporter'
36+
);
37+
38+
$config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(false);
39+
40+
$app->shouldNotReceive('make')->with(SqlLogger::class);
41+
42+
$provider->register();
43+
$this->assertTrue(true);
44+
45+
// $provider->boot();
46+
// $provider->shouldReceive('publishes')->once()->with(
47+
// [$configFile => config_path('sql-reporter.php')], 'config'
48+
// );
49+
}
50+
}

tests/FileNameTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Onlime\LaravelSqlReporter\Tests;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Container\Container;
7+
use Onlime\LaravelSqlReporter\Config;
8+
use Onlime\LaravelSqlReporter\FileName;
9+
use Mockery;
10+
11+
class FileNameTest extends UnitTestCase
12+
{
13+
/**
14+
* @var Container|\Mockery\Mock
15+
*/
16+
protected $app;
17+
18+
/**
19+
* @var Config|\Mockery\Mock
20+
*/
21+
protected $config;
22+
23+
/**
24+
* @var FileName
25+
*/
26+
protected $filename;
27+
28+
protected function setUp(): void
29+
{
30+
Carbon::setTestNow('2015-03-07 08:16:09');
31+
$this->app = Mockery::mock(Container::class);
32+
$this->config = Mockery::mock(Config::class);
33+
$this->filename = new FileName($this->app, $this->config);
34+
}
35+
36+
/** @test */
37+
public function it_returns_valid_file_name_for_queries_when_not_running_in_console()
38+
{
39+
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(false);
40+
$this->config->shouldReceive('queriesFileName')->once()->withNoArgs()
41+
->andReturn('sample[Y]-test-[m]-abc-[d]');
42+
$this->config->shouldReceive('fileExtension')->once()->withNoArgs()
43+
->andReturn('.extension');
44+
$result = $this->filename->getLogfile();
45+
$this->assertSame('sample2015-test-03-abc-07.extension', $result);
46+
}
47+
48+
/** @test */
49+
public function it_returns_valid_file_name_for_queries_when_running_in_console()
50+
{
51+
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(true);
52+
$this->config->shouldReceive('consoleSuffix')->once()->withNoArgs()
53+
->andReturn('-artisan-suffix');
54+
$this->config->shouldReceive('queriesFileName')->once()->withNoArgs()
55+
->andReturn('sample[Y]-test-[m]-abc-[d]');
56+
$this->config->shouldReceive('fileExtension')->once()->withNoArgs()
57+
->andReturn('.extension');
58+
$result = $this->filename->getLogfile();
59+
$this->assertSame('sample2015-test-03-abc-07-artisan-suffix.extension', $result);
60+
}
61+
}

0 commit comments

Comments
 (0)