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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ declare(strict_types=1);

namespace Tests;

use Testo\Application\Attribute\Test;
use Testo\Assert;
use Testo\Assert\ExpectException;
use Testo\Attribute\Test;
use Testo\Retry\RetryPolicy;

final class CalculatorTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Testo\Application\Middleware\Locator;

use Testo\Application\Attribute\Test;
use Testo\Attribute\Test;
use Testo\Common\Reflection;
use Testo\Core\Definition\CaseDefinitions;
use Testo\Core\Value\TestType;
Expand Down
11 changes: 11 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Testo\Assert\State\AssertException;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Assert\State\Test\Fail;
use Testo\Attribute\AssertMethod;

/**
* Assertion utilities.
Expand All @@ -38,6 +39,7 @@ final class Assert
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function same(mixed $expected, mixed $actual, string $message = ''): void
{
$actual === $expected
Expand All @@ -53,6 +55,7 @@ public static function same(mixed $expected, mixed $actual, string $message = ''
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function notSame(mixed $expected, mixed $actual, string $message = ''): void
{
$actual !== $expected
Expand All @@ -74,6 +77,7 @@ public static function notSame(mixed $expected, mixed $actual, string $message =
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function equals(mixed $expected, mixed $actual, string $message = ''): void
{
$actual == $expected
Expand All @@ -94,6 +98,7 @@ public static function equals(mixed $expected, mixed $actual, string $message =
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function notEquals(mixed $expected, mixed $actual, string $message = ''): void
{
$actual != $expected
Expand All @@ -114,6 +119,7 @@ public static function notEquals(mixed $expected, mixed $actual, string $message
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function true(mixed $actual, string $message = ''): void
{
$actual === true
Expand All @@ -133,6 +139,7 @@ public static function true(mixed $actual, string $message = ''): void
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function false(mixed $actual, string $message = ''): void
{
$actual === false
Expand Down Expand Up @@ -194,6 +201,7 @@ public static function contains(mixed $needle, iterable $haystack, string $messa
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function null(
mixed $actual,
string $message = '',
Expand All @@ -215,6 +223,7 @@ public static function null(
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function blank(
mixed $actual,
string $message = '',
Expand All @@ -238,6 +247,7 @@ public static function blank(
*
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
public static function string(mixed $actual): StringType
{
return AssertString::validateAndCreate($actual);
Expand Down Expand Up @@ -335,6 +345,7 @@ public static function object(mixed $actual): ObjectType
* @param string $message The reason for the failure.
* @throws AssertException
*/
#[AssertMethod]
public static function fail(string $message = ''): never
{
$exception = new Fail($message);
Expand Down
4 changes: 4 additions & 0 deletions src/Assert/Internal/Assertion/AssertArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Testo\Assert\Internal\StaticState;
use Testo\Assert\State\Assertion\AssertionComposite;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Attribute\AssertMethod;

/**
* Assertion utilities for arrays.
Expand Down Expand Up @@ -39,6 +40,7 @@ public static function validateAndCreate(mixed $value): self
return new self($value, $parent);
}

#[AssertMethod]
#[\Override]
public function hasKeys(int|string ...$keys): static
{
Expand Down Expand Up @@ -70,6 +72,7 @@ public function hasKeys(int|string ...$keys): static
);
}

#[AssertMethod]
#[\Override]
public function doesNotHaveKeys(string|int ...$keys): static
{
Expand Down Expand Up @@ -101,6 +104,7 @@ public function doesNotHaveKeys(string|int ...$keys): static
);
}

#[AssertMethod]
#[\Override]
public function isList(string $message = ''): static
{
Expand Down
13 changes: 13 additions & 0 deletions src/Assert/Internal/Assertion/AssertJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Testo\Assert\Api\Json\JsonObject;
use Testo\Assert\Api\Json\JsonStructure;
use Testo\Assert\State\Assertion\AssertionComposite;
use Testo\Attribute\AssertMethod;

/**
* Implementation of JSON assertions.
Expand All @@ -30,6 +31,7 @@ public function __construct(
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function maxDepth(int $expected): static
{
Expand All @@ -41,6 +43,7 @@ public function maxDepth(int $expected): static
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function empty(): JsonCommon
{
Expand All @@ -56,6 +59,7 @@ public function empty(): JsonCommon
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function assertPath(string $path, callable $callback): static
{
Expand All @@ -69,6 +73,7 @@ public function assertPath(string $path, callable $callback): static
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function count(int $count, string $message = ''): static
{
Expand All @@ -78,6 +83,7 @@ public function count(int $count, string $message = ''): static
/**
* Asserts that the JSON string represents a valid JSON structure (object or array).
*/
#[AssertMethod]
#[\Override]
public function isStructure(): JsonStructure
{
Expand All @@ -87,6 +93,7 @@ public function isStructure(): JsonStructure
/**
* Assert that the JSON string represents a valid JSON object.
*/
#[AssertMethod]
#[\Override]
public function isObject(): JsonObject
{
Expand All @@ -96,6 +103,7 @@ public function isObject(): JsonObject
/**
* Assert that the JSON string represents a valid JSON array.
*/
#[AssertMethod]
#[\Override]
public function isArray(): JsonArray
{
Expand All @@ -105,6 +113,7 @@ public function isArray(): JsonArray
/**
* Assert that the JSON string represents a primitive value (string, number, boolean, null).
*/
#[AssertMethod]
#[\Override]
public function isPrimitive(): JsonCommon
{
Expand All @@ -119,6 +128,7 @@ public function isPrimitive(): JsonCommon
*
* @param non-empty-string $type The Psalm type to validate against
*/
#[AssertMethod]
#[\Override]
public function matchesType(string $type): static
{
Expand All @@ -132,6 +142,7 @@ public function matchesType(string $type): static
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function matchesSchema(string $schema): static
{
Expand All @@ -143,6 +154,7 @@ public function matchesSchema(string $schema): static
*
* @param array<string>|string $keys The keys to check for existence.
*/
#[AssertMethod]
#[\Override]
public function hasKeys(array|string $keys, string $message = ''): JsonObject
{
Expand All @@ -156,6 +168,7 @@ public function hasKeys(array|string $keys, string $message = ''): JsonObject
*
* @deprecated To be implemented
*/
#[AssertMethod]
#[\Override]
public function decode(): mixed
{
Expand Down
3 changes: 3 additions & 0 deletions src/Assert/Internal/Assertion/AssertObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Testo\Assert\Internal\StaticState;
use Testo\Assert\State\Assertion\AssertionComposite;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Attribute\AssertMethod;

/**
* Assertion utilities for objects.
Expand Down Expand Up @@ -38,6 +39,7 @@ public static function validateAndCreate(mixed $value): self
return new self($value, $parent);
}

#[AssertMethod]
#[\Override]
public function instanceOf(string $expected, string $message = ''): static
{
Expand All @@ -48,6 +50,7 @@ public function instanceOf(string $expected, string $message = ''): static
return $this;
}

#[AssertMethod]
#[\Override]
public function hasProperty(string $propertyName, string $message = ''): static
{
Expand Down
41 changes: 15 additions & 26 deletions src/Assert/Internal/Assertion/AssertString.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Testo\Assert\State\AssertException;
use Testo\Assert\State\Assertion\AssertionComposite;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Attribute\AssertMethod;

/**
* Assertion utilities for string data type.
Expand Down Expand Up @@ -41,48 +42,36 @@ public static function validateAndCreate(mixed $value): self
/**
* Asserts that the string contains the given substring.
*
* @param string $needle Substring to search for.
* @param non-empty-string $needle Substring to search for.
* @param string $message Optional message for the assertion.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
#[\Override]
public function contains(string $needle, string $message = ''): static
{
if (\str_contains($this->value, $needle)) {
StaticState::success($this->value, ' contains "' . $needle . '"', $message);
return $this;
}

StaticState::fail(AssertException::compare(
$needle,
$this->value,
$message,
pattern: 'Failed asserting that string `%2$s` contains `%1$s`.',
showDiff: false,
));
$str = 'contains "' . $needle . '"';
\str_contains($this->value, $needle)
? $this->parent->success($str, $message)
: throw $this->parent->fail($str, 'the substring is not found', $message);
return $this;
}

/**
* Asserts that the string does not contain the given substring.
*
* @param string $needle Substring to search for.
* @param non-empty-string $needle Substring to search for.
* @param string $message Optional message for the assertion.
* @throws AssertException when the assertion fails.
*/
#[AssertMethod]
#[\Override]
public function notContains(string $needle, string $message = ''): static
{
if (!\str_contains($this->value, $needle)) {
StaticState::success($this->value, 'does not contain "' . $needle . '"', $message);
return $this;
}

StaticState::fail(AssertException::compare(
$needle,
$this->value,
$message,
pattern: 'Failed asserting that string `%2$s` does not contain `%1$s`.',
showDiff: false,
));
$str = 'does not contain "' . $needle . '"';
!\str_contains($this->value, $needle)
? $this->parent->success($str, $message)
: throw $this->parent->fail($str, 'the substring is found', $message);
return $this;
}
}
6 changes: 6 additions & 0 deletions src/Assert/Internal/Assertion/Traits/IterableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Testo\Assert\Internal\Support;
use Testo\Assert\State\Assertion\AssertionComposite;
use Testo\Attribute\AssertMethod;

/**
* Contains assertion methods for iterable values.
Expand All @@ -15,6 +16,7 @@
*/
trait IterableTrait
{
#[AssertMethod]
#[\Override]
public function notEmpty(string $message = ''): static
{
Expand All @@ -31,6 +33,7 @@ public function notEmpty(string $message = ''): static
);
}

#[AssertMethod]
#[\Override]
public function contains(mixed $needle, string $message = ''): static
{
Expand Down Expand Up @@ -68,6 +71,7 @@ public function every(callable $callback, string $message = ''): static
return $this;
}

#[AssertMethod]
#[\Override]
public function sameSizeAs(iterable $expected, string $message = ''): static
{
Expand All @@ -86,6 +90,7 @@ public function sameSizeAs(iterable $expected, string $message = ''): static
);
}

#[AssertMethod]
#[\Override]
public function allOf(string $type, string $message = ''): static
{
Expand All @@ -111,6 +116,7 @@ public function allOf(string $type, string $message = ''): static
return $this;
}

#[AssertMethod]
#[\Override]
public function hasCount(int $expected): static
{
Expand Down
Loading
Loading