Skip to content

Commit 2ac9f7a

Browse files
committed
issue #9: tests
1 parent 7ff5d55 commit 2ac9f7a

File tree

4 files changed

+122
-35
lines changed

4 files changed

+122
-35
lines changed

EventListener/AbstractListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SymfonyRollbarBundle\EventListener;
44

5+
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
56
use Symfony\Component\Console\ConsoleEvents;
67
use Symfony\Component\DependencyInjection\ContainerInterface;
78
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Tests/Fixtures/MyExceptionCommand.php

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\SymfonyRollbarBundle\EventListener;
4+
5+
use Monolog\Logger;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Symfony\Component\Console\ConsoleEvents;
8+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
10+
use Symfony\Component\Console\Input\ArrayInput;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Output\StreamOutput;
13+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
14+
use SymfonyRollbarBundle\Command\DeployCommand;
15+
use SymfonyRollbarBundle\EventListener\AbstractListener;
16+
use SymfonyRollbarBundle\Tests\Fixtures\ErrorHandler;
17+
18+
/**
19+
* Class ConsoleListenerTest
20+
* @package SymfonyRollbarBundle\Tests\SymfonyRollbarBundle\EventListener
21+
* @runTestsInSeparateProcesses
22+
*/
23+
class ConsoleListenerTest extends KernelTestCase
24+
{
25+
public function setUp()
26+
{
27+
parent::setUp();
28+
static::bootKernel();
29+
}
30+
31+
/**
32+
* @dataProvider provideLegacyEvents
33+
*
34+
* @param $error
35+
* @param $event
36+
*/
37+
public function testLegacyConsoleException($error, $event)
38+
{
39+
$container = static::$kernel->getContainer();
40+
41+
/**
42+
* @var TraceableEventDispatcher $eventDispatcher
43+
*/
44+
$eventDispatcher = $container->get('event_dispatcher');
45+
46+
$key = '';
47+
if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
48+
$key = class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')
49+
? ConsoleEvents::ERROR
50+
: ConsoleEvents::EXCEPTION;
51+
} else {
52+
$this->markTestSkipped('Nothing to test.');
53+
}
54+
55+
foreach ($eventDispatcher->getListeners('kernel.exception') as $listener) {
56+
$eventDispatcher->removeListener('kernel.exception', $listener);
57+
}
58+
59+
$handler = new ErrorHandler();
60+
$handler->setAssert(function ($record) use ($error) {
61+
$this->assertNotEmpty($record);
62+
63+
$this->assertNotEmpty($record['context']['exception']);
64+
$exception = $record['context']['exception'];
65+
66+
$this->assertEquals($error->getMessage(), $record['message']);
67+
$this->assertEquals(Logger::ERROR, $record['level']);
68+
69+
$this->assertInstanceOf(\Exception::class, $exception);
70+
});
71+
72+
foreach ($eventDispatcher->getListeners($key) as $listener) {
73+
/**
74+
* @var AbstractListener $listener
75+
*/
76+
if (!$listener[0] instanceof AbstractListener) {
77+
// disable default symfony listeners
78+
$eventDispatcher->removeListener($key, $listener);
79+
continue;
80+
}
81+
82+
$listener[0]->getLogger()->setHandlers([$handler]);
83+
}
84+
85+
$eventDispatcher->dispatch($key, $event);
86+
restore_error_handler();
87+
}
88+
89+
/**
90+
* @return array
91+
*/
92+
public function provideLegacyEvents()
93+
{
94+
$input = new ArrayInput([]);
95+
$output = new StreamOutput(
96+
fopen('php://memory', 'w', false),
97+
OutputInterface::VERBOSITY_QUIET,
98+
false
99+
);
100+
101+
$error = new \Exception('This is console exception');
102+
$command = new DeployCommand();
103+
104+
$events = [];
105+
106+
if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
107+
if (class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')) {
108+
$events[] = [$error, new ConsoleErrorEvent($input, $output, $error, $command)];
109+
}
110+
111+
if (class_exists('\Symfony\Component\Console\Event\ConsoleExceptionEvent')) {
112+
$events[] = [$error, new ConsoleExceptionEvent($command, $input, $output, $error, 1)];
113+
}
114+
}
115+
116+
117+
return $events;
118+
}
119+
}

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
backupGlobals="false"
77
colors="true"
88
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
911
bootstrap="./Tests/Fixtures/app/autoload.php"
1012
>
1113
<listeners>

0 commit comments

Comments
 (0)