Skip to content

Commit

Permalink
test: add new tests to cover code
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodmain committed Jun 6, 2024
1 parent c731ebf commit e0f92f0
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.phpunit.result.cache
.phpunit.cache
vendor/
73 changes: 68 additions & 5 deletions Tests/TelescopePruneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Laravel\Telescope\Storage\DatabaseEntriesRepository;
use Orchestra\Testbench\TestCase;
use RonasIT\TelescopeExtension\Repositories\TelescopeRepository;
use RonasIT\TelescopeExtension\TelescopeExtensionServiceProvider;
Expand Down Expand Up @@ -50,7 +49,6 @@ protected function getEnvironmentSetUp($app): void
'prefix' => '',
]);

//$app->when(DatabaseEntriesRepository::class)
$app->when(TelescopeRepository::class)
->needs('$connection')
->give('testbench');
Expand Down Expand Up @@ -122,9 +120,74 @@ public function testPruneWithSeveralSetHoursAndHours()
->assertExitCode(0);
}

/*public function testPruneRequestsAndOthers()

public function testPruneWithUnresolvedException()
{
$this->mockQueriesWithUnresolvedException();

$this->artisan('telescope:prune --set-hours=request:5,unresolved_exception:20,query:25 --hours=80')
->expectsOutput("Pruning records of type 'request' older than 5 hours...")
->expectsOutput('Deleted 200 records.')
->expectsOutput("Pruning records of type 'unresolved_exception' older than 20 hours...")
->expectsOutput('Deleted 32 records.')
->expectsOutput("Pruning records of type 'query' older than 25 hours...")
->expectsOutput('Deleted 50 records.')
->expectsOutput('Pruning records of other types older than 80 hours...')
->expectsOutput('Deleted 200 records.')
->assertExitCode(0);
}

public function testPruneWithResolvedExceptionWithoutHours()
{
$this->mockQueriesWithResolvedExceptionWithoutHours();

$this->artisan('telescope:prune --set-hours=request:5,resolved_exception:10,query:25')
->expectsOutput("Pruning records of type 'request' older than 5 hours...")
->expectsOutput('Deleted 200 records.')
->expectsOutput("Pruning records of type 'resolved_exception' older than 10 hours...")
->expectsOutput('Deleted 15 records.')
->expectsOutput("Pruning records of type 'query' older than 25 hours...")
->expectsOutput('Deleted 50 records.')
->assertExitCode(0);
}

public function testPruneValidateSetHoursType()
{
$this->expectException('Exception');
$this->expectExceptionMessage("Incorrect type value 'incorrect'.");

$this->artisan('telescope:prune --set-hours=query:12,incorrect:22');
}

public function testPruneValidateSetHoursValueNotSet()
{
$this->expectException('Exception');
$this->expectExceptionMessage("Incorrect value 'request' of the 'set-hours' option.");

$this->artisan('telescope:prune --set-hours=query:12,request');
}

public function testPruneValidateSetHoursValueEmpty()
{
Artisan::call('telescope:prune --set-hours=requests:6 --hours=2');
$this->expectException('Exception');
$this->expectExceptionMessage("Hours value for 'request' type must be set.");

}*/
$this->artisan('telescope:prune --set-hours=query:12,request:');
}

public function testPruneValidateSetHoursValueIsNotNumber()
{
$this->expectException('Exception');
$this->expectExceptionMessage("Hours value for 'request' type must be a number.");

$this->artisan('telescope:prune --set-hours=query:12,request:ss');
}

public function testPruneValidateHoursValueIsNotNumber()
{
$this->expectException('Exception');
$this->expectExceptionMessage('Hours hours must be a number.');

$this->artisan('telescope:prune --set-hours=query:12,request:34 --hours=ss');
}
}
138 changes: 138 additions & 0 deletions Tests/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laravel\Telescope\EntryType;
use Mpyw\LaravelDatabaseMock\Facades\DBMock;
use Mpyw\LaravelDatabaseMock\Proxies\SingleConnectionProxy;
use RonasIT\TelescopeExtension\Console\Commands\TelescopePrune;

trait SqlMockTrait
{
Expand Down Expand Up @@ -188,6 +189,143 @@ protected function mockQueriesWithSeveralSetHoursAndHours(): void
);
}

protected function mockQueriesWithUnresolvedException(): void
{
$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(5)->toDateTimeString(), EntryType::REQUEST],
200
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(5)->toDateTimeString(), EntryType::REQUEST]
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and (("type" = ? '
. 'and content::jsonb->>\'resolved_at\' is null)) limit 1000)',
[Carbon::now()->subHours(20)->toDateTimeString(), EntryType::EXCEPTION],
32
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and (("type" = ? '
. 'and content::jsonb->>\'resolved_at\' is null)) limit 1000)',
[Carbon::now()->subHours(20)->toDateTimeString(), EntryType::EXCEPTION]
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(25)->toDateTimeString(), EntryType::QUERY],
50
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(25)->toDateTimeString(), EntryType::QUERY]
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ? or "type" = ? or '
. '"type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or '
. '"type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or ("type" = ? and content::jsonb->>\'resolved_at\' is not null)) limit 1000)',
[
Carbon::now()->subHours(80)->toDateTimeString(),
EntryType::BATCH,
EntryType::CACHE,
EntryType::DUMP,
EntryType::EVENT,
EntryType::JOB,
EntryType::LOG,
EntryType::MAIL,
EntryType::MODEL,
EntryType::NOTIFICATION,
EntryType::REDIS,
EntryType::SCHEDULED_TASK,
EntryType::GATE,
EntryType::VIEW,
EntryType::EXCEPTION
],
200
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ? or "type" = ? or '
. '"type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or '
. '"type" = ? or "type" = ? or "type" = ? or "type" = ? or "type" = ? or ("type" = ? and content::jsonb->>\'resolved_at\' is not null)) limit 1000)',
[
Carbon::now()->subHours(80)->toDateTimeString(),
EntryType::BATCH,
EntryType::CACHE,
EntryType::DUMP,
EntryType::EVENT,
EntryType::JOB,
EntryType::LOG,
EntryType::MAIL,
EntryType::MODEL,
EntryType::NOTIFICATION,
EntryType::REDIS,
EntryType::SCHEDULED_TASK,
EntryType::GATE,
EntryType::VIEW,
EntryType::EXCEPTION
]
);
}

protected function mockQueriesWithResolvedExceptionWithoutHours(): void
{
$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(5)->toDateTimeString(), EntryType::REQUEST],
200
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(5)->toDateTimeString(), EntryType::REQUEST]
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and (("type" = ? '
. 'and content::jsonb->>\'resolved_at\' is not null)) limit 1000)',
[Carbon::now()->subHours(10)->toDateTimeString(), EntryType::EXCEPTION],
15
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and (("type" = ? '
. 'and content::jsonb->>\'resolved_at\' is not null)) limit 1000)',
[Carbon::now()->subHours(10)->toDateTimeString(), EntryType::EXCEPTION]
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(25)->toDateTimeString(), EntryType::QUERY],
50
);

$this->mockDelete(
'delete from "telescope_entries" where "rowid" in (select "telescope_entries"."rowid" '
. 'from "telescope_entries" where "created_at" < ? and ("type" = ?) limit 1000)',
[Carbon::now()->subHours(25)->toDateTimeString(), EntryType::QUERY]
);
}

protected function mockDelete(string $sql, array $bindings = [], ?int $rowCount = 0): void
{
$this->getPdo()->shouldDeleteForRows($sql, $bindings, $rowCount);
Expand Down
15 changes: 5 additions & 10 deletions src/Console/Commands/TelescopePrune.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Console\Command;
use Laravel\Telescope\EntryType;
use Exception;
use Throwable;

class TelescopePrune extends Command
{
Expand Down Expand Up @@ -57,14 +56,10 @@ public function handle(): void
{
$this->defaultExpirationHours = 0;

//try {
$this->validateSetHoursOption();
$this->validateHoursOption();
$this->pruneSetHours();
$this->pruneHours();
//} catch (Throwable $exception) {
// $this->error($exception->getMessage());
//}
$this->validateSetHoursOption();
$this->validateHoursOption();
$this->pruneSetHours();
$this->pruneHours();
}

protected function validateSetHoursOption(): void
Expand Down Expand Up @@ -160,7 +155,7 @@ protected function filterTypes(): array
in_array(self::UNRESOLVED_EXCEPTION, $excludeTypes)
|| in_array(self::RESOLVED_EXCEPTION, $excludeTypes)
) {
$types = array_diff($types, EntryType::EXCEPTION);
$types = array_diff($types, [EntryType::EXCEPTION]);

if (!in_array(self::EXCEPTION_TYPES, $excludeTypes)) {
$types = [
Expand Down
15 changes: 10 additions & 5 deletions src/Repositories/TelescopeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeInterface;
use Illuminate\Database\Query\Builder;
use Laravel\Telescope\EntryType;
use Laravel\Telescope\Storage\DatabaseEntriesRepository;
use RonasIT\TelescopeExtension\Console\Commands\TelescopePrune;

Expand All @@ -19,11 +20,15 @@ public function prune(DateTimeInterface $before): int
->where(function(Builder $subQuery) {
foreach ($this->pruneTypes as $type) {
if (in_array($type, TelescopePrune::EXCEPTION_TYPES)) {
$subQuery->orWhereRaw(
($type === TelescopePrune::UNRESOLVED_EXCEPTION)
? "content::jsonb->>'resolved_at' is null"
: "content::jsonb->>'resolved_at' is not null"
);
$subQuery->orWhere(function ($subSubQuery) use ($type) {
$subSubQuery
->where('type', EntryType::EXCEPTION)
->whereRaw(
($type === TelescopePrune::UNRESOLVED_EXCEPTION)
? "content::jsonb->>'resolved_at' is null"
: "content::jsonb->>'resolved_at' is not null"
);
});
} else {
$subQuery->orWhere('type', $type);
}
Expand Down

0 comments on commit e0f92f0

Please sign in to comment.