Skip to content

Commit 381bff9

Browse files
authored
Merge pull request #62: implement AssertObject methods
2 parents 70f6b36 + bc29f82 commit 381bff9

File tree

11 files changed

+49
-29
lines changed

11 files changed

+49
-29
lines changed

src/Assert.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static function false(mixed $actual, string $message = ''): void
153153
* @param class-string<ExpectedType> $expected Fully-qualified class or interface name.
154154
* @param mixed $actual The actual object to check.
155155
* @param string $message Optional message for the assertion.
156-
* @throws AssertException when the assertion fails.
156+
* @throws AssertionException when the assertion fails.
157157
*
158158
* @psalm-assert ExpectedType $actual
159159
* @phpstan-assert ExpectedType $actual
@@ -318,11 +318,10 @@ public static function array(mixed $actual): ArrayType
318318
*
319319
* @throws AssertionException
320320
*
321-
* @deprecated To be implemented
322321
*/
323322
public static function object(mixed $actual): ObjectType
324323
{
325-
throw new \LogicException('Not implemented yet');
324+
return AssertObject::validateAndCreate($actual);
326325
}
327326

328327
/**

src/Assert/Api/Builtin/ArrayType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Testo\Assert\Api\Builtin;
66

7-
use Testo\Assert\State\AssertException;
7+
use Testo\Assert\State\Assertion\AssertionException;
88

99
/**
1010
* Assertion utilities for array-like data type.
@@ -17,7 +17,7 @@ interface ArrayType extends IterableType
1717
* Asserts that the array contains given key.
1818
*
1919
* @param int|string ...$keys The keys to check for existence in the array.
20-
* @throws AssertException when the assertion fails.
20+
* @throws AssertionException when the assertion fails.
2121
*/
2222
public function hasKeys(int|string ...$keys): static;
2323

@@ -28,7 +28,7 @@ public function hasKeys(int|string ...$keys): static;
2828
* Equivalent to {@see array_is_list()} in PHP 8.1+.
2929
*
3030
* @param string $message Optional message for the assertion.
31-
* @throws AssertException when the assertion fails.
31+
* @throws AssertionException when the assertion fails.
3232
*/
3333
public function isList(string $message = ''): static;
3434
}

src/Assert/Api/Builtin/IterableType.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Testo\Assert\Api\Builtin;
66

77
use Testo\Assert\State\AssertException;
8+
use Testo\Assert\State\Assertion\AssertionException;
89

910
/**
1011
* Assertion utilities for iterables.
@@ -20,7 +21,7 @@ interface IterableType
2021
*
2122
* @param mixed $needle The value to look for within the iterable.
2223
* @param string $message Optional message for the assertion.
23-
* @throws AssertException when the assertion fails.
24+
* @throws AssertionException when the assertion fails.
2425
*/
2526
public function contains(mixed $needle, string $message = ''): static;
2627

@@ -29,15 +30,15 @@ public function contains(mixed $needle, string $message = ''): static;
2930
*
3031
* @param iterable $expected The iterable to compare size against.
3132
* @param string $message Optional message for the assertion.
32-
* @throws AssertException when the assertion fails.
33+
* @throws AssertionException when the assertion fails.
3334
*/
3435
public function sameSizeAs(iterable $expected, string $message = ''): static;
3536

3637
/**
3738
* Asserts that the iterable has the expected number of elements.
3839
*
3940
* @param int $expected The expected count of elements.
40-
* @throws AssertException when the assertion fails.
41+
* @throws AssertionException when the assertion fails.
4142
*/
4243
public function hasCount(int $expected): static;
4344

@@ -47,7 +48,7 @@ public function hasCount(int $expected): static;
4748
* @param non-empty-string $type The expected type name (e.g., 'int', 'string', 'object', class name)
4849
* considered valid by {@see \get_debug_type()}.
4950
* @param string $message Optional message for the assertion.
50-
* @throws AssertException when the assertion fails.
51+
* @throws AssertionException when the assertion fails.
5152
*/
5253
public function allOf(string $type, string $message = ''): static;
5354

src/Assert/Api/Builtin/NumericType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Testo\Assert\Api\Builtin;
66

7-
use Testo\Assert\State\AssertException;
7+
use Testo\Assert\State\Assertion\AssertionException;
88

99
/**
1010
* Assertion utilities for numeric data types.
@@ -18,7 +18,7 @@ interface NumericType
1818
*
1919
* @param int|float $min Minimum threshold to compare with.
2020
* @param string $message Optional message for the assertion.
21-
* @throws AssertException when the assertion fails.
21+
* @throws AssertionException when the assertion fails.
2222
*/
2323
public function greaterThan(int|float $min, string $message = ''): static;
2424

@@ -27,7 +27,7 @@ public function greaterThan(int|float $min, string $message = ''): static;
2727
*
2828
* @param int|float $min Minimum threshold to compare with.
2929
* @param string $message Optional message for the assertion.
30-
* @throws AssertException when the assertion fails.
30+
* @throws AssertionException when the assertion fails.
3131
*/
3232
public function greaterThanOrEqual(int|float $min, string $message = ''): static;
3333

@@ -36,7 +36,7 @@ public function greaterThanOrEqual(int|float $min, string $message = ''): static
3636
*
3737
* @param int|float $max Maximum threshold to compare with.
3838
* @param string $message Optional message for the assertion.
39-
* @throws AssertException when the assertion fails.
39+
* @throws AssertionException when the assertion fails.
4040
*/
4141
public function lessThan(int|float $max, string $message = ''): static;
4242

@@ -45,7 +45,7 @@ public function lessThan(int|float $max, string $message = ''): static;
4545
*
4646
* @param int|float $max Maximum threshold to compare with.
4747
* @param string $message Optional message for the assertion.
48-
* @throws AssertException when the assertion fails.
48+
* @throws AssertionException when the assertion fails.
4949
*/
5050
public function lessThanOrEqual(int|float $max, string $message = ''): static;
5151
}

src/Assert/Api/Builtin/ObjectType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Testo\Assert\Api\Builtin;
66

7-
use Testo\Assert\State\AssertException;
7+
use Testo\Assert\State\Assertion\AssertionException;
88

99
/**
1010
* Assertion utilities for objects.
@@ -18,7 +18,7 @@ interface ObjectType
1818
*
1919
* @param class-string<ExpectedType> $expected Fully-qualified class or interface name.
2020
* @param string $message Optional message for the assertion.
21-
* @throws AssertException when the assertion fails.
21+
* @throws AssertionException when the assertion fails.
2222
*
2323
* @psalm-assert ExpectedType $actual
2424
* @phpstan-assert ExpectedType $actual
@@ -30,9 +30,8 @@ public function instanceOf(string $expected, string $message = ''): static;
3030
*
3131
* @param non-empty-string $propertyName The property name to check.
3232
* @param string $message Optional message for the assertion.
33-
* @throws AssertException when the assertion fails.
33+
* @throws AssertionException when the assertion fails.
3434
*
35-
* @deprecated To be implemented
3635
*/
3736
public function hasProperty(string $propertyName, string $message = ''): static;
3837
}

src/Assert/Api/Builtin/StringType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Testo\Assert\Api\Builtin;
66

7-
use Testo\Assert\State\AssertException;
7+
use Testo\Assert\State\Assertion\AssertionException;
88

99
/**
1010
* Assertion utilities for string data type.
@@ -16,7 +16,7 @@ interface StringType
1616
*
1717
* @param string $needle Substring to search for.
1818
* @param string $message Optional message for the assertion.
19-
* @throws AssertException when the assertion fails.
19+
* @throws AssertionException when the assertion fails.
2020
*/
2121
public function contains(string $needle, string $message = ''): static;
2222

@@ -25,7 +25,7 @@ public function contains(string $needle, string $message = ''): static;
2525
*
2626
* @param string $needle Substring to search for.
2727
* @param string $message Optional message for the assertion.
28-
* @throws AssertException when the assertion fails.
28+
* @throws AssertionException when the assertion fails.
2929
*/
3030
public function notContains(string $needle, string $message = ''): static;
3131
}

src/Assert/Internal/Assertion/AssertArray.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Testo\Assert\State\Assertion\AssertionComposite;
1010
use Testo\Assert\State\Assertion\AssertionException;
1111
use Testo\Assert\StaticState;
12-
use Testo\Assert\Support;
1312

1413
/**
1514
* Assertion utilities for arrays.

src/Assert/Internal/Assertion/AssertObject.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace Testo\Assert\Internal\Assertion;
66

77
use Testo\Assert\Api\Builtin\ObjectType;
8-
use Testo\Assert\State\AssertException;
98
use Testo\Assert\State\Assertion\AssertionComposite;
9+
use Testo\Assert\State\Assertion\AssertionException;
1010
use Testo\Assert\StaticState;
1111

1212
/**
13-
* Assertion utilities for iterables.
13+
* Assertion utilities for objects.
1414
*
1515
* @internal
1616
*/
@@ -24,8 +24,8 @@ public function __construct(
2424
/**
2525
* @template ValueType
2626
*
27-
* @param ValueType $value The value to be asserted as float.
28-
* @throws AssertException
27+
* @param ValueType $value The value to be asserted as object.
28+
* @throws AssertionException when the value is not an object.
2929
*
3030
* @psalm-assert object $value
3131
* @phpstan-assert object $value
@@ -51,6 +51,10 @@ public function instanceOf(string $expected, string $message = ''): static
5151
#[\Override]
5252
public function hasProperty(string $propertyName, string $message = ''): static
5353
{
54-
throw new \LogicException('Not implemented yet');
54+
$str = "has property `{$propertyName}`";
55+
\property_exists($this->value, $propertyName)
56+
? $this->parent->success($str, $message)
57+
: throw $this->parent->fail($str, 'does not have property `' . $propertyName . '`', $message);
58+
return $this;
5559
}
5660
}

src/Assert/Internal/Assertion/Traits/IterableTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Testo\Assert\Internal\Assertion\Traits;
66

77
use Testo\Assert\State\Assertion\AssertionComposite;
8-
use Testo\Assert\State\Assertion\AssertionException;
98
use Testo\Assert\Support;
109

1110
/**

src/Common/CloneWith.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ trait CloneWith
1313
{
1414
/**
1515
* Return a new immutable instance with the specified property value.
16+
* @param non-empty-string $key The property name to set.
17+
*
1618
* @psalm-immutable
1719
*/
1820
private function cloneWith(string $key, mixed $value): static

0 commit comments

Comments
 (0)