|
| 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\BooleanToYesNoSubscriber; |
| 26 | +use DarkWebDesign\SymfonyAddonFormTypes\BooleanType; |
| 27 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 28 | + |
| 29 | +/** |
| 30 | + * @covers \DarkWebDesign\SymfonyAddonFormTypes\BooleanToYesNoSubscriber |
| 31 | + * |
| 32 | + * @uses \DarkWebDesign\SymfonyAddonFormTypes\BooleanType |
| 33 | + * @uses \DarkWebDesign\SymfonyAddonTransformers\BooleanToValueTransformer |
| 34 | + */ |
| 35 | +class BooleanToYesNoSubscriberTest extends TypeTestCase |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @dataProvider provider |
| 39 | + */ |
| 40 | + public function test(?bool $value): void |
| 41 | + { |
| 42 | + $form = $this->factory->createBuilder() |
| 43 | + ->add('isActive', BooleanType::class) |
| 44 | + ->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive'])) |
| 45 | + ->getForm(); |
| 46 | + |
| 47 | + $form->submit([ |
| 48 | + 'isActive' => $value, |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->assertTrue($form->isSynchronized()); |
| 52 | + $this->assertSame($value, $form->get('isActive')->getData()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testDataNoField(): void |
| 56 | + { |
| 57 | + $form = $this->factory->createBuilder() |
| 58 | + ->add('isActive', BooleanType::class) |
| 59 | + ->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive'])) |
| 60 | + ->getForm(); |
| 61 | + |
| 62 | + $form->submit([]); |
| 63 | + |
| 64 | + $this->assertTrue($form->isSynchronized()); |
| 65 | + $this->assertNull($form->get('isActive')->getData()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testDataNotArray(): void |
| 69 | + { |
| 70 | + $form = $this->factory->createBuilder() |
| 71 | + ->add('isActive', BooleanType::class) |
| 72 | + ->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive'])) |
| 73 | + ->getForm(); |
| 74 | + |
| 75 | + $form->submit('not-array'); |
| 76 | + |
| 77 | + $this->assertFalse($form->isSynchronized()); |
| 78 | + $this->assertNull($form->get('isActive')->getData()); |
| 79 | + } |
| 80 | + |
| 81 | + public function provider(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + 'true' => [true], |
| 85 | + 'false' => [false], |
| 86 | + 'null' => [null], |
| 87 | + ]; |
| 88 | + } |
| 89 | +} |
0 commit comments