Skip to content

Commit

Permalink
Cleanup syntax, change multiple expect() calls into chain
Browse files Browse the repository at this point in the history
  • Loading branch information
onlime committed Mar 11, 2024
1 parent 33a30c7 commit 17d98b4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
10 changes: 5 additions & 5 deletions tests/Feature/SqlReporterServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
it('registers event listeners', function (string $eventName) {
$listeners = Event::getRawListeners()[$eventName] ?? [];

expect($listeners)->not->toBeEmpty();
expect($listeners)->toContain(LogSqlQuery::class);
expect($listeners)->not->toBeEmpty()
->and($listeners)->toContain(LogSqlQuery::class);
})->with([
CommandFinished::class,
RequestHandled::class,
Expand All @@ -18,9 +18,9 @@
it('merges the default config', function () {
$config = config('sql-reporter');

expect($config)->toBeArray();
expect($config)->toHaveKey('queries');
expect($config)->toHaveKey('general');
expect($config)->toBeArray()
->and($config)->toHaveKey('queries')
->and($config)->toHaveKey('general');
});

it('can publish the config file', function () {
Expand Down
87 changes: 42 additions & 45 deletions tests/Feature/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@

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);
config()->set(
// prepend all keys with 'sql-reporter.' prefix
is_array($key)
? collect($key)->mapWithKeys(fn (string $value, mixed $key) => ["sql-reporter.$key" => $value])->all()
: ['sql-reporter.'.$key => $value]
);
}

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();
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toBeEmpty();
});

it('does not create directory if it does not exist for 2nd query', function () {
Expand All @@ -66,10 +64,10 @@ function setConfig(string|array $key, mixed $value =null)

$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);
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('appends to existing log file', function () {
Expand All @@ -88,9 +86,9 @@ function setConfig(string|array $key, mixed $value =null)
$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);
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('replaces current file content for 1st query when overriding is turned on', function () {
Expand All @@ -112,9 +110,9 @@ function setConfig(string|array $key, mixed $value =null)
$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);
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('appends to current file content for 2nd query when overriding is turned on', function () {
Expand All @@ -138,9 +136,9 @@ function setConfig(string|array $key, mixed $value =null)
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);
expect($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('saves select query to file when pattern set to select queries', function () {
Expand All @@ -154,19 +152,18 @@ function setConfig(string|array $key, mixed $value =null)
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);
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(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);
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toHaveCount(0);
});

it('saves insert query to file when pattern set to insert or update queries', function () {
Expand All @@ -180,11 +177,10 @@ function setConfig(string|array $key, mixed $value =null)
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);
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('uses raw query without bindings when using query pattern', function () {
Expand All @@ -198,11 +194,10 @@ function setConfig(string|array $key, mixed $value =null)
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);
expect($this->directory)->toBeFile()
->and($this->filesystem->allFiles($this->directory))->toHaveCount(1)
->and($this->directory.'/'.$expectedFileName)->toBeFile()
->and(file_get_contents($this->directory.'/'.$expectedFileName))->toBe($expectedContent);
});

it('only logs slow queries', function () {
Expand All @@ -216,10 +211,11 @@ function setConfig(string|array $key, mixed $value =null)

$writer = Mockery::mock(Writer::class, [$this->formatter, $this->config, $this->filename])
->makePartial()->shouldAllowMockingProtectedMethods();
$writer->shouldAllowMockingProtectedMethods();
$writer->shouldReceive('writeLine')->twice()->andReturn(false);

expect($writer->writeQuery($query1))->toBeFalse();
expect($writer->writeQuery($query2))->toBeTrue();
expect($writer->writeQuery($query1))->toBeFalse()
->and($writer->writeQuery($query2))->toBeTrue();
});

it('respects query patterns', function () {
Expand All @@ -237,9 +233,10 @@ function setConfig(string|array $key, mixed $value =null)

$writer = Mockery::mock(Writer::class, [$this->formatter, $this->config, $this->filename])
->makePartial()->shouldAllowMockingProtectedMethods();
$writer->shouldAllowMockingProtectedMethods();
$writer->shouldReceive('writeLine')->twice()->andReturn(false);

expect($writer->writeQuery($query1))->toBeFalse();
expect($writer->writeQuery($query2))->toBeTrue();
expect($writer->writeQuery($query3))->toBeFalse();
expect($writer->writeQuery($query1))->toBeFalse()
->and($writer->writeQuery($query2))->toBeTrue()
->and($writer->writeQuery($query3))->toBeFalse();
});
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
|
*/

uses(\Tests\FeatureTestCase::class)->in('Feature');
uses(\Tests\UnitTestCase::class)->in('Unit');
uses(Tests\FeatureTestCase::class)->in('Feature');
uses(Tests\UnitTestCase::class)->in('Unit');

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/FileNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
});

it('returns valid file name for queries when not running in console', function () {
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(false);
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()
->andReturn(false);
$this->config->shouldReceive('queriesFileName')->once()->withNoArgs()
->andReturn('sample[Y]-test-[m]-abc-[d]');
$this->config->shouldReceive('fileExtension')->once()->withNoArgs()
Expand All @@ -23,7 +24,8 @@
});

it('returns valid file name for queries when running in console', function () {
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()->andReturn(true);
$this->app->shouldReceive('runningInConsole')->once()->withNoArgs()
->andReturn(true);
$this->config->shouldReceive('consoleSuffix')->once()->withNoArgs()
->andReturn('-artisan-suffix');
$this->config->shouldReceive('queriesFileName')->once()->withNoArgs()
Expand Down

0 comments on commit 17d98b4

Please sign in to comment.