Skip to content

Commit 29beaf4

Browse files
committed
Fixed and refactored the tests.
1 parent 4745a1a commit 29beaf4

File tree

4 files changed

+89
-34
lines changed

4 files changed

+89
-34
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"Rollbar\\Laravel\\": "src/"
3939
}
4040
},
41+
"autoload-dev": {
42+
"psr-4": {
43+
"Rollbar\\Laravel\\Tests\\": "tests/"
44+
}
45+
},
4146
"extra": {
4247
"laravel": {
4348
"providers": [

phpunit.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
34
backupStaticAttributes="false"
45
bootstrap="tests/bootstrap.php"
56
colors="true"
@@ -8,15 +9,15 @@
89
convertWarningsToExceptions="true"
910
processIsolation="false"
1011
stopOnFailure="true"
11-
>
12+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
13+
<coverage processUncoveredFiles="true">
14+
<include>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
1218
<testsuites>
1319
<testsuite name="Test Suite">
1420
<directory suffix=".php">./tests/</directory>
1521
</testsuite>
1622
</testsuites>
17-
<filter>
18-
<whitelist processUncoveredFilesFromWhitelist="true">
19-
<directory suffix=".php">./src</directory>
20-
</whitelist>
21-
</filter>
2223
</phpunit>

tests/RollbarTest.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,12 @@
22

33
namespace Rollbar\Laravel\Tests;
44

5-
use Rollbar\Laravel\RollbarServiceProvider;
65
use Rollbar\Laravel\MonologHandler;
76
use Rollbar\Laravel\AgentHandler;
87
use Rollbar\RollbarLogger;
9-
use Monolog\Logger;
10-
use Mockery;
118

12-
class RollbarTest extends \Orchestra\Testbench\TestCase
9+
class RollbarTest extends TestCase
1310
{
14-
private string $access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';
15-
16-
protected function setUp(): void
17-
{
18-
putenv('ROLLBAR_TOKEN=' . $this->access_token);
19-
20-
parent::setUp();
21-
22-
Mockery::close();
23-
}
24-
25-
protected function getPackageProviders($app)
26-
{
27-
return [RollbarServiceProvider::class];
28-
}
29-
3011
public function testBinding()
3112
{
3213
$client = $this->app->make(RollbarLogger::class);
@@ -55,28 +36,31 @@ public function testPassConfiguration()
5536

5637
public function testCustomConfiguration()
5738
{
58-
$this->app->config->set('logging.channels.rollbar.root', '/tmp');
59-
$this->app->config->set('logging.channels.rollbar.included_errno', E_ERROR);
60-
$this->app->config->set('logging.channels.rollbar.environment', 'staging');
39+
$this->refreshApplicationWithConfig([
40+
'logging.channels.rollbar.root' => '/tmp',
41+
'logging.channels.rollbar.included_errno' => E_ERROR,
42+
'logging.channels.rollbar.environment' => 'staging',
43+
]);
6144

45+
/** @var RollbarLogger $client */
6246
$client = $this->app->make(RollbarLogger::class);
63-
$config = $client->extend([]);
64-
47+
$config = $client->getConfig()->getConfigArray();
48+
6549
$this->assertEquals('staging', $config['environment']);
6650
$this->assertEquals('/tmp', $config['root']);
6751
$this->assertEquals(E_ERROR, $config['included_errno']);
6852
}
6953

7054
public function testRollbarAgentConfigurationAdapter()
7155
{
72-
$this->app->config->set('logging.channels.rollbar.handler', AgentHandler::class);
56+
$this->refreshApplicationWithConfig(['logging.channels.rollbar.handler' => AgentHandler::class]);
7357

7458
$client = $this->app->make(RollbarLogger::class);
7559
$config = $client->extend([]);
7660

7761
$this->assertEquals(
7862
'agent',
79-
$config['handler'],
63+
$config['handler'] ?? null,
8064
'AgentHandler given as Laravel logging config handler should be given as "agent" to Rollbar::init'
8165
);
8266
}

tests/TestCase.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Rollbar\Laravel\Tests;
4+
5+
use Illuminate\Foundation\Application;
6+
use Mockery;
7+
use Rollbar\Laravel\RollbarServiceProvider;
8+
9+
abstract class TestCase extends \Orchestra\Testbench\TestCase
10+
{
11+
protected string $access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';
12+
13+
protected array $appConfig = [];
14+
15+
protected function setUp(): void
16+
{
17+
putenv('ROLLBAR_TOKEN=' . $this->access_token);
18+
19+
parent::setUp();
20+
21+
Mockery::close();
22+
}
23+
24+
/**
25+
* Set up the environment.
26+
*
27+
* @param $app
28+
* @return void
29+
*/
30+
protected function defineEnvironment($app): void
31+
{
32+
$configs = array_merge(
33+
[
34+
'logging.channels.rollbar.driver' => 'rollbar',
35+
'logging.channels.rollbar.level' => 'debug',
36+
'logging.channels.rollbar.access_token' => env('ROLLBAR_TOKEN'),
37+
],
38+
$this->appConfig,
39+
);
40+
foreach ($configs as $key => $value) {
41+
$app['config']->set($key, $value);
42+
}
43+
}
44+
45+
/**
46+
* @param Application $app The Laravel application.
47+
* @return class-string[] The service providers to register.
48+
*/
49+
protected function getPackageProviders($app): array
50+
{
51+
return [RollbarServiceProvider::class];
52+
}
53+
54+
/**
55+
* Creates a new Laravel application with the given configuration and sets it as the active application.
56+
*
57+
* @param array $config The configuration to use.
58+
* @return void
59+
*/
60+
protected function refreshApplicationWithConfig(array $config): void
61+
{
62+
$this->appConfig = $config;
63+
$this->refreshApplication();
64+
}
65+
}

0 commit comments

Comments
 (0)