Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConsoleTest.php « System « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a82d31ecfb549cf0b5882169b51ec12e5f66ba52 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\System;

use Piwik\CliMulti\CliPhp;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Development;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugins\Monolog\Handler\FailureLogMessageDetector;
use Piwik\Tests\Framework\Fixture;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase;

class TestCommandWithWarning extends ConsoleCommand
{
    public function configure()
    {
        parent::configure();

        $this->setName('test-command-with-warning');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        StaticContainer::get(LoggerInterface::class)->warning('warn');
    }
}

class TestCommandWithError extends ConsoleCommand
{
    public function configure()
    {
        parent::configure();

        $this->setName('test-command-with-error');
        $this->addOption('no-error', null, InputOption::VALUE_NONE);
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        if (!$input->getOption('no-error')) {
            StaticContainer::get(LoggerInterface::class)->error('error');
        }
    }
}

class TestCommandWithFatalError extends ConsoleCommand
{
    public function configure()
    {
        parent::configure();

        $this->setName('test-command-with-fatal-error');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            \Piwik\ErrorHandler::pushFatalErrorBreadcrumb(static::class);

            $this->executeImpl($input, $output);
        } finally {
            \Piwik\ErrorHandler::popFatalErrorBreadcrumb();
        }
    }

    public function executeImpl(InputInterface $input, OutputInterface $output)
    {
        try {
            \Piwik\ErrorHandler::pushFatalErrorBreadcrumb(static::class, []);

            $val = "";
            while (true) {
                $val .= str_repeat("*", 1024 * 1024 * 1024);
            }
        } finally {
            \Piwik\ErrorHandler::popFatalErrorBreadcrumb();
        }
    }
}

class TestCommandWithException extends ConsoleCommand
{
    public function configure()
    {
        parent::configure();

        $this->setName('test-command-with-exception');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        throw new \Exception('test error');
    }
}

/**
 * @group ConsoleTest
 */
class ConsoleTest extends ConsoleCommandTestCase
{
    public function setUp(): void
    {
        parent::setUp();
        $this->application->addCommands([
            new TestCommandWithWarning(),
            new TestCommandWithError(),
        ]);

        StaticContainer::get(FailureLogMessageDetector::class)->reset();
    }

    public function test_Console_ReturnsCorrectExitCode_IfCommandEmitsWarning()
    {
        $exitCode = $this->applicationTester->run([
            'command' => 'test-command-with-warning',
        ]);
        $this->assertEquals(1, $exitCode);
    }

    public function test_Console_ReturnsCorrectExitCode_IfCommandEmitsError()
    {
        $exitCode = $this->applicationTester->run([
            'command' => 'test-command-with-error',
        ]);
        $this->assertEquals(1, $exitCode);
    }

    public function test_Console_ReturnsCorrectExitCode_IfCommandDoesNotEmitAnything()
    {
        $exitCode = $this->applicationTester->run([
            'command' => 'test-command-with-error',
            '--no-error' => true,
        ]);
        $this->assertEquals(0, $exitCode);
    }

    public function test_Console_handlesFatalErrorsCorrectly()
    {
        $cliPhp = new CliPhp();
        $php = $cliPhp->findPhpBinary();
        $command = $php . " -i | grep 'memory_limit => -1'";

        $output = shell_exec($command);

        if ($output == "memory_limit => -1 => -1\n") {
            $this->markTestSkipped("no memory limit in php-cli");
        }

        $command = Fixture::getCliCommandBase();
        $command .= ' test-command-with-fatal-error';
        $command .= ' 2>&1';

        $output = shell_exec($command);
        $output = $this->normalizeOutput($output);

        $expected = <<<END
PHP Fatal error:  Allowed memory size of X bytes exhausted (tried to allocate X bytes) in /tests/PHPUnit/System/ConsoleTest.php on line 85

Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes) in /tests/PHPUnit/System/ConsoleTest.php on line 85
*** IN SAFEMODE ***
Matomo encountered an error: Allowed memory size of X bytes exhausted (tried to allocate X bytes) (which lead to: Error: array (
  'type' => 1,
  'message' => 'Allowed memory size of X bytes exhausted (tried to allocate X bytes)',
  'file' => '/tests/PHPUnit/System/ConsoleTest.php',
  'line' => 85,
  'backtrace' => ' on /tests/PHPUnit/System/ConsoleTest.php(85)
#0 /tests/PHPUnit/System/ConsoleTest.php(72): Piwik\\\\Tests\\\\System\\\\TestCommandWithFatalError->executeImpl()
#1 /vendor/symfony/console/Symfony/Component/Console/Command/Command.php(257): Piwik\\\\Tests\\\\System\\\\TestCommandWithFatalError->execute()
',
))
END;

        if (PHP_MAJOR_VERSION < 8) {
            $expected = "#!/usr/bin/env php\n" . $expected;
        }

        $this->assertEquals($expected, $output);
    }

    public function test_Console_handlesExceptionsCorrectly()
    {
        $command = Fixture::getCliCommandBase();
        $command .= ' test-command-with-exception';
        $command .= ' 2>&1';

        $output = shell_exec($command);
        $output = $this->normalizeOutput($output);

        $expected = <<<END
*** IN SAFEMODE ***


               
  [Exception]  
  test error   
               


test-command-with-exception



END;

        if (PHP_MAJOR_VERSION < 8) {
            $expected = "#!/usr/bin/env php\n" . $expected;
        }

        $this->assertEquals($expected, $output);
    }

    public static function provideContainerConfigBeforeClass()
    {
        return [
            'log.handlers' => [\DI\get(FailureLogMessageDetector::class)],
            LoggerInterface::class => \DI\create(Logger::class)
                ->constructor('piwik', \DI\get('log.handlers'), \DI\get('log.processors')),

            'observers.global' => \DI\add([
                ['Console.filterCommands', \DI\value(function (&$commands) {
                    $commands[] = TestCommandWithFatalError::class;
                    $commands[] = TestCommandWithException::class;
                })],

                ['Request.dispatch', \DI\value(function ($module, $action) {
                    if ($module === 'CorePluginsAdmin' && $action === 'safemode') {
                        print "*** IN SAFEMODE ***\n"; // will appear in output
                    }
                })],
            ]),
        ];
    }

    private function normalizeOutput($output)
    {
        $output = str_replace(PIWIK_INCLUDE_PATH, '', $output);
        $output = preg_replace('/[0-9]+ bytes/', 'X bytes', $output);
        return $output;
    }
}