From e82fa73c0eec608811d016626aab2c007c43feaa Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 9 Oct 2020 23:44:52 +0100 Subject: [PATCH] Fixed tests --- phpunit.xml.dist | 2 +- tests/Whoops/Exception/FormatterTest.php | 4 +- .../Whoops/Exception/FrameCollectionTest.php | 11 +++-- tests/Whoops/Handler/CallbackHandlerTest.php | 9 ++-- tests/Whoops/Handler/PlainTextHandlerTest.php | 30 +++++++------- .../Whoops/Handler/PrettyPageHandlerTest.php | 6 +-- tests/Whoops/RunTest.php | 14 +++++-- tests/Whoops/TestCase.php | 41 +++++++++++++++++++ tests/Whoops/Util/SystemFacadeTest.php | 10 ++++- tests/Whoops/Util/TemplateHelperTest.php | 4 +- 10 files changed, 95 insertions(+), 36 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index df20f21..b809cb1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ - + tests/Whoops/ diff --git a/tests/Whoops/Exception/FormatterTest.php b/tests/Whoops/Exception/FormatterTest.php index 7fce399..2db61fc 100644 --- a/tests/Whoops/Exception/FormatterTest.php +++ b/tests/Whoops/Exception/FormatterTest.php @@ -14,7 +14,7 @@ class FormatterTest extends TestCase { $msg = 'Sample exception message foo'; $output = Formatter::formatExceptionPlain(new Inspector(new \Exception($msg))); - $this->assertContains($msg, $output); - $this->assertContains('Stacktrace', $output); + $this->assertStringContains($msg, $output); + $this->assertStringContains('Stacktrace', $output); } } diff --git a/tests/Whoops/Exception/FrameCollectionTest.php b/tests/Whoops/Exception/FrameCollectionTest.php index 0eacf63..5dc0813 100644 --- a/tests/Whoops/Exception/FrameCollectionTest.php +++ b/tests/Whoops/Exception/FrameCollectionTest.php @@ -80,21 +80,25 @@ class FrameCollectionTest extends TestCase /** * @covers Whoops\Exception\FrameCollection::offsetSet - * @expectedException Exception */ public function testArrayAccessSet() { $collection = $this->getFrameCollectionInstance(); + + $this->expectExceptionOfType('Exception'); + $collection[0] = 'foo'; } /** * @covers Whoops\Exception\FrameCollection::offsetUnset - * @expectedException Exception */ public function testArrayAccessUnset() { $collection = $this->getFrameCollectionInstance(); + + $this->expectExceptionOfType('Exception'); + unset($collection[0]); } @@ -132,12 +136,13 @@ class FrameCollectionTest extends TestCase /** * @covers Whoops\Exception\FrameCollection::map - * @expectedException UnexpectedValueException */ public function testMapFramesEnforceType() { $frames = $this->getFrameCollectionInstance(); + $this->expectExceptionOfType('UnexpectedValueException'); + // Filter out all frames with a line number under 6 $frames->map(function ($frame) { return "bajango"; diff --git a/tests/Whoops/Handler/CallbackHandlerTest.php b/tests/Whoops/Handler/CallbackHandlerTest.php index fd4a675..7c558ab 100644 --- a/tests/Whoops/Handler/CallbackHandlerTest.php +++ b/tests/Whoops/Handler/CallbackHandlerTest.php @@ -6,14 +6,15 @@ use Whoops\TestCase; class CallbackHandlerTest extends TestCase { - public function testSimplifiedBacktrace() { - $handler = new CallbackHandler(function($exception, $inspector, $run) { + public function testSimplifiedBacktrace() + { + $handler = new CallbackHandler(function ($exception, $inspector, $run) { return debug_backtrace(); }); $backtrace = $handler->handle(); - foreach($backtrace as $frame) { - $this->assertNotContains('call_user_func', $frame['function']); + foreach ($backtrace as $frame) { + $this->assertStringNotContains('call_user_func', $frame['function']); } } } diff --git a/tests/Whoops/Handler/PlainTextHandlerTest.php b/tests/Whoops/Handler/PlainTextHandlerTest.php index 3432c91..60142bb 100644 --- a/tests/Whoops/Handler/PlainTextHandlerTest.php +++ b/tests/Whoops/Handler/PlainTextHandlerTest.php @@ -75,22 +75,22 @@ class PlainTextHandlerTest extends TestCase /** * @covers Whoops\Handler\PlainTextHandler::__construct * @covers Whoops\Handler\PlainTextHandler::setLogger - * @expectedException \InvalidArgumentException */ public function testConstructor() { - $logger = new StdClass(); // guaranteed to be invalid! - $this->getHandler($logger); + $this->expectExceptionOfType('InvalidArgumentException'); + + $this->getHandler(new StdClass()); } /** * @covers Whoops\Handler\PlainTextHandler::setLogger - * @expectedException InvalidArgumentException */ public function testSetLogger() { - $logger = new StdClass(); // guaranteed to be invalid! - $this->getHandler()->setLogger($logger); + $this->expectExceptionOfType('InvalidArgumentException'); + + $this->getHandler()->setLogger(new StdClass()); } /** @@ -239,7 +239,7 @@ class PlainTextHandlerTest extends TestCase ); // Check that the response does not contain Inner exception message: - $this->assertNotContains( + $this->assertStringNotContains( sprintf( "%s: %s in file %s", RuntimeException::class, @@ -295,10 +295,10 @@ class PlainTextHandlerTest extends TestCase ); // Check that the response has the correct value: - $this->assertContains('Stack trace:', $text); + $this->assertStringContains('Stack trace:', $text); // Check that the trace is returned: - $this->assertContains( + $this->assertStringContains( sprintf( '%3d. %s->%s() %s:%d', 2, @@ -334,10 +334,10 @@ class PlainTextHandlerTest extends TestCase $this->assertGreaterThan(60, count($lines)); // Check that the response has the correct value: - $this->assertContains('Stack trace:', $text); + $this->assertStringContains('Stack trace:', $text); // Check that the trace is returned: - $this->assertContains( + $this->assertStringContains( sprintf( '%3d. %s->%s() %s:%d', 2, @@ -349,7 +349,7 @@ class PlainTextHandlerTest extends TestCase $text ); // Check that the trace arguments are returned: - $this->assertContains(sprintf( + $this->assertStringContains(sprintf( '%s string(%d) "%s"', PlainTextHandler::VAR_DUMP_PREFIX, strlen('test message'), @@ -376,10 +376,10 @@ class PlainTextHandlerTest extends TestCase ); // Check that the response has the correct value: - $this->assertContains('Stack trace:', $text); + $this->assertStringContains('Stack trace:', $text); // Check that the trace is returned: - $this->assertContains( + $this->assertStringContains( sprintf( '%3d. %s->%s() %s:%d', 2, @@ -392,7 +392,7 @@ class PlainTextHandlerTest extends TestCase ); // Check that the trace arguments are returned: - $this->assertContains(sprintf( + $this->assertStringContains(sprintf( '%s string(%d) "%s"', PlainTextHandler::VAR_DUMP_PREFIX, strlen('test message'), diff --git a/tests/Whoops/Handler/PrettyPageHandlerTest.php b/tests/Whoops/Handler/PrettyPageHandlerTest.php index 02f885c..73532c1 100644 --- a/tests/Whoops/Handler/PrettyPageHandlerTest.php +++ b/tests/Whoops/Handler/PrettyPageHandlerTest.php @@ -81,12 +81,12 @@ class PrettyPageHandlerTest extends TestCase /** * @covers Whoops\Handler\PrettyPageHandler::addResourcePath - * @expectedException InvalidArgumentException */ public function testSetInvalidResourcesPath() { - $path = __DIR__ . '/ZIMBABWE'; // guaranteed to be invalid! - $this->getHandler()->addResourcePath($path); + $this->expectExceptionOfType('InvalidArgumentException'); + + $this->getHandler()->addResourcePath(__DIR__ . '/ZIMBABWE'); } /** diff --git a/tests/Whoops/RunTest.php b/tests/Whoops/RunTest.php index 7b9c40e..77e0d79 100755 --- a/tests/Whoops/RunTest.php +++ b/tests/Whoops/RunTest.php @@ -93,13 +93,15 @@ class RunTest extends TestCase } /** - * @expectedException InvalidArgumentException * @covers Whoops\Run::pushHandler */ public function testPushInvalidHandler() { $run = $this->getRunInstance(); - $run->pushHandler($banana = 'actually turnip'); + + $this->expectExceptionOfType('InvalidArgumentException'); + + $run->pushHandler('actually turnip'); } /** @@ -180,7 +182,6 @@ class RunTest extends TestCase /** * @covers Whoops\Run::unregister - * @expectedException Exception */ public function testUnregisterHandler() { @@ -191,6 +192,9 @@ class RunTest extends TestCase $run->pushHandler($handler); $run->unregister(); + + $this->expectExceptionOfType('Exception'); + throw $this->getException("I'm not supposed to be caught!"); } @@ -402,6 +406,7 @@ class RunTest extends TestCase /** * @covers Whoops\Run::handleError + * @requires PHP < 8 */ public function testGetSilencedError() { @@ -491,10 +496,11 @@ class RunTest extends TestCase /** * @covers Whoops\Run::sendHttpCode - * @expectedException InvalidArgumentException */ public function testSendHttpCodeWrongCode() { + $this->expectExceptionOfType('InvalidArgumentException'); + $this->getRunInstance()->sendHttpCode(1337); } } diff --git a/tests/Whoops/TestCase.php b/tests/Whoops/TestCase.php index 352e64a..1ec2dcf 100644 --- a/tests/Whoops/TestCase.php +++ b/tests/Whoops/TestCase.php @@ -21,6 +21,47 @@ class TestCase extends BaseTestCase return $run; } + /** + * @param string $class + * @return void + */ + protected function expectExceptionOfType($class) + { + if (method_exists($this, 'expectException')) { + $this->expectException($class); + } else { + $this->setExpectedException($class); + } + } + + /** + * @param string $a + * @param string $b + * @return void + */ + protected function assertStringContains($a, $b) + { + if (method_exists($this, 'assertStringContainsString')) { + $this->assertStringContainsString($a, $b); + } else { + $this->assertContains($a, $b); + } + } + + /** + * @param string $a + * @param string $b + * @return void + */ + protected function assertStringNotContains($a, $b) + { + if (method_exists($this, 'assertStringNotContainsString')) { + $this->assertStringNotContainsString($a, $b); + } else { + $this->assertNotContains($a, $b); + } + } + /** * @param object|string $class_or_object * @param string $method diff --git a/tests/Whoops/Util/SystemFacadeTest.php b/tests/Whoops/Util/SystemFacadeTest.php index a9536aa..25b53ef 100644 --- a/tests/Whoops/Util/SystemFacadeTest.php +++ b/tests/Whoops/Util/SystemFacadeTest.php @@ -22,13 +22,19 @@ class SystemFacadeTest extends TestCase : call_user_func_array("\\$fn", $args); } - protected function setUp() + /** + * @before + */ + public function getReady() { self::$runtime = \Mockery::mock(['ob_start' => true]); $this->facade = new SystemFacade(); } - protected function tearDown() + /** + * @after + */ + public function finishUp() { self::$runtime = null; \Mockery::close(); diff --git a/tests/Whoops/Util/TemplateHelperTest.php b/tests/Whoops/Util/TemplateHelperTest.php index 0d734f9..086645a 100644 --- a/tests/Whoops/Util/TemplateHelperTest.php +++ b/tests/Whoops/Util/TemplateHelperTest.php @@ -16,9 +16,9 @@ class TemplateHelperTest extends TestCase private $helper; /** - * {@inheritDoc} + * @before */ - public function setUp() + public function getReady() { $this->helper = new TemplateHelper(); }