-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
isMultiple
validator. (#21)
* Add support for `isMultiple` validator. * Fix typo in `README.md` --------- Co-authored-by: david_smith <[email protected]>
- Loading branch information
1 parent
a3b2898
commit ecab046
Showing
16 changed files
with
296 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/Unit/IsEmail/ValidUrl/IsEmailTest.php → ...s/Unit/IsEmail/ValidEmail/IsEmailTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/Unit/IsEmail/ValidUrl/User.php → tests/Unit/IsEmail/ValidEmail/User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/Unit/IsMultiple/Callable/InvalidEmail/InvalidMultipleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Callable\InvalidEmail; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
|
||
class InvalidMultipleTest extends TestCase | ||
{ | ||
#[Test] public function invalid_email(): void | ||
{ | ||
$this->expectException(NotMultipleException::class); | ||
|
||
User::from([ | ||
User::value => 2, | ||
]); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/IsMultiple/Callable/InvalidEmail/NotMultipleException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Callable\InvalidEmail; | ||
|
||
use RuntimeException; | ||
|
||
class NotMultipleException extends RuntimeException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Callable\InvalidEmail; | ||
|
||
use Zerotoprod\DataModel\DataModel; | ||
use Zerotoprod\DataModel\Describe; | ||
use Zerotoprod\DataModelHelper\DataModelHelper; | ||
|
||
class User | ||
{ | ||
use DataModel; | ||
use DataModelHelper; | ||
|
||
public const value = 'value'; | ||
|
||
#[Describe([ | ||
'cast' => [self::class, 'isMultiple'], | ||
'of' => 7, | ||
'on_fail' => [self::class, 'failed'], | ||
])] | ||
public int $value; | ||
|
||
public static function failed(): string | ||
{ | ||
throw new NotMultipleException(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Unit/IsMultiple/Exception/InvalidUrl/CustomExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Exception\InvalidUrl; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
|
||
class CustomExceptionTest extends TestCase | ||
{ | ||
#[Test] public function invalid_email(): void | ||
{ | ||
$this->expectException(NotMultipleException::class); | ||
User::from([ | ||
User::value => 2, | ||
]); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/IsMultiple/Exception/InvalidUrl/NotMultipleException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Exception\InvalidUrl; | ||
|
||
use RuntimeException; | ||
|
||
class NotMultipleException extends RuntimeException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Exception\InvalidUrl; | ||
|
||
use Zerotoprod\DataModel\DataModel; | ||
use Zerotoprod\DataModel\Describe; | ||
use Zerotoprod\DataModelHelper\DataModelHelper; | ||
|
||
class User | ||
{ | ||
use DataModel; | ||
use DataModelHelper; | ||
|
||
public const value = 'value'; | ||
|
||
#[Describe([ | ||
'cast' => [self::class, 'isMultiple'], | ||
'of' => 3, | ||
'exception' => NotMultipleException::class | ||
])] | ||
public int $value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Nullable; | ||
|
||
use Zerotoprod\DataModel\DataModel; | ||
use Zerotoprod\DataModel\Describe; | ||
use Zerotoprod\DataModelHelper\DataModelHelper; | ||
|
||
class User | ||
{ | ||
use DataModel; | ||
use DataModelHelper; | ||
|
||
public const value = 'value'; | ||
|
||
#[Describe([ | ||
'cast' => [self::class, 'isMultiple'], | ||
'of' => 10 | ||
])] | ||
public ?int $value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Nullable; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
|
||
class ValidMultipleTest extends TestCase | ||
{ | ||
#[Test] public function valid(): void | ||
{ | ||
$User = User::from([ | ||
User::value => null, | ||
]); | ||
|
||
self::assertNull($User->value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Required; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
use Zerotoprod\DataModel\PropertyRequiredException; | ||
|
||
class RequiredTest extends TestCase | ||
{ | ||
#[Test] public function required(): void | ||
{ | ||
$this->expectException(PropertyRequiredException::class); | ||
$this->expectExceptionMessage('Property `$value` is required.'); | ||
User::from(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\Required; | ||
|
||
use Zerotoprod\DataModel\DataModel; | ||
use Zerotoprod\DataModel\Describe; | ||
use Zerotoprod\DataModelHelper\DataModelHelper; | ||
|
||
class User | ||
{ | ||
use DataModel; | ||
use DataModelHelper; | ||
|
||
public const value = 'value'; | ||
|
||
#[Describe([ | ||
'cast' => [self::class, 'isMultiple'], | ||
'required' | ||
])] | ||
public string $value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\ValidMultiple; | ||
|
||
use Zerotoprod\DataModel\DataModel; | ||
use Zerotoprod\DataModel\Describe; | ||
use Zerotoprod\DataModelHelper\DataModelHelper; | ||
|
||
class User | ||
{ | ||
use DataModel; | ||
use DataModelHelper; | ||
|
||
public const value = 'value'; | ||
|
||
#[Describe([ | ||
'cast' => [self::class, 'isMultiple'], | ||
'of' => 10 | ||
])] | ||
public int $value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\IsMultiple\ValidMultiple; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
|
||
class ValidMultipleTest extends TestCase | ||
{ | ||
#[Test] public function valid(): void | ||
{ | ||
$User = User::from([ | ||
User::value => 100, | ||
]); | ||
|
||
self::assertEquals(100, $User->value); | ||
} | ||
} |