Skip to content

Cleanup and restructure tests #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/LogChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LogChannel extends LogManager
*
* @return Logger
*/
public function __invoke(array $config): Logger
public function __invoke(array $config = []): Logger
{
$handler = new SentryHandler(
$this->app->make(HubInterface::class),
Expand Down
27 changes: 27 additions & 0 deletions test/Sentry/ClientBuilderDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sentry\Laravel\Tests;

use Sentry\ClientBuilderInterface;

class ClientBuilderDecoratorTest extends TestCase
{
protected function getEnvironmentSetUp($app): void
{
parent::getEnvironmentSetUp($app);

$app->extend(ClientBuilderInterface::class, function (ClientBuilderInterface $clientBuilder) {
$clientBuilder->getOptions()->setEnvironment('from_service_container');

return $clientBuilder;
});
}

public function testClientHasEnvironmentSetFromDecorator(): void
{
$this->assertEquals(
'from_service_container',
$this->getClientFromContainer()->getOptions()->getEnvironment()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

namespace Sentry\Laravel\Tests;
namespace Sentry\Laravel\Tests\EventHandler;

use Illuminate\Console\Events\CommandStarting;
use Sentry\Laravel\Tests\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\BufferedOutput;

class CommandInfoInBreadcrumbsTest extends SentryLaravelTestCase
class ConsoleEventsTest extends TestCase
{
public function testCommandInfoAreRecordedWhenEnabled()
public function testCommandBreadcrumbIsRecordedWhenEnabled(): void
{
$this->resetApplicationWithConfig([
'sentry.breadcrumbs.command_info' => true,
Expand All @@ -24,7 +25,7 @@ public function testCommandInfoAreRecordedWhenEnabled()
$this->assertEquals('--foo=bar', $lastBreadcrumb->getMetadata()['input']);
}

public function testCommandInfoAreRecordedWhenDisabled()
public function testCommandBreadcrumIsNotRecordedWhenDisabled(): void
{
$this->resetApplicationWithConfig([
'sentry.breadcrumbs.command_info' => false,
Expand All @@ -37,14 +38,9 @@ public function testCommandInfoAreRecordedWhenDisabled()
$this->assertEmpty($this->getCurrentBreadcrumbs());
}

private function dispatchCommandStartEvent()
private function dispatchCommandStartEvent(): void
{
$dispatcher = $this->app['events'];

$method = method_exists($dispatcher, 'dispatch') ? 'dispatch' : 'fire';

$this->app['events']->$method(
CommandStarting::class,
$this->dispatchLaravelEvent(
new CommandStarting(
'test:command',
new ArgvInput(['artisan', '--foo=bar']),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
<?php

namespace Sentry\Laravel\Tests;
namespace Sentry\Laravel\Tests\EventHandler;

use Illuminate\Database\Connection;
use Illuminate\Database\Events\QueryExecuted;
use Mockery;
use Sentry\Laravel\Tests\TestCase;

class SqlBindingsInBreadcrumbsTest extends SentryLaravelTestCase
class DatabaseEventsTest extends TestCase
{
public function testSqlQueriesAreRecordedWhenEnabled(): void
{
$this->resetApplicationWithConfig([
'sentry.breadcrumbs.sql_queries' => true,
]);

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

$this->dispatchLaravelEvent(new QueryExecuted(
$query = 'SELECT * FROM breadcrumbs WHERE bindings = ?;',
['1'],
10,
$this->getMockedConnection()
));

$lastBreadcrumb = $this->getLastBreadcrumb();

$this->assertEquals($query, $lastBreadcrumb->getMessage());
}

public function testSqlBindingsAreRecordedWhenEnabled(): void
{
$this->resetApplicationWithConfig([
Expand All @@ -16,14 +37,11 @@ public function testSqlBindingsAreRecordedWhenEnabled(): void

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

$connection = Mockery::mock(Connection::class)
->shouldReceive('getName')->andReturn('test');

$this->dispatchLaravelEvent(new QueryExecuted(
$query = 'SELECT * FROM breadcrumbs WHERE bindings = ?;',
$bindings = ['1'],
10,
$connection
$this->getMockedConnection()
));

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

public function testSqlQueriesAreRecordedWhenDisabled(): void
{
$this->resetApplicationWithConfig([
'sentry.breadcrumbs.sql_queries' => false,
]);

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

$this->dispatchLaravelEvent(new QueryExecuted(
'SELECT * FROM breadcrumbs WHERE bindings = ?;',
['1'],
10,
$this->getMockedConnection()
));

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

public function testSqlBindingsAreRecordedWhenDisabled(): void
{
$this->resetApplicationWithConfig([
Expand All @@ -40,19 +76,22 @@ public function testSqlBindingsAreRecordedWhenDisabled(): void

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

$connection = Mockery::mock(Connection::class)
->shouldReceive('getName')->andReturn('test');

$this->dispatchLaravelEvent(new QueryExecuted(
$query = 'SELECT * FROM breadcrumbs WHERE bindings <> ?;',
['1'],
10,
$connection
$this->getMockedConnection()
));

$lastBreadcrumb = $this->getLastBreadcrumb();

$this->assertEquals($query, $lastBreadcrumb->getMessage());
$this->assertFalse(isset($lastBreadcrumb->getMetadata()['bindings']));
}

private function getMockedConnection()
{
return Mockery::mock(Connection::class)
->shouldReceive('getName')->andReturn('test');
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Sentry\Laravel\Tests;
namespace Sentry\Laravel\Tests\EventHandler;

use Illuminate\Log\Events\MessageLogged;
use Mockery;
use Sentry\Laravel\Tests\TestCase;

class LaravelLogsInBreadcrumbsTest extends SentryLaravelTestCase
class LogEventsTest extends TestCase
{
public function testLaravelLogsAreRecordedWhenEnabled(): void
{
Expand Down Expand Up @@ -36,11 +36,7 @@ public function testLaravelLogsAreRecordedWhenDisabled(): void

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

$this->dispatchLaravelEvent('illuminate.log', [
$level = 'debug',
$message = 'test message',
$context = ['1'],
]);
$this->dispatchLaravelEvent(new MessageLogged('debug', 'test message'));

$this->assertEmpty($this->getCurrentBreadcrumbs());
}
Expand Down
29 changes: 14 additions & 15 deletions test/Sentry/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,41 @@

class EventHandlerTest extends TestCase
{
use ExpectsException;

public function test_missing_event_handler_throws_exception()
public function testMissingEventHandlerThrowsException(): void
{
$handler = new EventHandler($this->app, []);

$this->safeExpectException(RuntimeException::class);
$this->expectException(RuntimeException::class);

/** @noinspection PhpUndefinedMethodInspection */
$handler->thisIsNotAHandlerAndShouldThrowAnException();
}

public function test_all_mapped_event_handlers_exist()
public function testAllMappedEventHandlersExist(): void
{
$this->tryAllEventHandlerMethods(
$this->getStaticPropertyValueFromClass(EventHandler::class, 'eventHandlerMap')
$this->getEventHandlerMapFromEventHandler('eventHandlerMap')
);
}

public function test_all_mapped_auth_event_handlers_exist()
public function testAllMappedAuthEventHandlersExist(): void
{
$this->tryAllEventHandlerMethods(
$this->getStaticPropertyValueFromClass(EventHandler::class, 'authEventHandlerMap')
$this->getEventHandlerMapFromEventHandler('authEventHandlerMap')
);
}

public function test_all_mapped_queue_event_handlers_exist()
public function testAllMappedQueueEventHandlersExist(): void
{
$this->tryAllEventHandlerMethods(
$this->getStaticPropertyValueFromClass(EventHandler::class, 'queueEventHandlerMap')
$this->getEventHandlerMapFromEventHandler('queueEventHandlerMap')
);
}

public function test_all_mapped_octane_event_handlers_exist()
public function testAllMappedOctaneEventHandlersExist(): void
{
$this->tryAllEventHandlerMethods(
$this->getStaticPropertyValueFromClass(EventHandler::class, 'octaneEventHandlerMap')
$this->getEventHandlerMapFromEventHandler('octaneEventHandlerMap')
);
}

Expand All @@ -61,12 +60,12 @@ private function tryAllEventHandlerMethods(array $methods): void
}
}

private function getStaticPropertyValueFromClass($className, $attributeName)
private function getEventHandlerMapFromEventHandler($eventHandlerMapName)
{
$class = new ReflectionClass($className);
$class = new ReflectionClass(EventHandler::class);

$attributes = $class->getStaticProperties();

return $attributes[$attributeName];
return $attributes[$eventHandlerMapName];
}
}
25 changes: 0 additions & 25 deletions test/Sentry/ExpectsException.php

This file was deleted.

7 changes: 3 additions & 4 deletions test/Sentry/Integration/ExceptionContextIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
use Sentry\Event;
use Sentry\EventHint;
use Sentry\Laravel\Integration\ExceptionContextIntegration;
use Sentry\Laravel\Tests\SentryLaravelTestCase;
use Sentry\SentrySdk;
use Sentry\Laravel\Tests\TestCase;
use Sentry\State\Scope;
use function Sentry\withScope;

class ExceptionContextIntegrationTest extends SentryLaravelTestCase
class ExceptionContextIntegrationTest extends TestCase
{
public function testExceptionContextIntegrationIsRegistered(): void
{
Expand Down Expand Up @@ -58,7 +57,7 @@ public function invokeDataProvider(): iterable
];
}

private function generateExceptionWithContext($context)
private function generateExceptionWithContext($context): Exception
{
return new class($context) extends Exception {
private $context;
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Sentry\Tracing\TransactionSource;
use function Sentry\withScope;

class IntegrationTest extends SentryLaravelTestCase
class IntegrationTest extends TestCase
{
public function testIntegrationIsRegistered(): void
{
Expand Down
Loading