|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2021 DarkWeb Design |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | + * SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace DarkWebDesign\SymfonyAddonFormTypes\Tests; |
| 24 | + |
| 25 | +use DarkWebDesign\SymfonyAddonFormTypes\JsonSchemaSubscriber; |
| 26 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 27 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 28 | + |
| 29 | +/** |
| 30 | + * @covers \DarkWebDesign\SymfonyAddonFormTypes\JsonSchemaSubscriber |
| 31 | + */ |
| 32 | +class JsonSchemaSubscriberTest extends TypeTestCase |
| 33 | +{ |
| 34 | + public function test(): void |
| 35 | + { |
| 36 | + $form = $this->factory->createBuilder() |
| 37 | + ->add('schema', TextType::class) |
| 38 | + ->addEventSubscriber(new JsonSchemaSubscriber()) |
| 39 | + ->getForm(); |
| 40 | + |
| 41 | + $form->submit([ |
| 42 | + '$schema' => 'https://example.com/product.schema.json', |
| 43 | + ]); |
| 44 | + |
| 45 | + $expected = [ |
| 46 | + 'schema' => 'https://example.com/product.schema.json', |
| 47 | + ]; |
| 48 | + |
| 49 | + $this->assertTrue($form->isSynchronized()); |
| 50 | + $this->assertSame($expected, $form->getData()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAlternativeFieldName(): void |
| 54 | + { |
| 55 | + $form = $this->factory->createBuilder() |
| 56 | + ->add('alternativeFieldName', TextType::class) |
| 57 | + ->addEventSubscriber(new JsonSchemaSubscriber('alternativeFieldName')) |
| 58 | + ->getForm(); |
| 59 | + |
| 60 | + $form->submit([ |
| 61 | + '$schema' => 'https://example.com/product.schema.json', |
| 62 | + ]); |
| 63 | + |
| 64 | + $expected = [ |
| 65 | + 'alternativeFieldName' => 'https://example.com/product.schema.json', |
| 66 | + ]; |
| 67 | + |
| 68 | + $this->assertTrue($form->isSynchronized()); |
| 69 | + $this->assertSame($expected, $form->getData()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testDataNoSchema(): void |
| 73 | + { |
| 74 | + $form = $this->factory->createBuilder() |
| 75 | + ->add('schema', TextType::class) |
| 76 | + ->addEventSubscriber(new JsonSchemaSubscriber()) |
| 77 | + ->getForm(); |
| 78 | + |
| 79 | + $form->submit([]); |
| 80 | + |
| 81 | + $expected = [ |
| 82 | + 'schema' => null, |
| 83 | + ]; |
| 84 | + |
| 85 | + $this->assertTrue($form->isSynchronized()); |
| 86 | + $this->assertSame($expected, $form->getData()); |
| 87 | + } |
| 88 | + |
| 89 | + public function testDataNotArray(): void |
| 90 | + { |
| 91 | + $form = $this->factory->createBuilder() |
| 92 | + ->add('alternativeFieldName', TextType::class) |
| 93 | + ->addEventSubscriber(new JsonSchemaSubscriber('alternativeFieldName')) |
| 94 | + ->getForm(); |
| 95 | + |
| 96 | + $form->submit('not-array'); |
| 97 | + |
| 98 | + $this->assertFalse($form->isSynchronized()); |
| 99 | + $this->assertNull($form->getData()); |
| 100 | + } |
| 101 | +} |
0 commit comments