Skip to content

Commit

Permalink
Merge pull request #1 from iamgergo/master
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor authored Nov 23, 2020
2 parents e554343 + 54b697f commit 7f28fe5
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 2 deletions.
28 changes: 26 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@
},
"require-dev": {
"nunomaduro/larastan": "^0.6.9",
"orchestra/testbench": "^6.4"
}
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^9.0",
"laravel/laravel": "^8.0"
},
"autoload": {
"psr-4": {
"Larawelders\\QueueEventLogger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Larawelders\\QueueEventLogger\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Larawelders\\QueueEventLogger\\QueueEventLoggerServiceProvider"
]
},
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
34 changes: 34 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<server name="APP_KEY" value="base64:ybq4vTLI2AtF1xqSRZ8A52CtU9dUXu1yAcClXsNKGq4="/>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
37 changes: 37 additions & 0 deletions src/QueueEventLoggerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Larawelders\QueueEventLogger;

use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class QueueEventLoggerServiceProvider extends ServiceProvider
{
/**
* Boot any application services.
*
* @return void
*/
public function boot(): void
{
Event::listen(JobExceptionOccurred::class, static function (JobExceptionOccurred $event): void {
Log::channel('queue')->error(
sprintf(
'[%s] Uncaught exception %s in job %s: %s%s',
$event->job->getJobId(),
get_class($event->exception),
$event->job->resolveName(),
$event->exception->getMessage(),
property_exists($event->job, 'contextString') ? ', '.$event->job->contextString : ''
)
);
});
}
}
28 changes: 28 additions & 0 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Larawelders\QueueEventLogger\Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Larawelders\QueueEventLogger\QueueEventLoggerServiceProvider;

trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication(): Application
{
$app = require __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';

$app->booting(static function () use ($app): void {
$app->register(QueueEventLoggerServiceProvider::class);
});

$app->make(Kernel::class)->bootstrap();

return $app;
}
}
40 changes: 40 additions & 0 deletions tests/Feature/QueueEventLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Larawelders\QueueEventLogger\Tests\Feature;

use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Support\Facades\Log;
use Larawelders\QueueEventLogger\Tests\TestCase;

class QueueEventLoggerTest extends TestCase
{
/** @test */
public function it_logs_job_exception_occurred_event()
{
$job = new TestJob;
$exception = new Exception('TestJob failed');

$this->app['events']->dispatch(
new JobExceptionOccurred('default', $job, $exception)
);

Log::shouldReceive('queue')->with(
'[test-job] Uncaught exception Exception in job Larawelders\\QueueEventLogger\\Tests\\Feature\\TestJob: TestJob failed',
);
}
}

class TestJob implements ShouldQueue
{
public function resolveName()
{
return static::class;
}

public function getJobId()
{
return 'test-job';
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Larawelders\QueueEventLogger\Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}

0 comments on commit 7f28fe5

Please sign in to comment.