Skip to content

Commit

Permalink
added warning when no assertions are executed [Closes #112]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 31, 2014
1 parent 07e5cac commit 100fa89
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 9 deletions.
20 changes: 20 additions & 0 deletions Tester/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ class Assert
/** @var callable function(AssertException $exception) */
public static $onFailure;

/** @var int the count of assertions */
public static $counter = 0;


/**
* Checks assertion. Values must be exactly the same.
* @return void
*/
public static function same($expected, $actual)
{
self::$counter++;
if ($actual !== $expected) {
self::fail('%1 should be %2', $actual, $expected);
}
Expand All @@ -60,6 +64,7 @@ public static function same($expected, $actual)
*/
public static function notSame($expected, $actual)
{
self::$counter++;
if ($actual === $expected) {
self::fail('%1 should not be %2', $actual, $expected);
}
Expand All @@ -72,6 +77,7 @@ public static function notSame($expected, $actual)
*/
public static function equal($expected, $actual)
{
self::$counter++;
if (!self::isEqual($expected, $actual)) {
self::fail('%1 should be equal to %2', $actual, $expected);
}
Expand All @@ -84,6 +90,7 @@ public static function equal($expected, $actual)
*/
public static function notEqual($expected, $actual)
{
self::$counter++;
if (self::isEqual($expected, $actual)) {
self::fail('%1 should not be equal to %2', $actual, $expected);
}
Expand All @@ -96,6 +103,7 @@ public static function notEqual($expected, $actual)
*/
public static function contains($needle, $actual)
{
self::$counter++;
if (is_array($actual)) {
if (!in_array($needle, $actual, TRUE)) {
self::fail('%1 should contain %2', $actual, $needle);
Expand All @@ -116,6 +124,7 @@ public static function contains($needle, $actual)
*/
public static function notContains($needle, $actual)
{
self::$counter++;
if (is_array($actual)) {
if (in_array($needle, $actual, TRUE)) {
self::fail('%1 should not contain %2', $actual, $needle);
Expand All @@ -137,6 +146,7 @@ public static function notContains($needle, $actual)
*/
public static function true($actual)
{
self::$counter++;
if ($actual !== TRUE) {
self::fail('%1 should be TRUE', $actual);
}
Expand All @@ -150,6 +160,7 @@ public static function true($actual)
*/
public static function false($actual)
{
self::$counter++;
if ($actual !== FALSE) {
self::fail('%1 should be FALSE', $actual);
}
Expand All @@ -163,6 +174,7 @@ public static function false($actual)
*/
public static function null($actual)
{
self::$counter++;
if ($actual !== NULL) {
self::fail('%1 should be NULL', $actual);
}
Expand All @@ -176,6 +188,7 @@ public static function null($actual)
*/
public static function nan($actual)
{
self::$counter++;
if (!is_float($actual) || !is_nan($actual)) {
self::fail('%1 should be NAN', $actual);
}
Expand All @@ -189,6 +202,7 @@ public static function nan($actual)
*/
public static function truthy($actual)
{
self::$counter++;
if (!$actual) {
self::fail('%1 should be truthy', $actual);
}
Expand All @@ -202,6 +216,7 @@ public static function truthy($actual)
*/
public static function falsey($actual)
{
self::$counter++;
if ($actual) {
self::fail('%1 should be falsey', $actual);
}
Expand All @@ -214,6 +229,7 @@ public static function falsey($actual)
*/
public static function type($type, $value)
{
self::$counter++;
if (!is_object($type) && !is_string($type)) {
throw new \Exception('Type must be a object or a string.');

Expand Down Expand Up @@ -245,6 +261,7 @@ public static function type($type, $value)
*/
public static function exception($function, $class, $message = NULL, $code = NULL)
{
self::$counter++;
try {
call_user_func($function);
} catch (\Exception $e) {
Expand Down Expand Up @@ -288,6 +305,7 @@ public static function error($function, $expectedType, $expectedMessage = NULL)
return static::exception($function, $expectedType, $expectedMessage);
}

self::$counter++;
$expected = is_array($expectedType) ? $expectedType : array(array($expectedType, $expectedMessage));
foreach ($expected as & $item) {
list($expectedType, $expectedMessage) = $item;
Expand Down Expand Up @@ -353,6 +371,7 @@ public static function error($function, $expectedType, $expectedMessage = NULL)
*/
public static function match($pattern, $actual)
{
self::$counter++;
if (!is_string($pattern)) {
throw new \Exception('Pattern must be a string.');

Expand All @@ -368,6 +387,7 @@ public static function match($pattern, $actual)
*/
public static function matchFile($file, $actual)
{
self::$counter++;
$pattern = @file_get_contents($file);
if ($pattern === FALSE) {
throw new \Exception("Unable to read file '$file'.");
Expand Down
22 changes: 16 additions & 6 deletions Tester/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ class Environment
/** Code coverage file */
const COVERAGE = 'NETTE_TESTER_COVERAGE';


/** @var bool used for debugging Tester itself */
public static $debugMode = TRUE;

/** @var bool */
public static $checkAssertions = TRUE;

/** @var bool */
public static $useColors;

Expand All @@ -53,6 +55,7 @@ class_exists('Tester\Assert');
ini_set('log_errors', FALSE);

set_exception_handler(array(__CLASS__, 'handleException'));

set_error_handler(function($severity, $message, $file, $line) {
if (in_array($severity, array(E_RECOVERABLE_ERROR, E_USER_ERROR)) || ($severity & error_reporting()) === $severity) {
Environment::handleException(new \ErrorException($message, 0, $severity, $file, $line));
Expand All @@ -64,11 +67,16 @@ class_exists('Tester\Assert');
Assert::$onFailure = array(__CLASS__, 'handleException'); // note that Runner is unable to catch this errors in CLI & PHP 5.4.0 - 5.4.6 due PHP bug #62725

$error = error_get_last();
if (in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE)) && ($error['type'] & error_reporting()) !== $error['type']) {
register_shutdown_function(function() use ($error) {
echo "\nFatal error: $error[message] in $error[file] on line $error[line]\n";
});
}
register_shutdown_function(function() use ($error) {
if (in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
if (($error['type'] & error_reporting()) !== $error['type']) { // show fatal errors hidden by @shutup
echo "\nFatal error: $error[message] in $error[file] on line $error[line]\n";
}
} elseif (Environment::$checkAssertions && !Assert::$counter) {
echo "\nError: This test forgets to execute an assertion.\n";
exit(Runner\Job::CODE_FAIL);
}
});
});

if (getenv(self::COVERAGE)) {
Expand All @@ -84,6 +92,7 @@ class_exists('Tester\Assert');
/** @internal */
public static function handleException($e)
{
self::$checkAssertions = FALSE;
echo self::$debugMode ? Dumper::dumpException($e) : "\nError: {$e->getMessage()}\n";
exit($e instanceof AssertException ? Runner\Job::CODE_FAIL : Runner\Job::CODE_ERROR);
}
Expand All @@ -95,6 +104,7 @@ public static function handleException($e)
*/
public static function skip($message = '')
{
self::$checkAssertions = FALSE;
echo "\nSkipped:\n$message\n";
die(Runner\Job::CODE_SKIP);
}
Expand Down
1 change: 1 addition & 0 deletions Tester/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function run($method = NULL)

if (($method === NULL || $method === self::LIST_METHODS) && isset($_SERVER['argv'][1])) {
if ($_SERVER['argv'][1] === self::LIST_METHODS) {
Environment::$checkAssertions = FALSE;
header('Content-Type: application/json');
echo json_encode($methods);
return;
Expand Down
1 change: 1 addition & 0 deletions Tester/tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


Tester\Environment::setup();
Tester\Environment::$checkAssertions = FALSE;

ob_start();
echo <<<XX
Expand Down
4 changes: 2 additions & 2 deletions tests/Runner.multiple-fails.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Assert::same( Runner::SKIPPED, $logger->results['testcase-no-methods.phptx'][0]


Assert::match(
"Cannot list TestCase methods in file '%a%testcase-not-call-run.phptx'. Do you call TestCase::run() in it?",
$logger->results['testcase-not-call-run.phptx'][1]
'Error: This test forgets to execute an assertion.',
trim($logger->results['testcase-not-call-run.phptx'][1])
);
Assert::same( Runner::FAILED, $logger->results['testcase-not-call-run.phptx'][0] );

Expand Down
1 change: 1 addition & 0 deletions tests/annotations/exitCode.die.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


die(231);
2 changes: 1 addition & 1 deletion tests/annotations/exitCode.error1.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*/

require __DIR__ . '/../bootstrap.php';

Tester\Environment::$checkAssertions = FALSE;

die(231);
1 change: 1 addition & 0 deletions tests/annotations/exitCode.error2.phptx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


die(231);
1 change: 1 addition & 0 deletions tests/annotations/exitCode.notice.pure.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


restore_error_handler();
Expand Down
1 change: 1 addition & 0 deletions tests/annotations/exitCode.notice.shutup.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


@$a++;
1 change: 1 addition & 0 deletions tests/annotations/httpCode.200.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;
1 change: 1 addition & 0 deletions tests/annotations/httpCode.500.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


header('HTTP', TRUE, 500);
1 change: 1 addition & 0 deletions tests/annotations/httpCode.error1.phptx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;
1 change: 1 addition & 0 deletions tests/annotations/httpCode.error2.phptx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require __DIR__ . '/../bootstrap.php';
Tester\Environment::$checkAssertions = FALSE;


header('HTTP', TRUE, 500);

0 comments on commit 100fa89

Please sign in to comment.