-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f02a18f
commit f54e72b
Showing
15 changed files
with
654 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
<?php | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Onlime\LaravelSqlReporter\Config; | ||
use Onlime\LaravelSqlReporter\FileName; | ||
use Onlime\LaravelSqlReporter\Formatter; | ||
use Onlime\LaravelSqlReporter\SqlQuery; | ||
use Onlime\LaravelSqlReporter\Writer; | ||
|
||
beforeEach(function () { | ||
$this->now = Carbon::parse('2015-02-03 06:41:31'); | ||
Carbon::setTestNow($this->now); | ||
$this->formatter = Mockery::mock(Formatter::class); | ||
$this->config = app(Config::class); | ||
$this->filename = Mockery::mock(FileName::class); | ||
$this->writer = new Writer($this->formatter, $this->config, $this->filename); | ||
$this->directory = __DIR__.'/test-dir/directory'; | ||
setConfig('general.directory', $this->directory); | ||
$this->filesystem = new Filesystem(); | ||
}); | ||
|
||
afterEach(function () { | ||
$this->filesystem->deleteDirectory($this->directory); | ||
}); | ||
|
||
function setConfig(string|array $key, mixed $value =null) | ||
{ | ||
if (is_array($key)) { | ||
// prepend all keys with 'sql-reporter.' | ||
$key = collect($key)->mapWithKeys(fn (string $value, mixed $key) => ["sql-reporter.$key" => $value])->all(); | ||
} else { | ||
$key = ['sql-reporter.'.$key => $value]; | ||
} | ||
|
||
config()->set($key); | ||
} | ||
|
||
it('creates directory if it does not exist for 1st query', function () { | ||
$query = new SqlQuery(1, 'test', 5.41); | ||
setConfig('queries.enabled', false); | ||
expect($this->directory)->not()->toBeDirectory(); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toBeEmpty(); | ||
}); | ||
|
||
it('does not create directory if it does not exist for 2nd query', function () { | ||
$query = new SqlQuery(2, 'test', 5.41); | ||
setConfig('queries.enabled', false); | ||
expect($this->directory)->not()->toBeDirectory(); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->not()->toBeDirectory(); | ||
}); | ||
|
||
it('creates log file', function () { | ||
$lineContent = 'Sample log line'; | ||
$expectedContent = "-- header\nSample log line\n"; | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
|
||
$query = new SqlQuery(1, 'test', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header'); | ||
|
||
setConfig('queries.include_pattern', '#.*#i'); | ||
|
||
$this->filename->shouldReceive('getLogfile')->times(2)->withNoArgs()->andReturn($expectedFileName); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('appends to existing log file', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
mkdir($this->directory, 0777, true); | ||
$initialContent = "Initial file content\n"; | ||
file_put_contents($this->directory.'/'.$expectedFileName, $initialContent); | ||
|
||
$lineContent = 'Sample log line'; | ||
$expectedContent = $initialContent."-- header\nSample log line\n"; | ||
|
||
$query = new SqlQuery(1, 'test', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header'); | ||
setConfig('queries.include_pattern', '#.*#i'); | ||
$this->filename->shouldReceive('getLogfile')->times(2)->withNoArgs()->andReturn($expectedFileName); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
$this->writer->writeQuery($query); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('replaces current file content for 1st query when overriding is turned on', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
mkdir($this->directory, 0777, true); | ||
$initialContent = "Initial file content\n"; | ||
file_put_contents($this->directory.'/'.$expectedFileName, $initialContent); | ||
|
||
$lineContent = 'Sample log line'; | ||
$expectedContent = "-- header\nSample log line\n"; | ||
|
||
$query = new SqlQuery(1, 'test', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header'); | ||
setConfig([ | ||
'queries.include_pattern' => '#.*#i', | ||
'queries.override_log' => true, | ||
]); | ||
$this->filename->shouldReceive('getLogfile')->times(2)->withNoArgs()->andReturn($expectedFileName); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
$this->writer->writeQuery($query); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('appends to current file content for 2nd query when overriding is turned on', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
mkdir($this->directory, 0777, true); | ||
$initialContent = "Initial file content\n"; | ||
file_put_contents($this->directory.'/'.$expectedFileName, $initialContent); | ||
|
||
$lineContent = 'Sample log line'; | ||
$expectedContent = "-- header\n$lineContent\n$lineContent\n"; | ||
|
||
$query1 = new SqlQuery(1, 'test', 5.41); | ||
$query2 = new SqlQuery(2, 'test', 5.41); | ||
$this->formatter->shouldReceive('getLine')->twice()->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header'); | ||
setConfig([ | ||
'queries.include_pattern' => '#.*#i', | ||
'queries.override_log' => true, | ||
]); | ||
$this->filename->shouldReceive('getLogfile')->times(3)->withNoArgs()->andReturn($expectedFileName); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
$this->writer->writeQuery($query1); | ||
$this->writer->writeQuery($query2); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('saves select query to file when pattern set to select queries', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
$lineContent = 'Sample log line'; | ||
$expectedContent = "\n$lineContent\n"; | ||
|
||
$query = new SqlQuery(1, 'select * FROM test', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn(''); | ||
setConfig('queries.include_pattern', '#^SELECT .*$#i'); | ||
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
|
||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('doesnt save select query to file when pattern set to insert or update queries', function () { | ||
$query = new SqlQuery(1, 'select * FROM test', 5.41); | ||
setConfig('queries.include_pattern', '#^(?:UPDATE|INSERT) .*$#i'); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(0); | ||
}); | ||
|
||
it('saves insert query to file when pattern set to insert or update queries', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
$lineContent = 'Sample log line'; | ||
$expectedContent = "\n$lineContent\n"; | ||
|
||
$query = new SqlQuery(1, 'INSERT INTO test(one, two, three) values(?, ?, ?)', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn(''); | ||
setConfig('queries.include_pattern', '#^(?:UPDATE|INSERT) .*$#i'); | ||
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
|
||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('uses raw query without bindings when using query pattern', function () { | ||
$expectedFileName = $this->now->format('Y-m').'-log.sql'; | ||
$lineContent = 'Sample log line'; | ||
$expectedContent = "\n$lineContent\n"; | ||
|
||
$query = new SqlQuery(1, 'UPDATE test SET x = 2 WHERE id = 3', 5.41); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); | ||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn(''); | ||
setConfig('queries.include_pattern', '#^(?:UPDATE test SET x = \d |INSERT ).*$#i'); | ||
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName); | ||
$this->writer->writeQuery($query); | ||
expect($this->directory)->toBeFile(); | ||
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1); | ||
|
||
expect($this->directory.'/'.$expectedFileName)->toBeFile(); | ||
expect(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent); | ||
}); | ||
|
||
it('only logs slow queries', function () { | ||
$query1 = new SqlQuery(1, 'test1', 5.41); | ||
$query2 = new SqlQuery(2, 'test2', 500.5); | ||
|
||
setConfig('queries.min_exec_time', 500); | ||
|
||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn(''); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query2)->andReturn(''); | ||
|
||
$writer = Mockery::mock(Writer::class, [$this->formatter, $this->config, $this->filename]) | ||
->makePartial()->shouldAllowMockingProtectedMethods(); | ||
$writer->shouldReceive('writeLine')->twice()->andReturn(false); | ||
|
||
expect($writer->writeQuery($query1))->toBeFalse(); | ||
expect($writer->writeQuery($query2))->toBeTrue(); | ||
}); | ||
|
||
it('respects query patterns', function () { | ||
$query1 = new SqlQuery(1, 'select foo from bar', 5.41); | ||
$query2 = new SqlQuery(2, 'update bar set foo = 1', 3.55); | ||
$query3 = new SqlQuery(3, "update bar set last_visit = '2021-06-03 10:26:00'", 3.22); | ||
|
||
setConfig([ | ||
'queries.include_pattern' => '/^(?!SELECT).*$/i', | ||
'queries.exclude_pattern' => '/^UPDATE.*last_visit/i', | ||
]); | ||
|
||
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn(''); | ||
$this->formatter->shouldReceive('getLine')->once()->with($query2)->andReturn(''); | ||
|
||
$writer = Mockery::mock(Writer::class, [$this->formatter, $this->config, $this->filename]) | ||
->makePartial()->shouldAllowMockingProtectedMethods(); | ||
$writer->shouldReceive('writeLine')->twice()->andReturn(false); | ||
|
||
expect($writer->writeQuery($query1))->toBeFalse(); | ||
expect($writer->writeQuery($query2))->toBeTrue(); | ||
expect($writer->writeQuery($query3))->toBeFalse(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
namespace Tests; | ||
|
||
use Carbon\Carbon; | ||
use Mockery; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Test Case | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The closure you provide to your test functions is always bound to a specific PHPUnit test | ||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may | ||
| need to change it using the "uses()" function to bind a different classes or traits. | ||
| | ||
*/ | ||
|
||
uses(\Tests\FeatureTestCase::class)->in('Feature'); | ||
uses(\Tests\UnitTestCase::class)->in('Unit'); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Expectations | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When you're writing tests, you often need to check that values meet certain conditions. The | ||
| "expect()" function gives you access to a set of "expectations" methods that you can use | ||
| to assert different things. Of course, you may extend the Expectation API at any time. | ||
| | ||
*/ | ||
|
||
expect()->extend('toBeOne', function () { | ||
return $this->toBe(1); | ||
}); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Functions | ||
|-------------------------------------------------------------------------- | ||
| | ||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your | ||
| project that you don't want to repeat in every file. Here you can also expose helpers as | ||
| global functions to help you to reduce the number of lines of code in your test files. | ||
| | ||
*/ | ||
|
||
function something() | ||
{ | ||
// .. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use PHPUnit\Framework\TestCase as BaseTestCase; | ||
|
||
abstract class TestCase extends BaseTestCase | ||
{ | ||
// | ||
} |
Oops, something went wrong.