/
ConsoleListenerTest.php
91 lines (68 loc) · 2.68 KB
/
ConsoleListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Sentry\SentryBundle\Test\EventListener;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sentry\Event;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
class ConsoleListenerTest extends TestCase
{
private $currentHub;
private $currentScope;
protected function setUp()
{
parent::setUp();
$this->currentScope = $scope = new Scope();
$this->currentHub = $this->prophesize(HubInterface::class);
$this->currentHub->configureScope(Argument::type('callable'))
->shouldBeCalled()
->will(function ($arguments) use ($scope): void {
$callable = $arguments[0];
$callable($scope);
});
Hub::setCurrent($this->currentHub->reveal());
}
public function testOnConsoleCommandAddsCommandName(): void
{
$command = $this->prophesize(Command::class);
$command->getName()
->willReturn('sf:command:name');
$event = $this->prophesize(ConsoleCommandEvent::class);
$event->getCommand()
->willReturn($command->reveal());
$listener = new ConsoleListener($this->currentHub->reveal());
$listener->onConsoleCommand($event->reveal());
$this->assertSame(['command' => 'sf:command:name'], $this->getTagsContext($this->currentScope));
}
public function testOnConsoleCommandWithNoCommandAddsPlaceholder(): void
{
$event = $this->prophesize(ConsoleCommandEvent::class);
$event->getCommand()
->willReturn(null);
$listener = new ConsoleListener($this->currentHub->reveal());
$listener->onConsoleCommand($event->reveal());
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
}
public function testOnConsoleCommandWithNoCommandNameAddsPlaceholder(): void
{
$command = $this->prophesize(Command::class);
$command->getName()
->willReturn(null);
$event = $this->prophesize(ConsoleCommandEvent::class);
$event->getCommand()
->willReturn($command->reveal());
$listener = new ConsoleListener($this->currentHub->reveal());
$listener->onConsoleCommand($event->reveal());
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
}
private function getTagsContext(Scope $scope): array
{
$event = new Event();
$scope->applyToEvent($event, []);
return $event->getTagsContext()->toArray();
}
}