Skip to content

Commit

Permalink
MAGECLOUD-1193: Enable strict errors (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftedreality authored Oct 31, 2017
1 parent 29df844 commit 979a25c
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/ece-tools
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require_once __DIR__ . '/../autoload.php';
require_once __DIR__ . '/../bootstrap.php';

$application = \Magento\MagentoCloud\App\Bootstrap::create()->createApplication();
$application->run();
12 changes: 12 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
error_reporting(E_ALL);
date_default_timezone_set('UTC');

require_once __DIR__ . '/autoload.php';

$handler = new \Magento\MagentoCloud\App\ErrorHandler();
set_error_handler([$handler, 'handle']);
2 changes: 1 addition & 1 deletion m2-ece-build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require_once __DIR__ . '/autoload.php';
require_once __DIR__ . '/bootstrap.php';

$application = \Magento\MagentoCloud\App\Bootstrap::create()->createApplication();
$application->setDefaultCommand('build');
Expand Down
2 changes: 1 addition & 1 deletion m2-ece-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require_once __DIR__ . '/autoload.php';
require_once __DIR__ . '/bootstrap.php';

$application = \Magento\MagentoCloud\App\Bootstrap::create()->createApplication();
$application->setDefaultCommand('deploy');
Expand Down
2 changes: 1 addition & 1 deletion m2-ece-scd-dump
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require_once __DIR__ . '/autoload.php';
require_once __DIR__ . '/bootstrap.php';

$application = \Magento\MagentoCloud\App\Bootstrap::create()->createApplication();
$application->setDefaultCommand('dump');
Expand Down
66 changes: 66 additions & 0 deletions src/App/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\App;

/**
* An error handler that converts runtime errors into exceptions.
*/
class ErrorHandler
{
/**
* Error messages
*
* @var array
*/
private $errorPhrases = [
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict Notice',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated Functionality',
E_USER_DEPRECATED => 'User Deprecated Functionality',
];

/**
* Custom error handler.
*
* @param int $errorNo
* @param string $errorStr
* @param string $errorFile
* @param int $errorLine
* @return bool
* @throws \Exception
*/
public function handle($errorNo, $errorStr, $errorFile, $errorLine)
{
if (strpos($errorStr, 'DateTimeZone::__construct') !== false) {
/**
* There's no way to distinguish between caught system exceptions and warnings.
*/
return false;
}

$errorNo = $errorNo & error_reporting();

if ($errorNo == 0) {
return false;
}

$msg = isset($this->errorPhrases[$errorNo]) ? $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})";
$msg .= ": {$errorStr} in {$errorFile} on line {$errorLine}";

throw new \Exception($msg);
}
}
60 changes: 60 additions & 0 deletions src/Test/Unit/App/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Test\Unit\App;

use Magento\MagentoCloud\App\ErrorHandler;
use PHPUnit\Framework\TestCase;

/**
* @inheritdoc
*/
class ErrorHandlerTest extends TestCase
{
/**
* @var ErrorHandler
*/
private $handler;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->handler = new ErrorHandler();
}

public function testHandleDatetime()
{
$this->assertFalse(
$this->handler->handle(1, 'DateTimeZone::__construct', 'some_file.php', 1)
);
}

public function testHandleNoError()
{
$this->assertFalse(
$this->handler->handle(0, 'Some string', 'some_file.php', 1)
);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Error: Some string in some_file.php on line 1
*/
public function testHandleWithException()
{
$this->handler->handle(1, 'Some string', 'some_file.php', 1);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Unknown error (3): Some string in some_file.php on line 1
*/
public function testHandleWithUnknownException()
{
$this->handler->handle(3, 'Some string', 'some_file.php', 1);
}
}
2 changes: 1 addition & 1 deletion tests/integration/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
require __DIR__ . '/../../autoload.php';
require __DIR__ . '/../../bootstrap.php';

\Magento\MagentoCloud\Test\Integration\Bootstrap::create()->run();
2 changes: 1 addition & 1 deletion tests/unit/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
colors="true"
columns="max"
bootstrap="../../autoload.php"
bootstrap="../../bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="false"
>
<testsuites>
Expand Down

0 comments on commit 979a25c

Please sign in to comment.