Skip to content

Commit

Permalink
Merge pull request #462 from deleugpn/after-recording
Browse files Browse the repository at this point in the history
[1.0] Provide AfterRecording Hook
  • Loading branch information
taylorotwell authored Dec 4, 2018
2 parents 4ff1283 + e843958 commit 502d400
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Telescope
*/
public static $filterBatchUsing = [];

/**
* The callback executed after queuing a new entry.
*
* @var \Closure
*/
public static $afterRecordingHook;

/**
* The callback that adds tags to the record.
*
Expand Down Expand Up @@ -242,6 +249,10 @@ protected static function record(string $type, IncomingEntry $entry)
if (collect(static::$filterUsing)->every->__invoke($entry)) {
static::$entriesQueue[] = $entry;
}

if (static::$afterRecordingHook) {
call_user_func(static::$afterRecordingHook, new static);
}
});
}

Expand Down Expand Up @@ -469,6 +480,19 @@ public static function filterBatch(Closure $callback)
return new static;
}

/**
* Set the callback that will be executed after an entry is recorded in the queue.
*
* @param \Closure $callback
* @return static
*/
public static function afterRecording(Closure $callback)
{
static::$afterRecordingHook = $callback;

return new static;
}

/**
* Set the callback that adds tags to the record.
*
Expand Down
73 changes: 73 additions & 0 deletions tests/Telescope/TelescopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Laravel\Telescope\Tests\Telescope;

use Laravel\Telescope\Telescope;
use Laravel\Telescope\Tests\FeatureTestCase;
use Laravel\Telescope\Watchers\QueryWatcher;
use Laravel\Telescope\Contracts\EntriesRepository;

class TelescopeTest extends FeatureTestCase
{
private $count = 0;

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app->get('config')->set('telescope.watchers', [
QueryWatcher::class => [
'enabled' => true,
'slow' => 0.9,
],
]);
}

protected function tearDown()
{
Telescope::$afterRecordingHook = null;

parent::tearDown();
}

/**
* @test
*/
public function run_after_recording_callback()
{
Telescope::afterRecording(function () {
$this->count++;
});

$this->app->get('db')->table('telescope_entries')->count();

$this->app->get('db')->table('telescope_entries')->count();

$this->assertSame(2, $this->count);
}

/**
* @test
*/
public function after_recording_callback_can_store_and_flush()
{
Telescope::afterRecording(function (Telescope $telescope) {
if (count($telescope::$entriesQueue) > 1) {
$repository = $this->app->make(EntriesRepository::class);
$telescope->store($repository);
}
});

$this->app->get('db')->table('telescope_entries')->count();

$this->assertCount(1, Telescope::$entriesQueue);

$this->app->get('db')->table('telescope_entries')->count();

$this->assertCount(0, Telescope::$entriesQueue);

$this->app->get('db')->table('telescope_entries')->count();

$this->assertCount(1, Telescope::$entriesQueue);
}
}

0 comments on commit 502d400

Please sign in to comment.