Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions plugin/assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ 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 ComparisonFailure(
expected: 'non-null',
actual: $actual,
value: Support::stringify($actual),
assertion: 'is not `null`',
context: $message,
reason: 'expected a non-null value, got `null`',
));
}

/**
* Checks if a value is blank.
*
Expand Down
49 changes: 49 additions & 0 deletions plugin/assert/tests/Feature/AssertNotNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Feature;

use Testo\Assert;
use Testo\Assert\State\Assertion;
use Testo\Core\Value\Status;
use Testo\Test;
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
{
$result = TestRunner::runTest([AssertNotNullNegative::class, 'nullFails']);

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`');
}

#[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.');
}
}
55 changes: 55 additions & 0 deletions plugin/assert/tests/Self/AssertNotNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Self;

use Testo\Assert;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Expect;
use Testo\Test;

final class AssertNotNull
{
#[Test]
public function checkNonNullValues(): void
{
Assert::notNull(0);
Assert::notNull('');
Assert::notNull(false);
Assert::notNull([]);
Assert::notNull(0.0);
}

#[Test]
public function checkNullFails(): void
{
Expect::exception(AssertionException::class);
Assert::notNull(null);
}

#[Test]
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());
}
}
26 changes: 26 additions & 0 deletions plugin/assert/tests/Stub/AssertNotNullNegative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Stub;

use Testo\Assert;
use Testo\Test;

/**
* Stubs for negative {@see Assert::notNull()} scenarios.
*/
final class AssertNotNullNegative
{
#[Test]
public function nullFails(): void
{
Assert::notNull(null);
}

#[Test]
public function nullFailsWithMessage(): void
{
Assert::notNull(null, 'Value must not be null.');
}
}
24 changes: 24 additions & 0 deletions plugin/assert/tests/Stub/AssertNotNullPositive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Stub;

use Testo\Assert;
use Testo\Test;

/**
* Stubs for positive {@see Assert::notNull()} scenarios.
*/
final class AssertNotNullPositive
{
#[Test]
public function falsyNonNullValues(): void
{
Assert::notNull(0);
Assert::notNull('');
Assert::notNull(false);
Assert::notNull([]);
Assert::notNull(0.0);
}
}
1 change: 1 addition & 0 deletions tests/Testo/Self/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function simpleAssertions(): void
{
Assert::same(1, 1);
Assert::null(null);
Assert::notNull(0);
Assert::notSame('42', 42);
Assert::true(true);
Assert::false(false);
Expand Down
Loading