-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from xp-framework/feature/warnings-fail-tests
Make warnings raised during test execution fail these tests
- Loading branch information
Showing
4 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php namespace test; | ||
|
||
use lang\{Throwable, StackTraceElement}; | ||
|
||
class Warnings extends Throwable { | ||
private static $LEVELS= [ | ||
E_ERROR => 'E_ERROR', | ||
E_WARNING => 'E_WARNING', | ||
E_PARSE => 'E_PARSE', | ||
E_NOTICE => 'E_NOTICE', | ||
E_CORE_ERROR => 'E_CORE_ERROR', | ||
E_CORE_WARNING => 'E_CORE_WARNING', | ||
E_COMPILE_ERROR => 'E_COMPILE_ERROR', | ||
E_COMPILE_WARNING => 'E_COMPILE_WARNING', | ||
E_USER_ERROR => 'E_USER_ERROR', | ||
E_USER_WARNING => 'E_USER_WARNING', | ||
E_USER_NOTICE => 'E_USER_NOTICE', | ||
E_STRICT => 'E_STRICT', | ||
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', | ||
E_DEPRECATED => 'E_DEPRECATED', | ||
E_USER_DEPRECATED => 'E_USER_DEPRECATED', | ||
]; | ||
|
||
/** Creates a new warnings instance */ | ||
public function __construct(array $warnings) { | ||
$message= ''; | ||
foreach ($warnings as $warning) { | ||
$message.= ', '.$warning[1]; | ||
$this->trace[]= new StackTraceElement( | ||
$warning[2], | ||
null, | ||
'@error', | ||
$warning[3], | ||
[], | ||
(self::$LEVELS[$warning[0]] ?? 'E_UNKNOWN('.$warning[0].')').': '.$warning[1] | ||
); | ||
} | ||
parent::__construct(substr($message, 2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php namespace test\unittest; | ||
|
||
use test\assert\Matches; | ||
use test\execution\TestCase; | ||
use test\outcome\{Succeeded, Failed}; | ||
use test\{Assert, Test}; | ||
|
||
class WarningsTest { | ||
|
||
/** Executes test function */ | ||
private function execute($function) { | ||
return (new TestCase('test', $function))->run(); | ||
} | ||
|
||
#[Test] | ||
public function without_warnings() { | ||
Assert::instance(Succeeded::class, $this->execute(function() { })); | ||
} | ||
|
||
#[Test] | ||
public function trigger_error() { | ||
$r= $this->execute(function() { trigger_error('Test'); }); | ||
Assert::equals('E_USER_NOTICE: Test', $r->cause->getStackTrace()[0]->message); | ||
} | ||
|
||
#[Test] | ||
public function trigger_deprecation_error() { | ||
$r= $this->execute(function() { trigger_error('Test', E_USER_DEPRECATED); }); | ||
Assert::equals('E_USER_DEPRECATED: Test', $r->cause->getStackTrace()[0]->message); | ||
} | ||
|
||
#[Test] | ||
public function fopen_nonexistant_file() { | ||
$r= $this->execute(function() { fopen('$', 'r'); }); | ||
|
||
Assert::matches( | ||
'/E_WARNING: fopen.+: No such file or directory/i', | ||
$r->cause->getStackTrace()[0]->message | ||
); | ||
} | ||
|
||
#[Test] | ||
public function multiple_warnings() { | ||
$r= $this->execute(function() { trigger_error('One'); trigger_error('Two'); }); | ||
Assert::equals('E_USER_NOTICE: One', $r->cause->getStackTrace()[0]->message); | ||
Assert::equals('E_USER_NOTICE: Two', $r->cause->getStackTrace()[1]->message); | ||
} | ||
} |