Issues (32)

Tests/Listener/CommandListenerTest.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\NewRelicBundle\Tests\Listener;
15
16
use Ekino\NewRelicBundle\Listener\CommandListener;
17
use Ekino\NewRelicBundle\NewRelic\Config;
18
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
19
use PHPUnit\Framework\TestCase;
0 ignored issues
show
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Event\ConsoleCommandEvent;
22
use Symfony\Component\Console\Event\ConsoleErrorEvent;
23
use Symfony\Component\Console\Input\ArrayInput;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputDefinition;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
class CommandListenerTest extends TestCase
30
{
31
    public function testCommandMarkedAsBackgroundJob()
32
    {
33
        if (!\class_exists('Symfony\Component\Console\Event\ConsoleCommandEvent')) {
34
            $this->markTestSkipped('Console Events is only available from Symfony 2.3');
35
        }
36
37
        $parameters = [
38
            '--foo' => true,
39
            '--foobar' => ['baz', 'baz_2'],
40
            'name' => 'bar',
41
        ];
42
43
        $definition = new InputDefinition([
44
            new InputOption('foo'),
45
            new InputOption('foobar', 'fb', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY),
46
            new InputArgument('name', InputArgument::REQUIRED),
47
         ]);
48
49
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
50
        $interactor->expects($this->once())->method('setTransactionName')->with($this->equalTo('test:newrelic'));
51
        $interactor->expects($this->once())->method('enableBackgroundJob');
52
53
        $interactor->expects($this->at(3))->method('addCustomParameter')->with('--foo', true);
54
        $interactor->expects($this->at(4))->method('addCustomParameter')->with('--foobar[0]', 'baz');
55
        $interactor->expects($this->at(5))->method('addCustomParameter')->with('--foobar[1]', 'baz_2');
56
        $interactor->expects($this->at(6))->method('addCustomParameter')->with('name', 'bar');
57
58
        $command = new Command('test:newrelic');
59
        $input = new ArrayInput($parameters, $definition);
60
61
        $output = $this->getMockBuilder(OutputInterface::class)->getMock();
62
63
        $event = new ConsoleCommandEvent($command, $input, $output);
64
65
        $listener = new CommandListener(new Config('App name', 'Token'), $interactor, []);
66
        $listener->onConsoleCommand($event);
67
    }
68
69
    public function testIgnoreBackgroundJob()
70
    {
71
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
72
        $interactor->expects($this->never())->method('startTransaction');
73
74
        $command = new Command('test:ignored-commnand');
75
        $input = new ArrayInput([], new InputDefinition([]));
76
77
        $output = $this->getMockBuilder(OutputInterface::class)->getMock();
78
79
        $event = new ConsoleCommandEvent($command, $input, $output);
80
81
        $listener = new CommandListener(new Config('App name', 'Token'), $interactor, ['test:ignored-command']);
82
        $listener->onConsoleCommand($event);
83
    }
84
85
    public function testConsoleError()
86
    {
87
        $exception = new \Exception('', 1);
88
89
        $newrelic = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
90
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
91
        $interactor->expects($this->once())->method('noticeThrowable')->with($exception);
92
93
        $command = new Command('test:exception');
94
95
        $input = new ArrayInput([], new InputDefinition([]));
96
        $output = $this->getMockBuilder(OutputInterface::class)->getMock();
97
98
        $event = new ConsoleErrorEvent($input, $output, $exception, $command);
99
100
        $listener = new CommandListener($newrelic, $interactor, ['test:exception']);
101
        $listener->onConsoleError($event);
102
    }
103
104
    public function testConsoleErrorsWithThrowable()
105
    {
106
        $exception = new \Error();
107
108
        $newrelic = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
109
        $interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
110
        $interactor->expects($this->once())->method('noticeThrowable')->with($exception);
111
        $command = new Command('test:exception');
112
113
        $input = new ArrayInput([], new InputDefinition([]));
114
        $output = $this->getMockBuilder(OutputInterface::class)->getMock();
115
116
        $event = new ConsoleErrorEvent($input, $output, $exception, $command);
117
118
        $listener = new CommandListener($newrelic, $interactor, ['test:exception']);
119
        $listener->onConsoleError($event);
120
    }
121
}
122