|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfonycasts\DynamicForms\Tests; |
| 4 | + |
| 5 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 6 | +use Symfonycasts\DynamicForms\Tests\fixtures\DynamicFormsTestKernel; |
| 7 | +use Zenstruck\Browser\Test\HasBrowser; |
| 8 | + |
| 9 | +class FunctionalTest extends KernelTestCase |
| 10 | +{ |
| 11 | + use HasBrowser; |
| 12 | + |
| 13 | + public function testDynamicFields() |
| 14 | + { |
| 15 | + $browser = $this->browser(); |
| 16 | + $browser->visit('/form') |
| 17 | + // check for the hidden field |
| 18 | + ->assertSeeElement('#test_dynamic_form___dynamic_error') |
| 19 | + ->assertSee('Is Form Valid: no') |
| 20 | + // Breakfast is the pre-selected meal |
| 21 | + ->assertSee('What is for Breakfast?') |
| 22 | + ->assertContains('<option value="bacon">') |
| 23 | + ->assertNotContains('<option value="pizza">') |
| 24 | + ->assertNotContains('What size pizza?') |
| 25 | + ; |
| 26 | + |
| 27 | + // change the meal to dinner |
| 28 | + $browser->selectFieldOption('Meal', 'Dinner') |
| 29 | + ->click('Submit Form') |
| 30 | + // changing the field doesn't cause any issues |
| 31 | + ->assertSee('Is Form Valid: yes') |
| 32 | + ->assertNotContains('<option value="bacon">') |
| 33 | + ->assertContains('<option value="pizza">') |
| 34 | + ->assertNotContains('What size pizza?') |
| 35 | + ; |
| 36 | + |
| 37 | + // now select Pizza! |
| 38 | + $browser->selectFieldOption('Main food', 'Pizza') |
| 39 | + ->click('Submit Form') |
| 40 | + ->assertSee('Is Form Valid: yes') |
| 41 | + ->assertContains('<option value="pizza" selected="selected">') |
| 42 | + ->assertContains('What size pizza?') |
| 43 | + ; |
| 44 | + |
| 45 | + // select the size |
| 46 | + $browser->selectFieldOption('Pizza size', '14 inch') |
| 47 | + ->click('Submit Form') |
| 48 | + ->assertSee('Is Form Valid: yes') |
| 49 | + ->assertContains('<option value="pizza" selected="selected">') |
| 50 | + ->assertContains('<option value="14" selected="selected">') |
| 51 | + ; |
| 52 | + |
| 53 | + // now change the meal to breakfast |
| 54 | + $browser->selectFieldOption('Meal', 'Breakfast') |
| 55 | + ->click('Submit Form') |
| 56 | + // form is not valid: the mainFood submitted an invalid value |
| 57 | + ->assertSee('Is Form Valid: no') |
| 58 | + ->assertContains('<option value="bacon">') |
| 59 | + ->assertNotContains('<option value="pizza"') |
| 60 | + ->assertNotContains('What size pizza?') |
| 61 | + ; |
| 62 | + |
| 63 | + // select a valid food for breakfast |
| 64 | + $browser->selectFieldOption('Main food', 'Bacon') |
| 65 | + ->click('Submit Form') |
| 66 | + // form is valid again |
| 67 | + ->assertSee('Is Form Valid: yes') |
| 68 | + ; |
| 69 | + |
| 70 | + // change the meal again |
| 71 | + $browser->selectFieldOption('Meal', 'Lunch') |
| 72 | + ->click('Submit Form') |
| 73 | + // form is not valid: the mainFood=bacon is invalid for lunch |
| 74 | + ->assertSee('Is Form Valid: no') |
| 75 | + ; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + protected static function getKernelClass(): string |
| 80 | + { |
| 81 | + return DynamicFormsTestKernel::class; |
| 82 | + } |
| 83 | +} |
0 commit comments