Skip to content

Commit

Permalink
Added Errors Data Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed Dec 3, 2018
1 parent 8a6aa4e commit 0b1ea32
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests-legacy/phpunit-admin.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<phpunit
bootstrap="bootstrap-admin.php"
processIsolation="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<env name="SYMFONY_ENV" value="test"/>
<env name="kernel.environment" value="test" />
<server name="KERNEL_CLASS" value="AppKernel" />
<const name="_PS_IN_TEST_" value="1" />
</php>
<listeners>
<listener class="Tests\TestCase\ErrorsDataListener" />
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
</listeners>
<groups>
<include>
<group>admin</group>
Expand Down
4 changes: 4 additions & 0 deletions tests-legacy/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<phpunit bootstrap="bootstrap.php"
color="true"
stopOnFailure="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<server name="KERNEL_CLASS" value="AppKernel" />
<env name="SYMFONY_ENV" value="test"/>
<env name="kernel.environment" value="test" />
</php>
<listeners>
<listener class="Tests\TestCase\ErrorsDataListener" />
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
</listeners>
<groups>
Expand Down
8 changes: 7 additions & 1 deletion tests-legacy/sf-tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
colors = "true"
processIsolation = "true"
bootstrap = "bootstrap-sf.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<server name="KERNEL_CLASS" value="AppKernel" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
<const name="_PS_IN_TEST_" value="1" />
</php>

<listeners>
<listener class="Tests\TestCase\ErrorsDataListener" />
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
</listeners>
<groups>
<exclude>
<group>controller</group>
Expand Down
76 changes: 76 additions & 0 deletions tests/TestCase/ErrorsDataListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace Tests\TestCase;

use PHPUnit\Framework\BaseTestListener;
use PHPUnit_Framework_TestSuite;

class ErrorsDataListener extends BaseTestListener
{
/**
* @var PhpErrorsCounter a dedicated error handler.
*/
private $errorsCounter;

/**
* @var bool error handler is registered
*/
private $isRegistered = false;

/**
* Internal tracking for test suites.
*
* Increments as more suites are run, then decremented as they finish. All
* suites have been run when returns to 0.
*/
protected $suites = 0;

public function __construct()
{
$this->errorsCounter = new PhpErrorsCounter();
}

public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
$this->suites++;
if (!$this->isRegistered) {
$this->errorsCounter->registerErrorHandler();
$this->isRegistered = true;
}
}

public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
$this->suites--;

if ($this->suites === 0) {
printf(PHP_EOL . PHP_EOL . 'Current report of phpErrorsHandler:');
printf(PHP_EOL . $this->errorsCounter->displaySummary() . PHP_EOL);
$this->errorsCounter->restoreErrorHandler();
}
}
}
125 changes: 125 additions & 0 deletions tests/TestCase/PhpErrorsCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace Tests\TestCase;

class PhpErrorsCounter
{
private $notices = 0;
private $warnings = 0;
private $errors = 0;
private $deprecations = 0;

/**
* This error handler allow us to count every errors
* in our test suite. Once we will have fixed all we will
* enable the error handler of PHPUnit that convert errors to exceptions.
*/
public function registerErrorHandler()
{
set_error_handler(function ($errorType) {
switch($errorType) {
case E_WARNING:
$this->warnings++;
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
$this->deprecations++;
break;
case E_ERROR:
$this->errors++;
break;
case E_NOTICE:
$this->notices++;
break;
default:
// nothing to do.
}
}, E_ALL);
}

public function restoreErrorHandler()
{
restore_error_handler();
}

/**
* @return int the number of notices
*/
public function getNotices()
{
return $this->notices;
}

/**
* @return int the number of warnings
*/
public function getWarnings()
{
return $this->warnings;
}

/**
* @return int the number of deprecations
*/
public function getDeprecations()
{
return $this->deprecations;
}

/**
* @return int the number of errors
*/
public function getErrors()
{
return $this->errors;
}

/**
* @return string a summary report of errors.
*/
public function displaySummary()
{
return sprintf(
'Errors: %d / Warnings: %d / Notices: %d / Deprecations: %d',
$this->getErrors(),
$this->getWarnings(),
$this->getNotices(),
$this->getDeprecations()
);
}

/**
* Reset all counters to 0.
*/
public function reset()
{
$this->deprecations = 0;
$this->errors = 0;
$this->notices = 0;
$this->warnings = 0;
}
}

0 comments on commit 0b1ea32

Please sign in to comment.