From 86f49e2d6c5205a21be0bb9a0e00a80b3906f1d0 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 26 Apr 2022 19:41:58 +0200 Subject: [PATCH] Assert::exception() prints stack of unexpected exception --- src/Framework/Assert.php | 4 ++-- tests/Framework/Assert.exception.phpt | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index e855cb70..f3cf7eda 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -346,10 +346,10 @@ public static function exception( self::fail("$class was expected but got " . get_class($e) . ($e->getMessage() ? " ({$e->getMessage()})" : ''), null, null, $e); } elseif ($message && !self::isMatching($message, $e->getMessage())) { - self::fail("$class with a message matching %2 was expected but got %1", $e->getMessage(), $message); + self::fail("$class with a message matching %2 was expected but got %1", $e->getMessage(), $message, $e); } elseif ($code !== null && $e->getCode() !== $code) { - self::fail("$class with a code %2 was expected but got %1", $e->getCode(), $code); + self::fail("$class with a code %2 was expected but got %1", $e->getCode(), $code, $e); } return $e; diff --git a/tests/Framework/Assert.exception.phpt b/tests/Framework/Assert.exception.phpt index 22754b2c..35a82ed9 100644 --- a/tests/Framework/Assert.exception.phpt +++ b/tests/Framework/Assert.exception.phpt @@ -33,23 +33,23 @@ $e = Assert::exception(function () use (&$inner) { }, Tester\AssertException::class, 'UnknownException was expected but got Exception (message)'); Assert::same($inner, $e->getPrevious()); -$e = Assert::exception(function () { - Assert::exception(function () { - throw new Exception('Text'); +$e = Assert::exception(function () use (&$inner) { + Assert::exception(function () use (&$inner) { + throw $inner = new Exception('Text'); }, Exception::class, 'Abc'); }, Tester\AssertException::class, "Exception with a message matching 'Abc' was expected but got 'Text'"); -Assert::null($e->getPrevious()); +Assert::same($inner, $e->getPrevious()); Assert::exception(function () { throw new Exception('Text', 42); }, Exception::class, null, 42); -$e = Assert::exception(function () { - Assert::exception(function () { - throw new Exception('Text', 1); +$e = Assert::exception(function () use (&$inner) { + Assert::exception(function () use (&$inner) { + throw $inner = new Exception('Text', 1); }, Exception::class, null, 42); }, Tester\AssertException::class, 'Exception with a code 42 was expected but got 1'); -Assert::null($e->getPrevious()); +Assert::same($inner, $e->getPrevious()); $old = Assert::$onFailure; Assert::$onFailure = function () {};