Skip to content

Commit

Permalink
Validate type
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Jan 7, 2022
1 parent 537afb0 commit e988229
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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);
}
}

0 comments on commit e988229

Please sign in to comment.