Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate type #150

Merged
merged 1 commit into from
Jan 7, 2022
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
7 changes: 7 additions & 0 deletions src/CreateDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Koriym\AppStateDiagram\Exception\DescriptorIsNotArrayException;
use Koriym\AppStateDiagram\Exception\InvalidDescriptorException;
use Koriym\AppStateDiagram\Exception\InvalidTypeException;
use stdClass;

use function assert;
Expand All @@ -18,6 +19,8 @@

final class CreateDescriptor
{
private const VALID_TYPES = ['semantic', 'safe', 'unsafe', 'idempotent'];

/**
* @param array<string, stdClass> $descriptorsArray
*
Expand Down Expand Up @@ -118,5 +121,9 @@ private function validateDescriptor(stdClass $descriptor): void
if ($hasNoId) {
throw new InvalidDescriptorException((string) json_encode($descriptor));
}

if (isset($descriptor->type) && ! in_array($descriptor->type, self::VALID_TYPES)) {
throw new InvalidTypeException((string) $descriptor->type);
}
}
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Koriym\AppStateDiagram\Exception;

use RuntimeException;

class InvalidTypeException extends RuntimeException
{
}
22 changes: 22 additions & 0 deletions tests/CreateDescriptorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Koriym\AppStateDiagram;

use Koriym\AppStateDiagram\Exception\InvalidTypeException;
use PHPUnit\Framework\TestCase;
use stdClass;

class CreateDescriptorTest extends TestCase
{
public function testInvalidType(): void
{
$this->expectException(InvalidTypeException::class);
$stdClass = new stdClass();
$stdClass->id = 'About';
$stdClass->type = '_invalid_';
$instances = ['About' => $stdClass];
(new CreateDescriptor())($instances);
}
}