|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Forms\ControlGroup. |
| 5 | + */ |
| 6 | + |
| 7 | +use Tester\Assert; |
| 8 | +use Nette\Forms\ControlGroup; |
| 9 | + |
| 10 | +require __DIR__ . '/../bootstrap.php'; |
| 11 | + |
| 12 | + |
| 13 | +function createContainer() { |
| 14 | + $container = new Nette\Forms\Container; |
| 15 | + $container->addText('street', 'Street'); |
| 16 | + $container->addText('town', 'Town'); |
| 17 | + return $container; |
| 18 | +} |
| 19 | + |
| 20 | +$form = new Nette\Forms\Form; |
| 21 | + |
| 22 | + |
| 23 | +// controls only |
| 24 | +$group = $form->addGroup(); |
| 25 | +$form->addText('name', 'Name'); |
| 26 | +$form->addText('surname', 'Surname'); |
| 27 | + |
| 28 | +Assert::same($group, $form->getGroup(0)); |
| 29 | +Assert::true($form->getGroup(0) instanceof ControlGroup); |
| 30 | +Assert::equal(2, count($group->getControls())); |
| 31 | +Assert::equal([$form['name'], $form['surname']], $group->getControls()); |
| 32 | + |
| 33 | + |
| 34 | +// container only |
| 35 | +$group = $form->addGroup('Address'); |
| 36 | +$form->addComponent(createContainer(), 'address'); |
| 37 | + |
| 38 | +Assert::same($group, $form->getGroup('Address')); |
| 39 | +Assert::true($form->getGroup('Address') instanceof ControlGroup); |
| 40 | +Assert::equal(2, count($group->getControls())); |
| 41 | +Assert::equal([$form['address']['street'], $form['address']['town']], $group->getControls()); |
| 42 | + |
| 43 | + |
| 44 | +// nested containers |
| 45 | +$group = $form->addGroup('Nested'); |
| 46 | +$container = createContainer(); |
| 47 | +$container->addComponent(createContainer(), 'child'); |
| 48 | +$form->addComponent($container, 'parent'); |
| 49 | + |
| 50 | +Assert::equal(4, count($group->getControls())); |
| 51 | +Assert::equal([$form['parent']['street'], $form['parent']['town'], $form['parent']['child']['street'], $form['parent']['child']['town']], $group->getControls()); |
| 52 | + |
| 53 | + |
| 54 | +// container and controls |
| 55 | +$group = $form->addGroup('Mix'); |
| 56 | +$form->addText('foo'); |
| 57 | +$form->addComponent(createContainer(), 'mix'); |
| 58 | +$form->addText('bar'); |
| 59 | + |
| 60 | +Assert::equal(4, count($group->getControls())); |
| 61 | +Assert::equal([$form['foo'], $form['mix']['street'], $form['mix']['town'], $form['bar']], $group->getControls()); |
| 62 | + |
| 63 | + |
| 64 | +// addContainer |
| 65 | +$group = $form->addGroup('Container'); |
| 66 | +$container = $form->addContainer('container'); |
| 67 | +$container->addText('text1'); |
| 68 | +$container->addText('text2'); |
| 69 | + |
| 70 | +Assert::equal(2, count($group->getControls())); |
| 71 | +Assert::equal([$form['container']['text1'], $form['container']['text2']], $group->getControls()); |
| 72 | + |
| 73 | + |
| 74 | +// addContainer recursive |
| 75 | +$group = $form->addGroup('Container'); |
| 76 | +$container1 = $form->addContainer('outer'); |
| 77 | +$container2 = $container1->addContainer('inner'); |
| 78 | +$container2->addText('text'); |
| 79 | + |
| 80 | +Assert::equal(1, count($group->getControls())); |
| 81 | +Assert::equal([$form['outer']['inner']['text']], $group->getControls()); |
| 82 | + |
| 83 | +Assert::equal(6, count($form->getGroups())); |
0 commit comments