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
path: root/tests
diff options
context:
space:
mode:
authordiosmosis <diosmosis@users.noreply.github.com>2020-01-31 05:05:16 +0300
committerGitHub <noreply@github.com>2020-01-31 05:05:16 +0300
commit82c8b932ecd006b3066085f30d23c67ffd3da916 (patch)
tree02f601464f3faf4b342398e612265d5bd2791db3 /tests
parent591cc9dfba9cb4e8587e19f61dd76ca30f8f6a90 (diff)
Use safemode when running CLI commands (#15472)
Diffstat (limited to 'tests')
-rw-r--r--tests/PHPUnit/Framework/Fixture.php15
-rw-r--r--tests/PHPUnit/System/ConsoleTest.php129
2 files changed, 144 insertions, 0 deletions
diff --git a/tests/PHPUnit/Framework/Fixture.php b/tests/PHPUnit/Framework/Fixture.php
index 3656785150..e1308751bb 100644
--- a/tests/PHPUnit/Framework/Fixture.php
+++ b/tests/PHPUnit/Framework/Fixture.php
@@ -15,6 +15,7 @@ use Piwik\Auth;
use Piwik\Auth\Password;
use Piwik\Cache\Backend\File;
use Piwik\Cache as PiwikCache;
+use Piwik\CliMulti\CliPhp;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
@@ -148,6 +149,20 @@ class Fixture extends \PHPUnit_Framework_Assert
return 'python';
}
+ public static function getCliCommandBase()
+ {
+ $cliPhp = new CliPhp();
+ $php = $cliPhp->findPhpBinary();
+
+ $command = $php . ' ' . PIWIK_INCLUDE_PATH .'/tests/PHPUnit/proxy/console ';
+
+ if (!empty($_SERVER['HTTP_HOST'])) {
+ $command .= '--matomo-domain=' . $_SERVER['HTTP_HOST'];
+ }
+
+ return $command;
+ }
+
public static function getTestRootUrl()
{
return self::getRootUrl() . 'tests/PHPUnit/proxy/';
diff --git a/tests/PHPUnit/System/ConsoleTest.php b/tests/PHPUnit/System/ConsoleTest.php
index 939cfee6ac..ba1aa796cd 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,87 @@ 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
+
+Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1073741825 bytes) in /tests/PHPUnit/System/ConsoleTest.php on line 85
+*** IN SAFEMODE ***
+Matomo encountered an error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1073741856 bytes) (which lead to: Error: array (
+ 'type' => 1,
+ 'message' => 'Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1073741825 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);
+ return $output;
+ }
} \ No newline at end of file