forked from cakephp/cakephp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cakephp#17403 from cakephp/5.x-enum-validation
5.x - Pull in Enum validation support from 5.next
- Loading branch information
Showing
6 changed files
with
185 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,9 @@ | |
use Laminas\Diactoros\UploadedFile; | ||
use Locale; | ||
use stdClass; | ||
use TestApp\Model\Enum\ArticleStatus; | ||
use TestApp\Model\Enum\NonBacked; | ||
use TestApp\Model\Enum\Priority; | ||
|
||
require_once __DIR__ . '/stubs.php'; | ||
|
||
|
@@ -2014,6 +2017,39 @@ public function testEmailCustomRegex(): void | |
$this->assertFalse(Validation::email('[email protected]', null, '/^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i')); | ||
} | ||
|
||
public function testEnum(): void | ||
{ | ||
$this->assertTrue(Validation::enum(ArticleStatus::PUBLISHED, ArticleStatus::class)); | ||
$this->assertTrue(Validation::enum('Y', ArticleStatus::class)); | ||
|
||
$this->assertTrue(Validation::enum(Priority::LOW, Priority::class)); | ||
$this->assertTrue(Validation::enum(1, Priority::class)); | ||
|
||
$this->assertFalse(Validation::enum(Priority::LOW, ArticleStatus::class)); | ||
$this->assertFalse(Validation::enum(1, ArticleStatus::class)); | ||
$this->assertFalse(Validation::enum('non-existent', ArticleStatus::class)); | ||
|
||
$this->assertFalse(Validation::enum(ArticleStatus::PUBLISHED, Priority::class)); | ||
$this->assertFalse(Validation::enum('wrong type', Priority::class)); | ||
$this->assertFalse(Validation::enum(123, Priority::class)); | ||
} | ||
|
||
public function testEnumNonBacked(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The `$enumClassName` argument must be the classname of a valid backed enum.'); | ||
|
||
Validation::enum(NonBacked::Basic, NonBacked::class); | ||
} | ||
|
||
public function testEnumNonEnum(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The `$enumClassName` argument must be the classname of a valid backed enum.'); | ||
|
||
Validation::enum('non-enum class', TestCase::class); | ||
} | ||
|
||
/** | ||
* testEqualTo method | ||
*/ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @since 5.1.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace TestApp\Model\Enum; | ||
|
||
enum NonBacked | ||
{ | ||
case Basic; | ||
} |