Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Integration with Laravel Telescope implemented #54

Merged
merged 10 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## UNRELEASED

### Added

- Integration with [Laravel Telescope](https://github.com/laravel/telescope/) is supported now (just enable `SetupTelescopeListener` for `BeforeLoopStartedEvent`)

## v5.1.0

### Added
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"laravel/laravel": "~6.0 || ~7.0 || ~8.0",
"laravel/scout": "~8.0 || ~9.0",
"laravel/socialite": "^5.0",
"laravel/telescope": "^4.5",
"mockery/mockery": "^1.3.2",
"phpstan/phpstan": "~0.12.80",
"phpunit/phpunit": "^8.0 || ^9.3"
Expand Down
1 change: 1 addition & 0 deletions config/roadrunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'listeners' => [
Events\BeforeLoopStartedEvent::class => [
...Defaults::beforeLoopStarted(),
// Listeners\SetupTelescopeListener::class, // for <https://github.com/laravel/telescope>
],

Events\BeforeLoopIterationEvent::class => [
Expand Down
67 changes: 67 additions & 0 deletions src/Listeners/SetupTelescopeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Listeners;

use Illuminate\Support\Str;
use Laravel\Telescope\{Telescope, EntryType, IncomingEntry};
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
use Illuminate\Contracts\Config\Repository as ConfigRepository;

/**
* Target package: <https://github.com/laravel/telescope>.
*/
class SetupTelescopeListener implements ListenerInterface
{
/**
* {@inheritdoc}
*/
public function handle($event): void
{
if ($event instanceof WithApplication) {
/** @var ConfigRepository $config */
$config = $event->application()->make(ConfigRepository::class);

if (!\class_exists(Telescope::class) || !$config->get('telescope.enabled')) {
return;
}

Telescope::filter(static function (IncomingEntry $entry): bool {
switch ($entry->type) {
case EntryType::EVENT:
if (Str::startsWith($entry->content['name'] ?? '', 'Spiral\\RoadRunnerLaravel\\')) {
return false;
}

break;

case EntryType::REQUEST:
if (Str::startsWith($entry->content['controller_action'] ?? '', 'Laravel\\Telescope\\')) {
return false;
}

break;

case EntryType::VIEW:
if (Str::startsWith($entry->content['name'] ?? '', 'telescope::')) {
return false;
}

break;

case EntryType::REDIS:
$cmd = $entry->content['command'] ?? '';

if (Str::contains($cmd, ['telescope:pause-recording', 'telescope:dump-watcher'])) {
return false;
}

break;
}

return true;
});
}
}
}
98 changes: 98 additions & 0 deletions tests/Unit/Listeners/SetupTelescopeListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;

use Mockery as m;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\IncomingEntry;
use Spiral\RoadRunnerLaravel\Listeners\SetupTelescopeListener;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;

/**
* @covers \Spiral\RoadRunnerLaravel\Listeners\SetupTelescopeListener
*/
class SetupTelescopeListenerTest extends AbstractListenerTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp(): void
{
parent::setUp();

$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
}

/**
* {@inheritdoc}
*/
public function testHandle(): void
{
/** @var m\MockInterface|WithApplication $event_mock */
$event_mock = m::mock(WithApplication::class)
->makePartial()
->expects('application')
->andReturn($this->app)
->getMock();

$this->assertEmpty(Telescope::$entriesQueue);
Telescope::recordEvent($event = new IncomingEntry(['name' => 'Spiral\\RoadRunnerLaravel\\']));
Telescope::recordRequest($request = new IncomingEntry(['controller_action' => 'Laravel\\Telescope\\']));
Telescope::recordView($view = new IncomingEntry(['name' => 'telescope::foo']));
Telescope::recordRedis($redis1 = new IncomingEntry(['command' => 'get cache:telescope:pause-recording']));
Telescope::recordRedis($redis2 = new IncomingEntry(['command' => 'get cache:telescope:dump-watcher']));
$this->assertCount(5, Telescope::$entriesQueue);

Telescope::flushEntries();

$this->listenerFactory()->handle($event_mock);

$this->assertEmpty(Telescope::$entriesQueue);
Telescope::recordEvent($event);
Telescope::recordRequest($request);
Telescope::recordView($view);
Telescope::recordRedis($redis1);
Telescope::recordRedis($redis2);
$this->assertEmpty(Telescope::$entriesQueue);

Telescope::recordBatch($any_another_entry = new IncomingEntry([]));
Telescope::recordCache($any_another_entry);
Telescope::recordCommand($any_another_entry);
Telescope::recordDump($any_another_entry);
Telescope::recordEvent($any_another_entry);
Telescope::recordException($any_another_entry);
Telescope::recordGate($any_another_entry);
Telescope::recordJob($any_another_entry);
Telescope::recordLog($any_another_entry);
Telescope::recordMail($any_another_entry);
Telescope::recordNotification($any_another_entry);
Telescope::recordQuery($any_another_entry);
Telescope::recordModelEvent($any_another_entry);
Telescope::recordRedis($any_another_entry);
Telescope::recordRequest($any_another_entry);
Telescope::recordScheduledCommand($any_another_entry);
Telescope::recordView($any_another_entry);
Telescope::recordClientRequest($any_another_entry);
$this->assertCount(18, Telescope::$entriesQueue);
}

/**
* @return SetupTelescopeListener
*/
protected function listenerFactory(): SetupTelescopeListener
{
return new SetupTelescopeListener();
}

/**
* {@inheritDoc}
*/
protected function tearDown(): void
{
Telescope::flushEntries();

parent::tearDown();
}
}