-
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.
- Loading branch information
Showing
3 changed files
with
267 additions
and
2 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
80 changes: 80 additions & 0 deletions
80
_metadata/src/Core/MetadataValidationEngine/MetadataValidator.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,80 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the medicalmundi/marketplace-engine | ||
* | ||
* @copyright (c) 2024 MedicalMundi | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT | ||
*/ | ||
|
||
namespace Metadata\Core\MetadataValidationEngine; | ||
|
||
class MetadataValidator implements ForMetadataSchemaValidation | ||
{ | ||
private const ALLOWED_CATEGORY = [ | ||
'administration', | ||
'billing', | ||
'ePrescribing', | ||
'miscellaneous', | ||
'telecom', | ||
'telehealth', | ||
'payment', | ||
]; | ||
|
||
private const ALLOWED_TAG = [ | ||
'fax', | ||
'reminder', | ||
'sms', | ||
'todo', | ||
]; | ||
|
||
/** | ||
* @throws MetadataValidationException | ||
* | ||
* fail when | ||
* - [] no key category | ||
* - [] category is not a string | ||
* - [] no key tags | ||
* - [] tags is not an array | ||
*/ | ||
public function validate(array $metadata): bool | ||
{ | ||
if (! \array_key_exists('category', $metadata)) { | ||
throw new MetadataValidationException('No key category'); | ||
} else { | ||
$category = $metadata['category']; | ||
|
||
if (! \is_string($category)) { | ||
throw new MetadataValidationException('Category should be string type'); | ||
} | ||
|
||
if (! \in_array($category, self::ALLOWED_CATEGORY)) { | ||
throw new MetadataValidationException('Category not allowed: ' . $category); | ||
} | ||
} | ||
|
||
if (! \array_key_exists('tags', $metadata)) { | ||
throw new MetadataValidationException('No key tags'); | ||
} else { | ||
$tags = $metadata['tags']; | ||
|
||
if (! \is_array($tags)) { | ||
throw new MetadataValidationException('Tags should be array type'); | ||
} | ||
/** @var string $tag */ | ||
foreach ($tags as $tag) { | ||
if (! \in_array($tag, self::ALLOWED_TAG)) { | ||
throw new MetadataValidationException('Tag not allowed: ' . $tag); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
_metadata/tests/Unit/Core/MetadataValidationEngine/MetadataValidatorTest.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,184 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the medicalmundi/marketplace-engine | ||
* | ||
* @copyright (c) 2024 MedicalMundi | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT | ||
*/ | ||
|
||
namespace MetadataTests\Unit\Core\MetadataValidationEngine; | ||
|
||
use Metadata\Core\MetadataValidationEngine\MetadataValidationException; | ||
use Metadata\Core\MetadataValidationEngine\MetadataValidator; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(MetadataValidator::class)] | ||
class MetadataValidatorTest extends TestCase | ||
{ | ||
#[Test] | ||
public function shouldFailWhenThereIsNoCategoryKey(): void | ||
{ | ||
self::expectException(MetadataValidationException::class); | ||
self::expectExceptionMessage('No key category'); | ||
$validator = new MetadataValidator(); | ||
|
||
$validator->validate([ | ||
'tags' => 'irrelevant', | ||
]); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('invalidCategoryDataprovider')] | ||
public function shouldFailWhenCategoryIsNotValid(array $metadata): void | ||
{ | ||
self::expectException(MetadataValidationException::class); | ||
|
||
$validator = new MetadataValidator(); | ||
$validator->validate($metadata); | ||
} | ||
|
||
#[Test] | ||
public function shouldFailWhenThereIsNoTagsKey(): void | ||
{ | ||
self::expectException(MetadataValidationException::class); | ||
self::expectExceptionMessage('No key tags'); | ||
$validator = new MetadataValidator(); | ||
|
||
$validator->validate([ | ||
'category' => 'billing', | ||
]); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('invalidTagsDataprovider')] | ||
public function shouldFailWhenTagsIsNotValid(array $metadata): void | ||
{ | ||
self::expectException(MetadataValidationException::class); | ||
|
||
$validator = new MetadataValidator(); | ||
$validator->validate($metadata); | ||
} | ||
|
||
#[Test] | ||
public function shouldPassTheValidation(): void | ||
{ | ||
$validator = new MetadataValidator(); | ||
|
||
$result = $validator->validate([ | ||
'category' => 'billing', | ||
'tags' => ['fax', 'sms'], | ||
]); | ||
|
||
self::assertTrue($result); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('approvedCategoryDataprovider')] | ||
#[DataProvider('approvedTagsDataprovider')] | ||
public function shouldPassTheValidationOnlyWithApprovedCategoryAndTags(array $metadata): void | ||
{ | ||
$validator = new MetadataValidator(); | ||
|
||
$result = $validator->validate($metadata); | ||
|
||
self::assertTrue($result); | ||
} | ||
|
||
public static function invalidCategoryDataprovider(): array | ||
{ | ||
return [ | ||
[[ | ||
'category' => null, | ||
'tags' => 'irrelevant', | ||
]], | ||
[[ | ||
'category' => 0, | ||
'tags' => 'irrelevant', | ||
]], | ||
[[ | ||
'category' => [], | ||
'tags' => 'irrelevant', | ||
]], | ||
[[ | ||
'category' => new \stdClass(), | ||
'tags' => 'irrelevant', | ||
]], | ||
]; | ||
} | ||
|
||
public static function invalidTagsDataprovider(): array | ||
{ | ||
return [ | ||
[[ | ||
'category' => 'irrelevant', | ||
'tags' => null, | ||
]], | ||
[[ | ||
'category' => 'irrelevant', | ||
'tags' => 0, | ||
]], | ||
[[ | ||
'category' => 'irrelevant', | ||
'tags' => 'string', | ||
]], | ||
[[ | ||
'category' => 'irrelevant', | ||
'tags' => new \stdClass(), | ||
]], | ||
]; | ||
} | ||
|
||
public static function approvedCategoryDataprovider(): array | ||
{ | ||
return [ | ||
[[ | ||
'category' => 'administration', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'billing', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'ePrescribing', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'miscellaneous', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'telecom', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'telehealth', | ||
'tags' => ['fax'], | ||
]], | ||
[[ | ||
'category' => 'payment', | ||
'tags' => ['fax'], | ||
]], | ||
]; | ||
} | ||
|
||
public static function approvedTagsDataprovider(): array | ||
{ | ||
return [ | ||
[[ | ||
'category' => 'miscellaneous', | ||
'tags' => ['fax'], | ||
]], | ||
]; | ||
} | ||
} |