Skip to content

Commit 2375b55

Browse files
55: WIP tests for the new Laravel 5.6 logging setup
1 parent b9ce5ef commit 2375b55

File tree

6 files changed

+181
-135
lines changed

6 files changed

+181
-135
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="tests/bootstrap.php"
55
colors="true"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"

src/ExceptionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ public function report(\Exception $exception)
1212

1313
parent::report($exception);
1414
}
15-
}
15+
}

src/MonologHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ protected function addContext(array $context = [])
6666

6767
return $context;
6868
}
69-
}
69+
}

tests/Helpers/AppExceptionHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
class Handler
6+
{
7+
}

tests/RollbarTest.php

Lines changed: 166 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace Rollbar\Laravel;
3+
namespace Rollbar\Laravel\Tests;
44

5-
use Rollbar\Laravel\Facades\Rollbar as RollbarFacade;
65
use Rollbar\Laravel\RollbarServiceProvider;
7-
use Rollbar\Laravel\RollbarLogHandler;
6+
use Rollbar\Laravel\MonologHandler;
87
use Rollbar\RollbarLogger;
8+
use Monolog\Logger;
99

1010
class RollbarTest extends \Orchestra\Testbench\TestCase
1111
{
@@ -27,23 +27,17 @@ public function testBinding()
2727
$client = $this->app->make(RollbarLogger::class);
2828
$this->assertInstanceOf(RollbarLogger::class, $client);
2929

30-
$handler = $this->app->make(RollbarLogHandler::class);
31-
$this->assertInstanceOf(RollbarLogHandler::class, $handler);
30+
$handler = $this->app->make(MonologHandler::class);
31+
$this->assertInstanceOf(MonologHandler::class, $handler);
3232
}
3333

3434
public function testIsSingleton()
3535
{
36-
$handler1 = $this->app->make(RollbarLogHandler::class);
37-
$handler2 = $this->app->make(RollbarLogHandler::class);
36+
$handler1 = $this->app->make(MonologHandler::class);
37+
$handler2 = $this->app->make(MonologHandler::class);
3838
$this->assertEquals(spl_object_hash($handler1), spl_object_hash($handler2));
3939
}
4040

41-
public function testFacade()
42-
{
43-
$client = RollbarFacade::getFacadeRoot();
44-
$this->assertInstanceOf(RollbarLogHandler::class, $client);
45-
}
46-
4741
public function testPassConfiguration()
4842
{
4943
$client = $this->app->make(RollbarLogger::class);
@@ -69,140 +63,180 @@ public function testAutomaticContext()
6963
{
7064
$this->app->session->put('foo', 'bar');
7165

72-
$logger = $this->app->make(RollbarLogger::class);
66+
$logger = \Mockery::mock(RollbarLogger::class, [
67+
'access_token' => $this->access_token,
68+
'environment' => 'test'
69+
]);
70+
$logger->shouldReceive('extend')->passthru();
71+
$logger->shouldReceive('log')->with('info', 'Test log message', []);
72+
73+
$handler = new MonologHandler($logger, Logger::INFO);
74+
$handler->setApp($this->app);
7375

74-
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$logger, $this->app]);
75-
$handlerMock->shouldReceive('log')->passthru();
76-
$this->app[RollbarLogHandler::class] = $handlerMock;
76+
$handler->handle([
77+
'level' => Logger::INFO,
78+
'message' => 'Test log message',
79+
'context' => [],
80+
'extra' => [],
81+
'level_name' => 'INFO',
82+
'channel' => 'local',
83+
'datetime' => new \DateTime(),
84+
'formatted' => 'foo'
85+
]);
7786

78-
$handlerMock->log('info', 'Test log message');
87+
// ["message"]=>
88+
// string(34) "some message to be sent to rollbar"
89+
// ["context"]=>
90+
// array(0) {
91+
// }
92+
// ["level"]=>
93+
// int(200)
94+
// ["level_name"]=>
95+
// string(4) "INFO"
96+
// ["channel"]=>
97+
// string(5) "local"
98+
// ["datetime"]=>
99+
// object(DateTime)#203 (3) {
100+
// ["date"]=>
101+
// string(26) "2018-08-16 02:56:18.300807"
102+
// ["timezone_type"]=>
103+
// int(3)
104+
// ["timezone"]=>
105+
// string(3) "UTC"
106+
// }
107+
// ["extra"]=>
108+
// array(0) {
109+
// }
110+
// ["formatted"]=>
111+
// string(71) "[2018-08-16 02:56:18] local.INFO: some message to be sent to rollbar
112+
// "
79113

80-
$config = $logger->extend([]);
114+
// $config = $logger->extend([]);
81115

82-
$this->assertEquals([
83-
'session' => ['foo' => 'bar'],
84-
'id' => $this->app->session->getId(),
85-
], $config['person']);
116+
// $this->assertEquals([
117+
// 'session' => ['foo' => 'bar'],
118+
// 'id' => $this->app->session->getId(),
119+
// ], $config['person']);
86120
}
87121

88-
public function testMergedContext()
89-
{
90-
$this->app->session->put('foo', 'bar');
122+
// public function testMergedContext()
123+
// {
124+
// $this->app->session->put('foo', 'bar');
91125

92-
$logger = $this->app->make(RollbarLogger::class);
126+
// $logger = $this->app->make(RollbarLogger::class);
93127

94-
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$logger, $this->app]);
95-
$handlerMock->shouldReceive('log')->passthru();
96-
$this->app[RollbarLogHandler::class] = $handlerMock;
128+
// $handlerMock = \Mockery::mock(RollbarLogHandler::class, [$logger, $this->app]);
129+
// $handlerMock->shouldReceive('log')->passthru();
130+
// $this->app[RollbarLogHandler::class] = $handlerMock;
97131

98-
$handlerMock->log('info', 'Test log message', [
99-
'tags' => ['one' => 'two'],
100-
'person' => ['id' => "1337", 'email' => '[email protected]'],
101-
]);
132+
// $handlerMock->log('info', 'Test log message', [
133+
// 'tags' => ['one' => 'two'],
134+
// 'person' => ['id' => "1337", 'email' => '[email protected]'],
135+
// ]);
102136

103-
$config = $logger->extend([]);
137+
// $config = $logger->extend([]);
104138

105-
$this->assertEquals([
106-
'session' => ['foo' => 'bar'],
107-
'id' => "1337",
108-
'email' => '[email protected]',
109-
], $config['person']);
110-
}
139+
// $this->assertEquals([
140+
// 'session' => ['foo' => 'bar'],
141+
// 'id' => "1337",
142+
// 'email' => '[email protected]',
143+
// ], $config['person']);
144+
// }
111145

112-
public function testLogListener()
113-
{
114-
$exception = new \Exception('Testing error handler');
146+
// public function testLogListener()
147+
// {
148+
// $exception = new \Exception('Testing error handler');
115149

116-
$clientMock = \Mockery::mock(RollbarLogger::class);
150+
// $clientMock = \Mockery::mock(RollbarLogger::class);
117151

118-
$clientMock->shouldReceive('log')->times(2);
119-
$clientMock->shouldReceive('log')->times(1)->with('error', $exception, ['foo' => 'bar']);
152+
// $clientMock->shouldReceive('log')->times(2);
153+
// $clientMock->shouldReceive('log')->times(1)->with('error', $exception, ['foo' => 'bar']);
120154

121-
$handlerMock = \Mockery::mock(RollbarLogHandler::class, [$clientMock, $this->app]);
155+
// $handlerMock = \Mockery::mock(RollbarLogHandler::class, [$clientMock, $this->app]);
122156

123-
$handlerMock->shouldReceive('log')->passthru();
157+
// $handlerMock->shouldReceive('log')->passthru();
124158

125-
$this->app[RollbarLogHandler::class] = $handlerMock;
126-
127-
$this->app->log->info('hello');
128-
$this->app->log->error('oops');
129-
$this->app->log->error($exception, ['foo' => 'bar']);
130-
}
131-
132-
public function testErrorLevels1()
133-
{
134-
$this->app->config->set('logging.channels.rollbar.level', 'critical');
135-
136-
$clientMock = \Mockery::mock(RollbarLogger::class);
137-
$clientMock->shouldReceive('log')->times(3);
138-
$this->app[RollbarLogger::class] = $clientMock;
139-
140-
$this->app->log->debug('hello');
141-
$this->app->log->info('hello');
142-
$this->app->log->notice('hello');
143-
$this->app->log->warning('hello');
144-
$this->app->log->error('hello');
145-
$this->app->log->critical('hello');
146-
$this->app->log->alert('hello');
147-
$this->app->log->emergency('hello');
148-
}
149-
150-
public function testErrorLevels2()
151-
{
152-
$this->app->config->set('logging.channels.rollbar.level', 'debug');
153-
154-
$clientMock = \Mockery::mock(RollbarLogger::class);
155-
$clientMock->shouldReceive('log')->times(8);
156-
$this->app[RollbarLogger::class] = $clientMock;
157-
158-
$this->app->log->debug('hello');
159-
$this->app->log->info('hello');
160-
$this->app->log->notice('hello');
161-
$this->app->log->warning('hello');
162-
$this->app->log->error('hello');
163-
$this->app->log->critical('hello');
164-
$this->app->log->alert('hello');
165-
$this->app->log->emergency('hello');
166-
}
167-
168-
public function testErrorLevels3()
169-
{
170-
$this->app->config->set('logging.channels.rollbar.level', 'none');
171-
172-
$clientMock = \Mockery::mock(RollbarLogger::class);
173-
$clientMock->shouldReceive('log')->times(0);
174-
$this->app[RollbarLogger::class] = $clientMock;
175-
176-
$this->app->log->debug('hello');
177-
$this->app->log->info('hello');
178-
$this->app->log->notice('hello');
179-
$this->app->log->warning('hello');
180-
$this->app->log->error('hello');
181-
$this->app->log->critical('hello');
182-
$this->app->log->alert('hello');
183-
$this->app->log->emergency('hello');
184-
}
185-
186-
public function testPersonFunctionIsCalledWhenSessionContainsAtLeastOneItem()
187-
{
188-
$this->app->config->set('logging.channels.rollbar.person_fn', function () {
189-
return [
190-
'id' => '123',
191-
'username' => 'joebloggs',
192-
];
193-
});
194-
195-
$logger = $this->app->make(RollbarLogger::class);
196-
197-
$this->app->session->put('foo', 'bar');
198-
199-
$this->app->log->debug('hello');
200-
201-
$config = $logger->extend([]);
202-
203-
$person = $config['person'];
204-
205-
$this->assertEquals('123', $person['id']);
206-
$this->assertEquals('joebloggs', $person['username']);
207-
}
159+
// $this->app[RollbarLogHandler::class] = $handlerMock;
160+
161+
// $this->app->log->info('hello');
162+
// $this->app->log->error('oops');
163+
// $this->app->log->error($exception, ['foo' => 'bar']);
164+
// }
165+
166+
// public function testErrorLevels1()
167+
// {
168+
// $this->app->config->set('logging.channels.rollbar.level', 'critical');
169+
170+
// $clientMock = \Mockery::mock(RollbarLogger::class);
171+
// $clientMock->shouldReceive('log')->times(3);
172+
// $this->app[RollbarLogger::class] = $clientMock;
173+
174+
// $this->app->log->debug('hello');
175+
// $this->app->log->info('hello');
176+
// $this->app->log->notice('hello');
177+
// $this->app->log->warning('hello');
178+
// $this->app->log->error('hello');
179+
// $this->app->log->critical('hello');
180+
// $this->app->log->alert('hello');
181+
// $this->app->log->emergency('hello');
182+
// }
183+
184+
// public function testErrorLevels2()
185+
// {
186+
// $this->app->config->set('logging.channels.rollbar.level', 'debug');
187+
188+
// $clientMock = \Mockery::mock(RollbarLogger::class);
189+
// $clientMock->shouldReceive('log')->times(8);
190+
// $this->app[RollbarLogger::class] = $clientMock;
191+
192+
// $this->app->log->debug('hello');
193+
// $this->app->log->info('hello');
194+
// $this->app->log->notice('hello');
195+
// $this->app->log->warning('hello');
196+
// $this->app->log->error('hello');
197+
// $this->app->log->critical('hello');
198+
// $this->app->log->alert('hello');
199+
// $this->app->log->emergency('hello');
200+
// }
201+
202+
// public function testErrorLevels3()
203+
// {
204+
// $this->app->config->set('logging.channels.rollbar.level', 'none');
205+
206+
// $clientMock = \Mockery::mock(RollbarLogger::class);
207+
// $clientMock->shouldReceive('log')->times(0);
208+
// $this->app[RollbarLogger::class] = $clientMock;
209+
210+
// $this->app->log->debug('hello');
211+
// $this->app->log->info('hello');
212+
// $this->app->log->notice('hello');
213+
// $this->app->log->warning('hello');
214+
// $this->app->log->error('hello');
215+
// $this->app->log->critical('hello');
216+
// $this->app->log->alert('hello');
217+
// $this->app->log->emergency('hello');
218+
// }
219+
220+
// public function testPersonFunctionIsCalledWhenSessionContainsAtLeastOneItem()
221+
// {
222+
// $this->app->config->set('logging.channels.rollbar.person_fn', function () {
223+
// return [
224+
// 'id' => '123',
225+
// 'username' => 'joebloggs',
226+
// ];
227+
// });
228+
229+
// $logger = $this->app->make(RollbarLogger::class);
230+
231+
// $this->app->session->put('foo', 'bar');
232+
233+
// $this->app->log->debug('hello');
234+
235+
// $config = $logger->extend([]);
236+
237+
// $person = $config['person'];
238+
239+
// $this->assertEquals('123', $person['id']);
240+
// $this->assertEquals('joebloggs', $person['username']);
241+
// }
208242
}

tests/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
require_once __DIR__ . '/Helpers/AppExceptionHandler.php';

0 commit comments

Comments
 (0)