From f2837342d825a8257b7e5b92ef29fa6b8b6f1703 Mon Sep 17 00:00:00 2001 From: Sergey Syomkin Date: Wed, 20 May 2026 16:24:04 +0300 Subject: [PATCH 1/5] Assert not null --- plugin/assert/Assert.php | 26 +++++++++++++++ plugin/assert/tests/Self/AssertNotNull.php | 37 ++++++++++++++++++++++ tests/Testo/Self/AssertTest.php | 1 + 3 files changed, 64 insertions(+) create mode 100644 plugin/assert/tests/Self/AssertNotNull.php diff --git a/plugin/assert/Assert.php b/plugin/assert/Assert.php index e179d16..74e3542 100644 --- a/plugin/assert/Assert.php +++ b/plugin/assert/Assert.php @@ -252,6 +252,32 @@ public static function null( )); } + /** + * Asserts that the given value is not null. + * + * @param mixed $actual The actual value to check for non-null. + * @param string $message Short description about what exactly is being asserted. + * @throws AssertionException when the assertion fails. + * + * @psalm-assert !null $actual + * @phpstan-assert !null $actual + */ + #[AssertMethod] + public static function notNull( + mixed $actual, + string $message = '', + ): void { + $actual !== null + ? StaticState::success($actual, 'is not `null`', $message) + : StaticState::fail(new AssertionException( + value: 'null', + assertion: 'is not `null`', + context: $message, + reason: 'expected a non-null value, got `null`', + details: '', + )); + } + /** * Checks if a value is blank. * diff --git a/plugin/assert/tests/Self/AssertNotNull.php b/plugin/assert/tests/Self/AssertNotNull.php new file mode 100644 index 0000000..b21e1ea --- /dev/null +++ b/plugin/assert/tests/Self/AssertNotNull.php @@ -0,0 +1,37 @@ + Date: Wed, 20 May 2026 17:59:46 +0300 Subject: [PATCH 2/5] Assert not null --- .../tests/Feature/AssertNotNullTest.php | 40 +++++++++++++++++++ .../tests/Stub/AssertNotNullNegative.php | 26 ++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 plugin/assert/tests/Feature/AssertNotNullTest.php create mode 100644 plugin/assert/tests/Stub/AssertNotNullNegative.php diff --git a/plugin/assert/tests/Feature/AssertNotNullTest.php b/plugin/assert/tests/Feature/AssertNotNullTest.php new file mode 100644 index 0000000..d8628c1 --- /dev/null +++ b/plugin/assert/tests/Feature/AssertNotNullTest.php @@ -0,0 +1,40 @@ +status, Status::Failed); + Assert::instanceOf($result->failure, Assertion::class); + Assert::string($result->failure->getFailReason()) + ->contains('expected a non-null value, got `null`'); + } + + #[Test] + public function nullFailsWithMessage(): void + { + $result = TestRunner::runTest([AssertNotNullNegative::class, 'nullFailsWithMessage']); + + Assert::same($result->status, Status::Failed); + Assert::instanceOf($result->failure, Assertion::class); + Assert::string($result->failure->getFailReason()) + ->contains('expected a non-null value, got `null`'); + Assert::same($result->failure->getContext(), 'Value must not be null.'); + } +} \ No newline at end of file diff --git a/plugin/assert/tests/Stub/AssertNotNullNegative.php b/plugin/assert/tests/Stub/AssertNotNullNegative.php new file mode 100644 index 0000000..6083f38 --- /dev/null +++ b/plugin/assert/tests/Stub/AssertNotNullNegative.php @@ -0,0 +1,26 @@ + Date: Wed, 20 May 2026 18:26:36 +0300 Subject: [PATCH 3/5] Assert not null --- .../tests/Feature/AssertNotNullTest.php | 9 +++++++ plugin/assert/tests/Self/AssertNotNull.php | 18 ++++++++++++++ .../tests/Stub/AssertNotNullPositive.php | 24 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 plugin/assert/tests/Stub/AssertNotNullPositive.php diff --git a/plugin/assert/tests/Feature/AssertNotNullTest.php b/plugin/assert/tests/Feature/AssertNotNullTest.php index d8628c1..532ed52 100644 --- a/plugin/assert/tests/Feature/AssertNotNullTest.php +++ b/plugin/assert/tests/Feature/AssertNotNullTest.php @@ -11,10 +11,19 @@ use Testo\Testing\Attribute\TestingSuite; use Testo\Testing\Traits\TestRunner; use Tests\Assert\Stub\AssertNotNullNegative; +use Tests\Assert\Stub\AssertNotNullPositive; #[TestingSuite(path: __DIR__ . '/../Stub')] final class AssertNotNullTest { + #[Test] + public function nonNullValuePasses(): void + { + $result = TestRunner::runTest([AssertNotNullPositive::class, 'falsyNonNullValues']); + + Assert::same($result->status, Status::Passed); + } + #[Test] public function nullFails(): void { diff --git a/plugin/assert/tests/Self/AssertNotNull.php b/plugin/assert/tests/Self/AssertNotNull.php index b21e1ea..14986c1 100644 --- a/plugin/assert/tests/Self/AssertNotNull.php +++ b/plugin/assert/tests/Self/AssertNotNull.php @@ -34,4 +34,22 @@ public function checkNullFailsWithMessage(): void Expect::exception(AssertionException::class); Assert::notNull(null, 'Value must not be null.'); } + + #[Test] + public function checkExceptionContext(): void + { + try { + Assert::notNull(null, 'my context'); + } catch (AssertionException $e) { + Assert::same($e->getContext(), 'my context'); + return; + } + Assert::fail('Expected AssertionException to be thrown'); + } + + #[Test] + public function checkObjectIsNotNull(): void + { + Assert::notNull(new \stdClass()); + } } diff --git a/plugin/assert/tests/Stub/AssertNotNullPositive.php b/plugin/assert/tests/Stub/AssertNotNullPositive.php new file mode 100644 index 0000000..937e4eb --- /dev/null +++ b/plugin/assert/tests/Stub/AssertNotNullPositive.php @@ -0,0 +1,24 @@ + Date: Wed, 20 May 2026 18:39:23 +0300 Subject: [PATCH 4/5] Assert not null --- plugin/assert/Assert.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugin/assert/Assert.php b/plugin/assert/Assert.php index 74e3542..2c8f441 100644 --- a/plugin/assert/Assert.php +++ b/plugin/assert/Assert.php @@ -264,17 +264,18 @@ public static function null( */ #[AssertMethod] public static function notNull( - mixed $actual, + mixed $actual, string $message = '', ): void { $actual !== null ? StaticState::success($actual, 'is not `null`', $message) - : StaticState::fail(new AssertionException( - value: 'null', + : StaticState::fail(new ComparisonFailure( + expected: 'non-null', + actual: $actual, + value: Support::stringify($actual), assertion: 'is not `null`', context: $message, reason: 'expected a non-null value, got `null`', - details: '', )); } From dc521dc6358c43b26c18e8834eb58ffa678dc1b1 Mon Sep 17 00:00:00 2001 From: Sergey <45151835+burn1ngbear@users.noreply.github.com> Date: Wed, 20 May 2026 18:46:08 +0300 Subject: [PATCH 5/5] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- plugin/assert/Assert.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/assert/Assert.php b/plugin/assert/Assert.php index 2c8f441..2c2b96d 100644 --- a/plugin/assert/Assert.php +++ b/plugin/assert/Assert.php @@ -264,7 +264,7 @@ public static function null( */ #[AssertMethod] public static function notNull( - mixed $actual, + mixed $actual, string $message = '', ): void { $actual !== null