Skip to content

Commit 8ee0073

Browse files
authored
Unit tests for format property (#9)
1 parent 81bc3ab commit 8ee0073

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

tests/Unit/Attribute/FieldTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Spiral\JsonSchemaGenerator\Attribute\Field;
9+
use Spiral\JsonSchemaGenerator\Schema\Format;
910

1011
final class FieldTest extends TestCase
1112
{
1213
#[Field]
1314
private string $default = '';
1415

15-
#[Field(title: 'Title', description: 'Description', default: 'foo')]
16+
#[Field(title: 'Title', description: 'Description', default: 'foo', format: Format::Email)]
1617
private string $withValues = 'foo';
1718

1819
public function testFieldWithDefaultValues(): void
@@ -36,4 +37,13 @@ public function testFieldWithValues(): void
3637
$this->assertSame('Description', $attr->description);
3738
$this->assertSame('foo', $attr->default);
3839
}
40+
41+
public function testFieldWithFormat(): void
42+
{
43+
$ref = new \ReflectionProperty(self::class, 'withValues');
44+
45+
$attr = $ref->getAttributes(Field::class)[0]->newInstance();
46+
$this->assertInstanceOf('Spiral\JsonSchemaGenerator\Schema\Format', $attr->format);
47+
$this->assertSame('email', $attr->format->value);
48+
}
3949
}

tests/Unit/Fixture/Movie.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Spiral\JsonSchemaGenerator\Tests\Unit\Fixture;
66

77
use Spiral\JsonSchemaGenerator\Attribute\Field;
8+
use Spiral\JsonSchemaGenerator\Schema\Format;
89

910
class Movie
1011
{
@@ -18,5 +19,7 @@ public function __construct(
1819
public readonly ?string $director = null,
1920
#[Field(title: 'Release Status', description: 'The release status of the movie')]
2021
public readonly ?ReleaseStatus $releaseStatus = null,
22+
#[Field(title: 'Release date', description: 'The release date of the movie', format: Format::Date)]
23+
public readonly ?string $releaseDate = null,
2124
) {}
2225
}

tests/Unit/GeneratorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function testGenerateMovie(): void
4646
],
4747
],
4848
],
49+
'releaseDate' => [
50+
'type' => 'string',
51+
'format' => 'date',
52+
'title' => 'Release date',
53+
'description' => 'The release date of the movie',
54+
],
4955
],
5056
'required' => [
5157
'title',
@@ -142,6 +148,12 @@ public function testGenerateActor(): void
142148
],
143149
],
144150
],
151+
'releaseDate' => [
152+
'type' => 'string',
153+
'format' => 'date',
154+
'title' => 'Release date',
155+
'description' => 'The release date of the movie',
156+
],
145157
],
146158
'required' => [
147159
'title',

tests/Unit/Schema/PropertyTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Spiral\JsonSchemaGenerator\Exception\InvalidTypeException;
9+
use Spiral\JsonSchemaGenerator\Schema\Format;
910
use Spiral\JsonSchemaGenerator\Schema\Property;
1011
use Spiral\JsonSchemaGenerator\Schema\Type;
1112
use Spiral\JsonSchemaGenerator\Tests\Unit\Fixture\Actor;
@@ -147,4 +148,16 @@ public function testInvalidTypeException(): void
147148
$this->expectException(InvalidTypeException::class);
148149
new Property(type: 'foo');
149150
}
151+
152+
public function testPropertyWithFormat(): void
153+
{
154+
$property = new Property(type: Type::String, title: 'Homepage', format: Format::Uri);
155+
156+
$this->assertEquals([
157+
'type' => 'string',
158+
'title' => 'Homepage',
159+
'format' => 'uri',
160+
], $property->jsonSerialize());
161+
}
162+
150163
}

0 commit comments

Comments
 (0)