Skip to content
Snippets Groups Projects
Commit b345337b authored by Ghislain Loas's avatar Ghislain Loas
Browse files

Merge branch 'speed_trap_on_test' into 'master'

Speed trap on test

See merge request !312
parents 9e6d9b62 be531fc9
Branches
Tags
1 merge request!318Master
<?php
/**
* Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
*
* AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* AFI-OPAC 2.0 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with AFI-OPAC 2.0; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @package Tests
*/
class TestSpeedTrap extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener {
protected static $_speed_trapped_test = [],
$_speed_limit = 0.1;
protected $_trapped_test = [];
public static function printSpeedTrappedTests() {
if(!$_trapped = self::sortTrapped())
return;
$_high_score_in_seconds = self::getHighScore($_trapped);
$_high_score_speed_test = self::getHighSpeedTest($_trapped);
self::renderScore($_high_score_in_seconds, $_high_score_speed_test);
}
/**
* called when test is ended - determines if it was long and prints
* @param PHUnit_Framework_Test $test
* @param float $length the length of time for the test
*/
public function endTest(PHPUnit_Framework_Test $test, $length)
{
if ($length > self::$_speed_limit)
$this->collect(['name' => $test->getName(), 'speed' => $length]);
}
/**
* Used to print error in error colors
* @param string $error
*/
protected static function printError($error, $speed)
{
print <<<END
[ $error ] ⤞ $speed seconds
END;
}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::startTest()
*/
public function startTest(PHPUnit_Framework_Test $test) {}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addError()
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addFailure()
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addError()
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time){}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::addSkippedTest()
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::startTestSuite()
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
/**
* Required for Interface
* (non-PHPdoc)
* @see PHPUnit_Framework_TestListener::endTestSuite()
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {}
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
protected function collect($trapped) {
self::$_speed_trapped_test[floor($trapped['speed'])][$trapped['name']] = round($trapped['speed'], 2);
}
protected static function sortTrapped() {
if(!count(self::$_speed_trapped_test))
return false;
krsort(self::$_speed_trapped_test);
reset(self::$_speed_trapped_test);
return self::$_speed_trapped_test;
}
protected static function getHighScore($trapped_tests) {
return key($trapped_tests);
}
protected static function getHighSpeedTest($trapped_tests) {
$high_score_tests = current($trapped_tests);
arsort($high_score_tests);
return $high_score_tests;
}
protected static function renderScore($high_score_in_second, $high_score_tests) {
$emot = count($high_score_tests) > 1 ? '😣' : '😄';
$header = $emot.' SpeedTrap score: ' . $high_score_in_second . ' seconds.';
print <<<END
$header
END;
foreach($high_score_tests as $test_name => $speed )
self::printError($test_name, $speed);
}
}
?>
\ No newline at end of file
......@@ -86,4 +86,8 @@ Zend_Controller_Front::getInstance()->getRouter()->route(new Zend_Controller_Req
require_once "tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php";
require_once 'tests/application/modules/admin/controllers/AdminAbstractControllerTestCase.php';
register_shutdown_function(function(){
TestSpeedTrap::printSpeedTrappedTests();
});
?>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./bootstrap.php"
colors="false"
backupGlobals="false"
stopOnFailure="false"
stopOnError="false"
>
<testsuites>
<testsuite name="ApplicationTestSuite">
<directory>./application/</directory>
<directory>./library/</directory>
<directory>./js/</directory>
<directory>../library/storm/tests/Storm/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../application</directory>
<directory suffix=".php">../library/Class</directory>
<directory suffix=".php">../library/Trait</directory>
<directory suffix=".php">../library/ZendAfi</directory>
<directory suffix=".php">../library/fonctions</directory>
<directory suffix=".php">./js/</directory>
<exclude>
<file>../index.php</file>
<directory>../library/Class/Pdf</directory>
<directory>../library/Thumbs</directory>
<directory>../library/storm/tests/Storm</directory>
</exclude>
</whitelist>
</filter>
bootstrap="./bootstrap.php"
colors="false"
backupGlobals="false"
stopOnFailure="false"
stopOnError="false"
>
<testsuites>
<testsuite name="ApplicationTestSuite">
<directory>./application/</directory>
<directory>./library/</directory>
<directory>./js/</directory>
<directory>../library/storm/tests/Storm/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../application</directory>
<directory suffix=".php">../library/Class</directory>
<directory suffix=".php">../library/Trait</directory>
<directory suffix=".php">../library/ZendAfi</directory>
<directory suffix=".php">../library/fonctions</directory>
<directory suffix=".php">./js/</directory>
<exclude>
<file>../index.php</file>
<directory>../library/Class/Pdf</directory>
<directory>../library/Thumbs</directory>
<directory>../library/storm/tests/Storm</directory>
</exclude>
</whitelist>
</filter>
<listeners>
<listener file = "./TestSpeedTrap.php" class="TestSpeedTrap"/>
</listeners>
</phpunit>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment