Skip to content

Commit ba8b490

Browse files
authored
Cleanup and restructure tests (#596)
1 parent 0d5d9a0 commit ba8b490

21 files changed

+206
-335
lines changed

src/Sentry/Laravel/LogChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LogChannel extends LogManager
1414
*
1515
* @return Logger
1616
*/
17-
public function __invoke(array $config): Logger
17+
public function __invoke(array $config = []): Logger
1818
{
1919
$handler = new SentryHandler(
2020
$this->app->make(HubInterface::class),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Sentry\Laravel\Tests;
4+
5+
use Sentry\ClientBuilderInterface;
6+
7+
class ClientBuilderDecoratorTest extends TestCase
8+
{
9+
protected function getEnvironmentSetUp($app): void
10+
{
11+
parent::getEnvironmentSetUp($app);
12+
13+
$app->extend(ClientBuilderInterface::class, function (ClientBuilderInterface $clientBuilder) {
14+
$clientBuilder->getOptions()->setEnvironment('from_service_container');
15+
16+
return $clientBuilder;
17+
});
18+
}
19+
20+
public function testClientHasEnvironmentSetFromDecorator(): void
21+
{
22+
$this->assertEquals(
23+
'from_service_container',
24+
$this->getClientFromContainer()->getOptions()->getEnvironment()
25+
);
26+
}
27+
}

test/Sentry/CommandInfoInBreadcrumbsTest.php renamed to test/Sentry/EventHandler/ConsoleEventsTest.php

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

3-
namespace Sentry\Laravel\Tests;
3+
namespace Sentry\Laravel\Tests\EventHandler;
44

55
use Illuminate\Console\Events\CommandStarting;
6+
use Sentry\Laravel\Tests\TestCase;
67
use Symfony\Component\Console\Input\ArgvInput;
78
use Symfony\Component\Console\Output\BufferedOutput;
89

9-
class CommandInfoInBreadcrumbsTest extends SentryLaravelTestCase
10+
class ConsoleEventsTest extends TestCase
1011
{
11-
public function testCommandInfoAreRecordedWhenEnabled()
12+
public function testCommandBreadcrumbIsRecordedWhenEnabled(): void
1213
{
1314
$this->resetApplicationWithConfig([
1415
'sentry.breadcrumbs.command_info' => true,
@@ -24,7 +25,7 @@ public function testCommandInfoAreRecordedWhenEnabled()
2425
$this->assertEquals('--foo=bar', $lastBreadcrumb->getMetadata()['input']);
2526
}
2627

27-
public function testCommandInfoAreRecordedWhenDisabled()
28+
public function testCommandBreadcrumIsNotRecordedWhenDisabled(): void
2829
{
2930
$this->resetApplicationWithConfig([
3031
'sentry.breadcrumbs.command_info' => false,
@@ -37,14 +38,9 @@ public function testCommandInfoAreRecordedWhenDisabled()
3738
$this->assertEmpty($this->getCurrentBreadcrumbs());
3839
}
3940

40-
private function dispatchCommandStartEvent()
41+
private function dispatchCommandStartEvent(): void
4142
{
42-
$dispatcher = $this->app['events'];
43-
44-
$method = method_exists($dispatcher, 'dispatch') ? 'dispatch' : 'fire';
45-
46-
$this->app['events']->$method(
47-
CommandStarting::class,
43+
$this->dispatchLaravelEvent(
4844
new CommandStarting(
4945
'test:command',
5046
new ArgvInput(['artisan', '--foo=bar']),
Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
<?php
22

3-
namespace Sentry\Laravel\Tests;
3+
namespace Sentry\Laravel\Tests\EventHandler;
44

55
use Illuminate\Database\Connection;
66
use Illuminate\Database\Events\QueryExecuted;
77
use Mockery;
8+
use Sentry\Laravel\Tests\TestCase;
89

9-
class SqlBindingsInBreadcrumbsTest extends SentryLaravelTestCase
10+
class DatabaseEventsTest extends TestCase
1011
{
12+
public function testSqlQueriesAreRecordedWhenEnabled(): void
13+
{
14+
$this->resetApplicationWithConfig([
15+
'sentry.breadcrumbs.sql_queries' => true,
16+
]);
17+
18+
$this->assertTrue($this->app['config']->get('sentry.breadcrumbs.sql_queries'));
19+
20+
$this->dispatchLaravelEvent(new QueryExecuted(
21+
$query = 'SELECT * FROM breadcrumbs WHERE bindings = ?;',
22+
['1'],
23+
10,
24+
$this->getMockedConnection()
25+
));
26+
27+
$lastBreadcrumb = $this->getLastBreadcrumb();
28+
29+
$this->assertEquals($query, $lastBreadcrumb->getMessage());
30+
}
31+
1132
public function testSqlBindingsAreRecordedWhenEnabled(): void
1233
{
1334
$this->resetApplicationWithConfig([
@@ -16,14 +37,11 @@ public function testSqlBindingsAreRecordedWhenEnabled(): void
1637

1738
$this->assertTrue($this->app['config']->get('sentry.breadcrumbs.sql_bindings'));
1839

19-
$connection = Mockery::mock(Connection::class)
20-
->shouldReceive('getName')->andReturn('test');
21-
2240
$this->dispatchLaravelEvent(new QueryExecuted(
2341
$query = 'SELECT * FROM breadcrumbs WHERE bindings = ?;',
2442
$bindings = ['1'],
2543
10,
26-
$connection
44+
$this->getMockedConnection()
2745
));
2846

2947
$lastBreadcrumb = $this->getLastBreadcrumb();
@@ -32,6 +50,24 @@ public function testSqlBindingsAreRecordedWhenEnabled(): void
3250
$this->assertEquals($bindings, $lastBreadcrumb->getMetadata()['bindings']);
3351
}
3452

53+
public function testSqlQueriesAreRecordedWhenDisabled(): void
54+
{
55+
$this->resetApplicationWithConfig([
56+
'sentry.breadcrumbs.sql_queries' => false,
57+
]);
58+
59+
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.sql_queries'));
60+
61+
$this->dispatchLaravelEvent(new QueryExecuted(
62+
'SELECT * FROM breadcrumbs WHERE bindings = ?;',
63+
['1'],
64+
10,
65+
$this->getMockedConnection()
66+
));
67+
68+
$this->assertEmpty($this->getCurrentBreadcrumbs());
69+
}
70+
3571
public function testSqlBindingsAreRecordedWhenDisabled(): void
3672
{
3773
$this->resetApplicationWithConfig([
@@ -40,19 +76,22 @@ public function testSqlBindingsAreRecordedWhenDisabled(): void
4076

4177
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.sql_bindings'));
4278

43-
$connection = Mockery::mock(Connection::class)
44-
->shouldReceive('getName')->andReturn('test');
45-
4679
$this->dispatchLaravelEvent(new QueryExecuted(
4780
$query = 'SELECT * FROM breadcrumbs WHERE bindings <> ?;',
4881
['1'],
4982
10,
50-
$connection
83+
$this->getMockedConnection()
5184
));
5285

5386
$lastBreadcrumb = $this->getLastBreadcrumb();
5487

5588
$this->assertEquals($query, $lastBreadcrumb->getMessage());
5689
$this->assertFalse(isset($lastBreadcrumb->getMetadata()['bindings']));
5790
}
91+
92+
private function getMockedConnection()
93+
{
94+
return Mockery::mock(Connection::class)
95+
->shouldReceive('getName')->andReturn('test');
96+
}
5897
}

test/Sentry/LaravelLogsInBreadcrumbsTest.php renamed to test/Sentry/EventHandler/LogEventsTest.php

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

3-
namespace Sentry\Laravel\Tests;
3+
namespace Sentry\Laravel\Tests\EventHandler;
44

55
use Illuminate\Log\Events\MessageLogged;
6-
use Mockery;
6+
use Sentry\Laravel\Tests\TestCase;
77

8-
class LaravelLogsInBreadcrumbsTest extends SentryLaravelTestCase
8+
class LogEventsTest extends TestCase
99
{
1010
public function testLaravelLogsAreRecordedWhenEnabled(): void
1111
{
@@ -36,11 +36,7 @@ public function testLaravelLogsAreRecordedWhenDisabled(): void
3636

3737
$this->assertFalse($this->app['config']->get('sentry.breadcrumbs.logs'));
3838

39-
$this->dispatchLaravelEvent('illuminate.log', [
40-
$level = 'debug',
41-
$message = 'test message',
42-
$context = ['1'],
43-
]);
39+
$this->dispatchLaravelEvent(new MessageLogged('debug', 'test message'));
4440

4541
$this->assertEmpty($this->getCurrentBreadcrumbs());
4642
}

test/Sentry/EventHandlerTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,41 @@
99

1010
class EventHandlerTest extends TestCase
1111
{
12-
use ExpectsException;
13-
14-
public function test_missing_event_handler_throws_exception()
12+
public function testMissingEventHandlerThrowsException(): void
1513
{
1614
$handler = new EventHandler($this->app, []);
1715

18-
$this->safeExpectException(RuntimeException::class);
16+
$this->expectException(RuntimeException::class);
1917

18+
/** @noinspection PhpUndefinedMethodInspection */
2019
$handler->thisIsNotAHandlerAndShouldThrowAnException();
2120
}
2221

23-
public function test_all_mapped_event_handlers_exist()
22+
public function testAllMappedEventHandlersExist(): void
2423
{
2524
$this->tryAllEventHandlerMethods(
26-
$this->getStaticPropertyValueFromClass(EventHandler::class, 'eventHandlerMap')
25+
$this->getEventHandlerMapFromEventHandler('eventHandlerMap')
2726
);
2827
}
2928

30-
public function test_all_mapped_auth_event_handlers_exist()
29+
public function testAllMappedAuthEventHandlersExist(): void
3130
{
3231
$this->tryAllEventHandlerMethods(
33-
$this->getStaticPropertyValueFromClass(EventHandler::class, 'authEventHandlerMap')
32+
$this->getEventHandlerMapFromEventHandler('authEventHandlerMap')
3433
);
3534
}
3635

37-
public function test_all_mapped_queue_event_handlers_exist()
36+
public function testAllMappedQueueEventHandlersExist(): void
3837
{
3938
$this->tryAllEventHandlerMethods(
40-
$this->getStaticPropertyValueFromClass(EventHandler::class, 'queueEventHandlerMap')
39+
$this->getEventHandlerMapFromEventHandler('queueEventHandlerMap')
4140
);
4241
}
4342

44-
public function test_all_mapped_octane_event_handlers_exist()
43+
public function testAllMappedOctaneEventHandlersExist(): void
4544
{
4645
$this->tryAllEventHandlerMethods(
47-
$this->getStaticPropertyValueFromClass(EventHandler::class, 'octaneEventHandlerMap')
46+
$this->getEventHandlerMapFromEventHandler('octaneEventHandlerMap')
4847
);
4948
}
5049

@@ -61,12 +60,12 @@ private function tryAllEventHandlerMethods(array $methods): void
6160
}
6261
}
6362

64-
private function getStaticPropertyValueFromClass($className, $attributeName)
63+
private function getEventHandlerMapFromEventHandler($eventHandlerMapName)
6564
{
66-
$class = new ReflectionClass($className);
65+
$class = new ReflectionClass(EventHandler::class);
6766

6867
$attributes = $class->getStaticProperties();
6968

70-
return $attributes[$attributeName];
69+
return $attributes[$eventHandlerMapName];
7170
}
7271
}

test/Sentry/ExpectsException.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/Sentry/Integration/ExceptionContextIntegrationTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
use Sentry\Event;
77
use Sentry\EventHint;
88
use Sentry\Laravel\Integration\ExceptionContextIntegration;
9-
use Sentry\Laravel\Tests\SentryLaravelTestCase;
10-
use Sentry\SentrySdk;
9+
use Sentry\Laravel\Tests\TestCase;
1110
use Sentry\State\Scope;
1211
use function Sentry\withScope;
1312

14-
class ExceptionContextIntegrationTest extends SentryLaravelTestCase
13+
class ExceptionContextIntegrationTest extends TestCase
1514
{
1615
public function testExceptionContextIntegrationIsRegistered(): void
1716
{
@@ -58,7 +57,7 @@ public function invokeDataProvider(): iterable
5857
];
5958
}
6059

61-
private function generateExceptionWithContext($context)
60+
private function generateExceptionWithContext($context): Exception
6261
{
6362
return new class($context) extends Exception {
6463
private $context;

test/Sentry/IntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Sentry\Tracing\TransactionSource;
1313
use function Sentry\withScope;
1414

15-
class IntegrationTest extends SentryLaravelTestCase
15+
class IntegrationTest extends TestCase
1616
{
1717
public function testIntegrationIsRegistered(): void
1818
{

0 commit comments

Comments
 (0)