Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
* Added direct support for laravel 5.6.x
* Changed php support to min of >=7.1 as per laravel 5.6
* Changed reqiured dependancy `illuminate/support` to minimum ^5.0
* Added local docker `phpunit`, `phpunit-versions`, `composer` helpers `./bin/`
    - e.g. simply type `bin/phpunit`
* Updated travis config
* Use of class constants to add coverage of namespace collision edge cases
* Added config helper to DRY codebase
* Added basic method return type declarations (strong typing is a win:win)
  • Loading branch information
Andrew McLagan committed Jul 5, 2018
commit 8c6c55bf4890a4c5cf36b0f2ab87fcc0456643f6
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ php:
- 7.2
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4
- hhvm
- nightly

Expand Down
8 changes: 8 additions & 0 deletions bin/composer
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e

docker run --rm --interactive --tty \
-e "TERM=xterm-256color" \
-v $PWD:/app \
composer "$@"
11 changes: 11 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -e

docker run -it --rm \
-e "TERM=xterm-256color" \
-v "$PWD":/usr/src \
-w /usr/src \
php:7.2-cli-alpine \
vendor/bin/phpunit "$@"

15 changes: 15 additions & 0 deletions bin/phpunit-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

declare -a versions=("7.1" "7.2")

for version in "${versions[@]}"
do
docker run -it --rm \
-e "TERM=xterm-256color" \
-v "$PWD":/usr/src \
-w /usr/src \
php:"$version"-cli-alpine \
vendor/bin/phpunit "$@"
done
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
}
],
"require": {
"php": ">=5.4",
"illuminate/support": "^4.0|^5.0",
"php": ">=7.0",
"illuminate/support": "^5.0",
"rollbar/rollbar": "^1"
},
"require-dev": {
"orchestra/testbench": "~3.0",
"orchestra/testbench": "~3.6",
"mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "2.*",
"phpunit/phpunit": "~4.0|~5.0"
"phpunit/phpunit": "~7.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Test Suite">
Expand Down
3 changes: 2 additions & 1 deletion src/Facades/Rollbar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Rollbar\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Rollbar\Laravel\RollbarLogHandler;

class Rollbar extends Facade
{
Expand All @@ -11,6 +12,6 @@ class Rollbar extends Facade
*/
protected static function getFacadeAccessor()
{
return 'Rollbar\Laravel\RollbarLogHandler';
return RollbarLogHandler::class;
}
}
52 changes: 38 additions & 14 deletions src/RollbarServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace Rollbar\Laravel;

use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Rollbar\Rollbar;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Rollbar\Laravel\RollbarLogHandler;
use Rollbar\RollbarLogger;
use Rollbar\Rollbar;

class RollbarServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -41,7 +43,7 @@ public function boot()
$context = $args[2];
}

$app['Rollbar\Laravel\RollbarLogHandler']->log($level, $message, $context);
$app[RollbarLogHandler::class]->log($level, $message, $context);
});
}

Expand All @@ -55,9 +57,7 @@ public function register()
return;
}

$app = $this->app;

$this->app->singleton('Rollbar\RollbarLogger', function ($app) {
$this->app->singleton(RollbarLogger::class, function ($app) {

$defaults = [
'environment' => $app->environment(),
Expand All @@ -66,8 +66,10 @@ public function register()
'handle_error' => true,
'handle_fatal' => true,
];
$config = array_merge($defaults, $app['config']->get('services.rollbar', []));
$config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token');

$config = array_merge($defaults, $app['config']->get('logging.channels.rollbar', []));

$config['access_token'] = static::config('access_token');

if (empty($config['access_token'])) {
throw new InvalidArgumentException('Rollbar access token not configured');
Expand All @@ -82,11 +84,11 @@ public function register()
return Rollbar::logger();
});

$this->app->singleton('Rollbar\Laravel\RollbarLogHandler', function ($app) {
$this->app->singleton(RollbarLogHandler::class, function ($app) {

$level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');
$level = static::config('level', 'debug');

return new RollbarLogHandler($app['Rollbar\RollbarLogger'], $app, $level);
return new RollbarLogHandler($app[RollbarLogger::class], $app, $level);
});
}

Expand All @@ -95,12 +97,34 @@ public function register()
*
* @return boolean
*/
public function stop()
public function stop() : bool
{
$level = getenv('ROLLBAR_LEVEL') ?: $this->app->config->get('services.rollbar.level', null);
$token = getenv('ROLLBAR_TOKEN') ?: $this->app->config->get('services.rollbar.access_token', null);
$level = static::config('level');

$token = static::config('token');

$hasToken = empty($token) === false;

return $hasToken === false || $level === 'none';
}

/**
* Return a rollbar logging config
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
protected static function config($key = '', $default = null)
{
$envKey = 'ROLLBAR_'.strtoupper($key);

if ($envKey === 'ROLLBAR_ACCESS_TOKEN') {
$envKey = 'ROLLBAR_TOKEN';
}

$logKey = empty($key) ? 'logging.channels.rollbar' : "logging.channels.rollbar.$key";

return getenv($envKey) ?: Config::get($logKey, $default);
}
}
73 changes: 39 additions & 34 deletions tests/RollbarTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php namespace Rollbar\Laravel;
<?php

namespace Rollbar\Laravel;

use Rollbar\Laravel\Facades\Rollbar as RollbarFacade;
use Rollbar\Laravel\RollbarServiceProvider;
use Rollbar\Laravel\RollbarLogHandler;
use Rollbar\RollbarLogger;

class RollbarTest extends \Orchestra\Testbench\TestCase
{
Expand All @@ -14,45 +19,45 @@ public function setUp()

protected function getPackageProviders($app)
{
return ['Rollbar\Laravel\RollbarServiceProvider'];
return [RollbarServiceProvider::class];
}

public function testBinding()
{
$client = $this->app->make('Rollbar\RollbarLogger');
$this->assertInstanceOf('Rollbar\RollbarLogger', $client);
$client = $this->app->make(RollbarLogger::class);
$this->assertInstanceOf(RollbarLogger::class, $client);

$handler = $this->app->make('Rollbar\Laravel\RollbarLogHandler');
$this->assertInstanceOf('Rollbar\Laravel\RollbarLogHandler', $handler);
$handler = $this->app->make(RollbarLogHandler::class);
$this->assertInstanceOf(RollbarLogHandler::class, $handler);
}

public function testIsSingleton()
{
$handler1 = $this->app->make('Rollbar\Laravel\RollbarLogHandler');
$handler2 = $this->app->make('Rollbar\Laravel\RollbarLogHandler');
$handler1 = $this->app->make(RollbarLogHandler::class);
$handler2 = $this->app->make(RollbarLogHandler::class);
$this->assertEquals(spl_object_hash($handler1), spl_object_hash($handler2));
}

public function testFacade()
{
$client = RollbarFacade::getFacadeRoot();
$this->assertInstanceOf('Rollbar\Laravel\RollbarLogHandler', $client);
$this->assertInstanceOf(RollbarLogHandler::class, $client);
}

public function testPassConfiguration()
{
$client = $this->app->make('Rollbar\RollbarLogger');
$client = $this->app->make(RollbarLogger::class);
$config = $client->extend([]);
$this->assertEquals($this->access_token, $config['access_token']);
}

public function testCustomConfiguration()
{
$this->app->config->set('services.rollbar.root', '/tmp');
$this->app->config->set('services.rollbar.included_errno', E_ERROR);
$this->app->config->set('services.rollbar.environment', 'staging');
$this->app->config->set('logging.channels.rollbar.root', '/tmp');
$this->app->config->set('logging.channels.rollbar.included_errno', E_ERROR);
$this->app->config->set('logging.channels.rollbar.environment', 'staging');

$client = $this->app->make('Rollbar\RollbarLogger');
$client = $this->app->make(RollbarLogger::class);
$config = $client->extend([]);

$this->assertEquals('staging', $config['environment']);
Expand All @@ -64,11 +69,11 @@ public function testAutomaticContext()
{
$this->app->session->put('foo', 'bar');

$logger = $this->app->make('Rollbar\RollbarLogger');
$logger = $this->app->make(RollbarLogger::class);

$handlerMock = \Mockery::mock('Rollbar\Laravel\RollbarLogHandler', [$logger, $this->app]);
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$logger, $this->app]);
$handlerMock->shouldReceive('log')->passthru();
$this->app['Rollbar\Laravel\RollbarLogHandler'] = $handlerMock;
$this->app[RollbarLogHandler::class] = $handlerMock;

$handlerMock->log('info', 'Test log message');

Expand All @@ -84,11 +89,11 @@ public function testMergedContext()
{
$this->app->session->put('foo', 'bar');

$logger = $this->app->make('Rollbar\RollbarLogger');
$logger = $this->app->make(RollbarLogger::class);

$handlerMock = \Mockery::mock('Rollbar\Laravel\RollbarLogHandler', [$logger, $this->app]);
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$logger, $this->app]);
$handlerMock->shouldReceive('log')->passthru();
$this->app['Rollbar\Laravel\RollbarLogHandler'] = $handlerMock;
$this->app[RollbarLogHandler::class] = $handlerMock;

$handlerMock->log('info', 'Test log message', [
'tags' => ['one' => 'two'],
Expand All @@ -108,16 +113,16 @@ public function testLogListener()
{
$exception = new \Exception('Testing error handler');

$clientMock = \Mockery::mock('Rollbar\RollbarLogger');
$clientMock = \Mockery::mock(RollbarLogger::class);

$clientMock->shouldReceive('log')->times(2);
$clientMock->shouldReceive('log')->times(1)->with('error', $exception, ['foo' => 'bar']);

$handlerMock = \Mockery::mock('Rollbar\Laravel\RollbarLogHandler', [$clientMock, $this->app]);
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$clientMock, $this->app]);

$handlerMock->shouldReceive('log')->passthru();

$this->app['Rollbar\Laravel\RollbarLogHandler'] = $handlerMock;
$this->app[RollbarLogHandler::class] = $handlerMock;

$this->app->log->info('hello');
$this->app->log->error('oops');
Expand All @@ -126,11 +131,11 @@ public function testLogListener()

public function testErrorLevels1()
{
$this->app->config->set('services.rollbar.level', 'critical');
$this->app->config->set('logging.channels.rollbar.level', 'critical');

$clientMock = \Mockery::mock('Rollbar\RollbarLogger');
$clientMock = \Mockery::mock(RollbarLogger::class);
$clientMock->shouldReceive('log')->times(3);
$this->app['Rollbar\RollbarLogger'] = $clientMock;
$this->app[RollbarLogger::class] = $clientMock;

$this->app->log->debug('hello');
$this->app->log->info('hello');
Expand All @@ -144,11 +149,11 @@ public function testErrorLevels1()

public function testErrorLevels2()
{
$this->app->config->set('services.rollbar.level', 'debug');
$this->app->config->set('logging.channels.rollbar.level', 'debug');

$clientMock = \Mockery::mock('Rollbar\RollbarLogger');
$clientMock = \Mockery::mock(RollbarLogger::class);
$clientMock->shouldReceive('log')->times(8);
$this->app['Rollbar\RollbarLogger'] = $clientMock;
$this->app[RollbarLogger::class] = $clientMock;

$this->app->log->debug('hello');
$this->app->log->info('hello');
Expand All @@ -162,11 +167,11 @@ public function testErrorLevels2()

public function testErrorLevels3()
{
$this->app->config->set('services.rollbar.level', 'none');
$this->app->config->set('logging.channels.rollbar.level', 'none');

$clientMock = \Mockery::mock('Rollbar\RollbarLogger');
$clientMock = \Mockery::mock(RollbarLogger::class);
$clientMock->shouldReceive('log')->times(0);
$this->app['Rollbar\RollbarLogger'] = $clientMock;
$this->app[RollbarLogger::class] = $clientMock;

$this->app->log->debug('hello');
$this->app->log->info('hello');
Expand All @@ -180,14 +185,14 @@ public function testErrorLevels3()

public function testPersonFunctionIsCalledWhenSessionContainsAtLeastOneItem()
{
$this->app->config->set('services.rollbar.person_fn', function () {
$this->app->config->set('logging.channels.rollbar.person_fn', function () {
return [
'id' => '123',
'username' => 'joebloggs',
];
});

$logger = $this->app->make('Rollbar\RollbarLogger');
$logger = $this->app->make(RollbarLogger::class);

$this->app->session->put('foo', 'bar');

Expand Down