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
45 changes: 43 additions & 2 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,47 @@ public static function notSame(mixed $expected, mixed $actual, string $message =
));
}

/**
* Asserts that two values are equal (not strict).
*
* @param mixed $expected The expected value.
* @param mixed $actual The actual value to compare against the expected value.
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
public static function equals(mixed $expected, mixed $actual, string $message = ''): void
{
$actual == $expected
? StaticState::log('Assert equals: `' . Support::stringify($expected) . '`', $message)
: StaticState::fail(AssertException::compare(
$expected,
$actual,
$message,
pattern: 'Failed asserting that `%1s` is equals to `%2s`',
));
}

/**
* Asserts that two values are not equal (not strict).
*
* @param mixed $expected The expected value.
* @param mixed $actual The actual value to compare against the expected value.
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
public static function notEquals(mixed $expected, mixed $actual, string $message = ''): void
{
$actual != $expected
? StaticState::log('Assert not equals: `' . Support::stringify($expected) . '`', $message)
: StaticState::fail(AssertException::compare(
$expected,
$actual,
$message,
pattern: 'Failed asserting that `%1s` is not equals to `%2s`',
showDiff: false,
));
}

/**
* Asserts that the condition is true.
*
Expand Down Expand Up @@ -104,7 +145,7 @@ public static function instanceOf(string $expected, mixed $actual, string $messa
$expected,
$actual,
$message,
'Expected instance of `%2$s`, got `%1$s`',
'Expected instance of `%1$s`, got `%2$s`',
));
}

Expand All @@ -128,7 +169,7 @@ public static function contains(mixed $needle, iterable $haystack, string $messa
$needle,
$haystack,
$message,
'Failed asserting that `%1$s` contains `%2$s`',
'Failed asserting that `%2$s` contains `%1$s`',
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Assert/State/AssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static function compare(

$msg = \sprintf(
$pattern,
Support::stringify($actual),
Support::stringify($expected),
Support::stringify($actual),
);
return new self(
assertion: $msg,
Expand Down
5 changes: 5 additions & 0 deletions tests/Testo/AsserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Testo\Attribute\RetryPolicy;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AsserTest
{
#[Test]
Expand All @@ -22,6 +25,8 @@ public function simpleAssertions(): void
Assert::contains(1, [1,2,3]);
Assert::contains(2, new \ArrayIterator([1,2,3]));
Assert::instanceOf(\Exception::class, new \RuntimeException());
Assert::equals(1, '1');
Assert::notEquals(42, 43);
}

#[Test]
Expand Down
48 changes: 48 additions & 0 deletions tests/Testo/Assert/AssertEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tests\Testo\Assert;

use Testo\Assert;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AssertEquals
{
#[Test]
public function numbers(): void
{
Assert::equals(1, 1);
Assert::equals(1, 1.0);
Assert::equals(1.0, 1);
Assert::equals("2", 2);
}

#[Test]
public function arrays(): void
{
# Same
Assert::equals([1, 2], [1, 2]);
Assert::equals(
['a' => 1, 'b' => 2],
['a' => 1, 'b' => 2],
);
# Different order
Assert::equals(
['b' => 2, 'a' => 1],
['a' => 1, 'b' => 2],
);
}

#[Test]
public function objects(): void
{
Assert::equals(
(object) ['a' => 1, 'b' => 2],
(object) ['b' => 2, 'a' => 1],
);
}
}
35 changes: 35 additions & 0 deletions tests/Testo/Assert/AssertNotEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\Testo\Assert;

use Testo\Assert;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AssertNotEquals
{
#[Test]
public function numbers(): void
{
Assert::notEquals(1, 2);
}

#[Test]
public function arrays(): void
{
Assert::notEquals([2, 1], [1, 2]);
}

#[Test]
public function objects(): void
{
Assert::notEquals(
(object) ['a' => 1],
(object) ['a' => 2],
);
}
}