|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\JsonSchema; |
| 4 | + |
| 5 | +use Illuminate\JsonSchema\JsonSchema; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class AnyOfTypeTest extends TestCase |
| 9 | +{ |
| 10 | + public function test_it_may_describe_any_of_multiple_schemas(): void |
| 11 | + { |
| 12 | + $type = JsonSchema::anyOf([ |
| 13 | + JsonSchema::string(), |
| 14 | + JsonSchema::integer(), |
| 15 | + ])->title('Identifier'); |
| 16 | + |
| 17 | + $this->assertEquals([ |
| 18 | + 'title' => 'Identifier', |
| 19 | + 'anyOf' => [ |
| 20 | + ['type' => 'string'], |
| 21 | + ['type' => 'integer'], |
| 22 | + ], |
| 23 | + ], $type->toArray()); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_it_may_be_initialized_with_a_closure(): void |
| 27 | + { |
| 28 | + $type = JsonSchema::anyOf(fn (JsonSchema $schema): array => [ |
| 29 | + $schema->string(), |
| 30 | + $schema->integer(), |
| 31 | + ]); |
| 32 | + |
| 33 | + $this->assertEquals([ |
| 34 | + 'anyOf' => [ |
| 35 | + ['type' => 'string'], |
| 36 | + ['type' => 'integer'], |
| 37 | + ], |
| 38 | + ], $type->toArray()); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_it_may_be_nullable(): void |
| 42 | + { |
| 43 | + $type = JsonSchema::anyOf([ |
| 44 | + JsonSchema::string(), |
| 45 | + JsonSchema::integer(), |
| 46 | + ])->nullable(); |
| 47 | + |
| 48 | + $this->assertEquals([ |
| 49 | + 'anyOf' => [ |
| 50 | + ['type' => 'string'], |
| 51 | + ['type' => 'integer'], |
| 52 | + ['type' => 'null'], |
| 53 | + ], |
| 54 | + ], $type->toArray()); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_it_may_describe_object_unions(): void |
| 58 | + { |
| 59 | + $type = JsonSchema::anyOf([ |
| 60 | + JsonSchema::object([ |
| 61 | + 'type' => JsonSchema::string()->enum(['article'])->required(), |
| 62 | + 'title' => JsonSchema::string()->required(), |
| 63 | + 'content' => JsonSchema::string()->required(), |
| 64 | + ]), |
| 65 | + JsonSchema::object([ |
| 66 | + 'type' => JsonSchema::string()->enum(['image'])->required(), |
| 67 | + 'url' => JsonSchema::string()->required(), |
| 68 | + 'caption' => JsonSchema::string(), |
| 69 | + ]), |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->assertEquals([ |
| 73 | + 'anyOf' => [ |
| 74 | + [ |
| 75 | + 'type' => 'object', |
| 76 | + 'properties' => [ |
| 77 | + 'type' => ['type' => 'string', 'enum' => ['article']], |
| 78 | + 'title' => ['type' => 'string'], |
| 79 | + 'content' => ['type' => 'string'], |
| 80 | + ], |
| 81 | + 'required' => ['type', 'title', 'content'], |
| 82 | + ], |
| 83 | + [ |
| 84 | + 'type' => 'object', |
| 85 | + 'properties' => [ |
| 86 | + 'type' => ['type' => 'string', 'enum' => ['image']], |
| 87 | + 'url' => ['type' => 'string'], |
| 88 | + 'caption' => ['type' => 'string'], |
| 89 | + ], |
| 90 | + 'required' => ['type', 'url'], |
| 91 | + ], |
| 92 | + ], |
| 93 | + ], $type->toArray()); |
| 94 | + } |
| 95 | +} |
0 commit comments