Skip to content

Add basic E2E test #277

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 26 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2ce0564
Add basic E2E test
Jean85 Nov 25, 2019
be53fa7
Fix test issues
Jean85 Nov 25, 2019
a936aad
Unignore subfolders
Jean85 Nov 25, 2019
7aaef67
Force minimum version for FrameworkBundle to avoid PHPUnit issues
Jean85 Nov 25, 2019
f1b3f2c
Allow PHPUnit 6 to avoid issues with --prefer-lowest
Jean85 Nov 25, 2019
522c8ea
Rework PHPunit prefer lowest
Jean85 Nov 25, 2019
91a67e4
Change update order
Jean85 Nov 25, 2019
67cc7bc
Try with a direct removal
Jean85 Nov 25, 2019
d5169c8
Try again
Jean85 Nov 25, 2019
8c70ade
Remove more stuff
Jean85 Nov 25, 2019
42b9d5f
Try class_alias
Jean85 Nov 25, 2019
146af27
Add a test for 500
Jean85 Nov 26, 2019
ad28cd3
Fix config for prefer-lowest
Jean85 Nov 26, 2019
e83a9a4
Make E2E tests backward compatible
Jean85 Nov 26, 2019
8e950d1
Fix PHPStan errors
Jean85 Nov 26, 2019
ae23bfb
Add Symfony deprecation handler
Jean85 Nov 26, 2019
de483aa
Remove all deprecations from Sentry
Jean85 Nov 26, 2019
cd15792
Remove deprecation from Symfony exception event listener
Jean85 Nov 27, 2019
b0a5ade
Fix test (and bug) in listener registration
Jean85 Nov 27, 2019
70ed51f
Fix usage of newer feature in test suite
Jean85 Nov 27, 2019
bb1ea0c
Avoid deprecation from dev dependency
Jean85 Nov 27, 2019
3a26525
Allow CI failures of sentry/sentry dev-develop
Jean85 Nov 28, 2019
2712152
Add more E2E tests
Jean85 Nov 28, 2019
2e7e2b3
Drop support for Symfony < 3.4
Jean85 Nov 29, 2019
2c3ab99
Try to avoid bug on prefer-lowest by limiting symfony/monolog-bundle
Jean85 Nov 29, 2019
98acc40
Fix prefer-lowest, again
Jean85 Nov 29, 2019
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
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ jobs:
php: 7.1
name: prefer-lowest
install:
- composer remove --dev friendsofphp/php-cs-fixer phpstan/phpstan --no-update
- travis_retry travis_wait composer install --no-interaction --prefer-dist
- composer remove --dev friendsofphp/php-cs-fixer phpstan/phpstan phpstan/phpstan-phpunit jangregor/phpstan-prophecy --no-update
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable --prefer-lowest
- name: sentry/sentry dev-develop
install: composer require sentry/sentry:dev-develop
- stage: Code style and static analysis
script:
- composer phpstan
Expand All @@ -45,6 +43,10 @@ jobs:
- phpdbg -qrr vendor/bin/phpunit --coverage-clover clover.xml
after_success:
- vendor/bin/ocular code-coverage:upload --format=php-clover clover.xml --revision=$TRAVIS_COMMIT
allow_failures:
- name: sentry/sentry dev-develop
stage: Test
install: composer require sentry/sentry:dev-develop

notifications:
webhooks:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
- Drop support for Symfony < 3.4 (#277)
- Fix handling of command with no name on `ConsoleListener` (#261)
- Remove check by AuthorizationChecker in `RequestListener` (#264)
- Fixed undefined variable in `RequestListener` (#263)
Expand Down
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"php": "^7.1",
"jean85/pretty-package-versions": "^1.0",
"sentry/sdk": "^2.0",
"symfony/config": "^2.8||^3.0||^4.0",
"symfony/console": "^2.8||^3.0||^4.0",
"symfony/dependency-injection": "^2.8||^3.0||^4.0",
"symfony/event-dispatcher": "^2.8||^3.0||^4.0",
"symfony/http-kernel": "^2.8||^3.0||^4.0",
"symfony/security-core": "^2.8||^3.0||^4.0"
"symfony/config": "^3.4||^4.0",
"symfony/console": "^3.4||^4.0",
"symfony/dependency-injection": "^3.4||^4.0",
"symfony/event-dispatcher": "^3.4||^4.0",
"symfony/http-kernel": "^3.4||^4.0",
"symfony/security-core": "^3.4||^4.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.8",
Expand All @@ -38,7 +38,12 @@
"phpstan/phpstan-phpunit": "^0.11",
"phpunit/phpunit": "^7.5",
"scrutinizer/ocular": "^1.4",
"symfony/expression-language": "^2.8||^3.0||^4.0"
"symfony/browser-kit": "^3.4||^4.0",
"symfony/expression-language": "^3.4||^4.0",
"symfony/framework-bundle": "^3.4||^4.0",
"symfony/monolog-bundle": "^3.4",
"symfony/phpunit-bridge": "^5.0",
"symfony/yaml": "^3.4||^4.0"
},
"suggest": {
"monolog/monolog": "Required to use the Monolog handler"
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
- '/Parameter \$.+ of method Sentry\\SentryBundle\\EventListener\\ErrorListener::onConsoleException\(\) has invalid typehint type Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
- '/Call to method getException\(\) on an unknown class Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
- '/Access to undefined constant Symfony\\Component\\Console\\ConsoleEvents::EXCEPTION./'
- '/Class PHPUnit_Framework_TestCase not found/'

includes:
- vendor/jangregor/phpstan-prophecy/src/extension.neon
Expand Down
10 changes: 8 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.5/phpunit.xsd"
colors="true"
backupGlobals="false"
bootstrap="vendor/autoload.php">
bootstrap="vendor/autoload.php"
processIsolation="true"
>

<testsuites>
<testsuite name="SentryBundle test suite">
Expand All @@ -17,4 +19,8 @@
<directory>./src</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
25 changes: 24 additions & 1 deletion src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* This is the class that loads and manages your bundle configuration
Expand Down Expand Up @@ -146,17 +149,37 @@ private function configureErrorListener(ContainerBuilder $container, array $proc
return;
}

$this->tagExceptionListener($container);
$this->tagConsoleErrorListener($container);
}

/**
* BC layer for Symfony < 4.3
*/
private function tagExceptionListener(ContainerBuilder $container): void
{
$listener = $container->getDefinition(ErrorListener::class);
$method = class_exists(ExceptionEvent::class)
? 'onException'
: 'onKernelException';

$tagAttributes = [
'event' => KernelEvents::EXCEPTION,
'method' => $method,
'priority' => '%sentry.listener_priorities.request_error%',
];

$listener->addTag('kernel.event_listener', $tagAttributes);
}

/**
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
*/
private function tagConsoleErrorListener(ContainerBuilder $container): void
{
$listener = $container->getDefinition(ErrorListener::class);

if (class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')) {
if (class_exists(ConsoleErrorEvent::class)) {
$tagAttributes = [
'event' => ConsoleEvents::ERROR,
'method' => 'onConsoleError',
Expand Down
9 changes: 9 additions & 0 deletions src/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sentry\State\HubInterface;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

final class ErrorListener
Expand All @@ -21,6 +22,14 @@ public function __construct(HubInterface $hub)
$this->hub = $hub; // not used, needed to trigger instantiation
}

public function onException(ExceptionEvent $event): void
{
\Sentry\captureException($event->getThrowable());
}

/**
* BC layer for Symfony < 4.3
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
{
\Sentry\captureException($event->getException());
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

<service id="Sentry\SentryBundle\EventListener\ErrorListener" class="Sentry\SentryBundle\EventListener\ErrorListener" public="false">
<argument type="service" id="Sentry\State\HubInterface" />
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="%sentry.listener_priorities.request_error%" />
<!-- The following tag is done manually in PHP for BC with Symfony < 4.3, -->
<!-- <tag name="kernel.event_listener" event="kernel.exception" method="onException" priority="%sentry.listener_priorities.request_error%" />-->
<!-- The following tag is done manually in PHP for BC with Symfony < 3.3 -->
<!-- <tag name="kernel.event_listener" event="console.error" method="onConsoleError" priority="%sentry.listener_priorities.console_error%" />-->
</service>
Expand Down
2 changes: 2 additions & 0 deletions src/SentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function setCurrentHub(HubInterface $hub): void
{
if (class_exists(SentrySdk::class)) {
SentrySdk::setCurrentHub($hub);

return;
}

Hub::setCurrent($hub);
Expand Down
12 changes: 12 additions & 0 deletions test/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PHPUnit\Framework\TestCase;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;

abstract class BaseTestCase extends TestCase
{
Expand Down Expand Up @@ -31,4 +34,13 @@ protected function getSupportedOptionsCount(): int

return $count;
}

protected function setCurrentHub(HubInterface $hub): void
{
if (class_exists(SentrySdk::class)) {
SentrySdk::setCurrentHub($hub);
} else {
Hub::setCurrent($hub);
}
}
}
13 changes: 6 additions & 7 deletions test/Command/SentryTestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

namespace Sentry\SentryBundle\Test\Command;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sentry\ClientInterface;
use Sentry\Options;
use Sentry\SentryBundle\Command\SentryTestCommand;
use Sentry\State\Hub;
use Sentry\SentryBundle\Test\BaseTestCase;
use Sentry\State\HubInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class SentryTestCommandTest extends TestCase
class SentryTestCommandTest extends BaseTestCase
{
public function testExecuteSuccessfully(): void
{
Expand All @@ -29,7 +28,7 @@ public function testExecuteSuccessfully(): void
->shouldBeCalled()
->willReturn($lastEventId);

Hub::setCurrent($hub->reveal());
$this->setCurrentHub($hub->reveal());

$commandTester = $this->executeCommand();

Expand All @@ -51,7 +50,7 @@ public function testExecuteFailsDueToMissingDSN(): void
$hub->getClient()
->willReturn($client->reveal());

Hub::setCurrent($hub->reveal());
$this->setCurrentHub($hub->reveal());

$commandTester = $this->executeCommand();

Expand All @@ -75,7 +74,7 @@ public function testExecuteFailsDueToMessageNotSent(): void
->shouldBeCalled()
->willReturn(null);

Hub::setCurrent($hub->reveal());
$this->setCurrentHub($hub->reveal());

$commandTester = $this->executeCommand();

Expand All @@ -92,7 +91,7 @@ public function testExecuteFailsDueToMissingClient(): void
$hub->getClient()
->willReturn(null);

Hub::setCurrent($hub->reveal());
$this->setCurrentHub($hub->reveal());

$commandTester = $this->executeCommand();

Expand Down
2 changes: 1 addition & 1 deletion test/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testOptionsDefaultValues(): void

$this->assertNull($options->getDsn());
$this->assertSame('test', $options->getEnvironment());
$this->assertSame(['var/cache', $vendorDir], $options->getInAppExcludedPaths());
$this->assertSame([realpath('./var/cache'), $vendorDir], $options->getInAppExcludedPaths());

$this->assertSame(1, $container->getParameter('sentry.listener_priorities.request'));
$this->assertSame(1, $container->getParameter('sentry.listener_priorities.sub_request'));
Expand Down
51 changes: 51 additions & 0 deletions test/End2End/App/Controller/MainController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Sentry\SentryBundle\Test\End2End\App\Controller;

use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class MainController
{
/** @var HubInterface */
private $sentry;

/** @var RequestStack */
private $requestStack;

/** @var HttpKernelInterface */
private $kernel;

public function __construct(HubInterface $sentry, RequestStack $requestStack, HttpKernelInterface $kernel)
{
$this->sentry = $sentry;
$this->requestStack = $requestStack;
$this->kernel = $kernel;
}

public function exception(): Response
{
throw new \RuntimeException('This is an intentional error');
}

public function index(): Response
{
$this->sentry->captureMessage('Hello there');

return new Response('Hello there');
}

public function subrequest(): Response
{
$request = $this->requestStack->getCurrentRequest();
assert($request instanceof Request);
$path['_controller'] = __CLASS__ . '::index';

$subRequest = $request->duplicate([], null, $path);

return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
}
25 changes: 25 additions & 0 deletions test/End2End/App/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Sentry\SentryBundle\Test\End2End\App;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;

class Kernel extends SymfonyKernel
{
public function registerBundles()
{
$bundles = [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Symfony\Bundle\MonologBundle\MonologBundle(),
new \Sentry\SentryBundle\SentryBundle(),
];

return $bundles;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__ . '/config.yml');
}
}
21 changes: 21 additions & 0 deletions test/End2End/App/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
framework:
router: { resource: "%kernel.root_dir%/routing.yml" }
secret: secret
test: ~

services:
test.hub:
alias: Sentry\State\HubInterface
public: true

Sentry\SentryBundle\Test\End2End\App\Controller\MainController:
autowire: true
tags:
- controller.service_arguments

monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
15 changes: 15 additions & 0 deletions test/End2End/App/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exception:
path: /exception
defaults: { _controller: 'Sentry\SentryBundle\Test\End2End\App\Controller\MainController::exception' }

200:
path: /200
defaults: { _controller: 'Sentry\SentryBundle\Test\End2End\App\Controller\MainController::index' }

secured200:
path: /secured/200
defaults: { _controller: 'Sentry\SentryBundle\Test\End2End\App\Controller\MainController::index' }

subrequest:
path: /subrequest
defaults: { _controller: 'Sentry\SentryBundle\Test\End2End\App\Controller\MainController::subrequest' }
Loading