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

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/PHPUnit/System/ConsoleTest.php')
-rw-r--r--tests/PHPUnit/System/ConsoleTest.php132
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/PHPUnit/System/ConsoleTest.php b/tests/PHPUnit/System/ConsoleTest.php
index 939cfee6ac..9221fa8530 100644
--- a/tests/PHPUnit/System/ConsoleTest.php
+++ b/tests/PHPUnit/System/ConsoleTest.php
@@ -8,9 +8,13 @@
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;
@@ -51,6 +55,56 @@ class TestCommandWithError extends ConsoleCommand
}
}
+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');
+ }
+}
+
class ConsoleTest extends ConsoleCommandTestCase
{
public function setUp()
@@ -89,12 +143,90 @@ class ConsoleTest extends ConsoleCommandTestCase
$this->assertEquals(0, $exitCode);
}
+ public function test_Console_handlesFatalErrorsCorrectly()
+ {
+ $command = Fixture::getCliCommandBase();
+ $command .= ' test-command-with-fatal-error';
+ $command .= ' 2>&1';
+
+ $output = shell_exec($command);
+ $output = $this->normalizeOutput($output);
+
+ $expected = <<<END
+#!/usr/bin/env php
+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;
+
+ $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
+#!/usr/bin/env php
+*** IN SAFEMODE ***
+
+
+
+ [Exception]
+ test error
+
+
+
+test-command-with-exception
+
+
+
+END;
+ $this->assertEquals($expected, $output);
+ }
+
public static function provideContainerConfigBeforeClass()
{
return [
'log.handlers' => [\DI\get(FailureLogMessageDetector::class)],
LoggerInterface::class => \DI\object(Logger::class)
->constructor('piwik', \DI\get('log.handlers'), \DI\get('log.processors')),
+
+ 'observers.global' => \DI\add([
+ ['Console.filterCommands', function (&$commands) {
+ $commands[] = TestCommandWithFatalError::class;
+ $commands[] = TestCommandWithException::class;
+ }],
+
+ ['Request.dispatch', 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;
+ }
} \ No newline at end of file