From 094e8fdccb0e86f291d04c93e80e5492c96852b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:20:43 +0100 Subject: [PATCH 01/10] Add files via upload --- client-side/dependentSelectBox.js | 16 +- doc/index.md | 4 +- src/Controls/DependentMultiSelectBox.php | 247 +++---- src/Controls/DependentSelectBox.php | 6 +- src/DependentData.php | 287 ++++---- src/DependentExtension.php | 97 +-- src/DependentTrait.php | 308 ++++---- src/Exceptions.php | 60 +- src/_Controls.BackCompatibility.php | 64 +- src/_DI.BackCompatibility.php | 64 +- tests/app/presenters/BasePresenter.php | 512 ++++++------- .../Base.dependentMultiSelect1.latte | 1 + .../Base.dependentMultiSelect2.latte | 1 + .../templates/Base.dependentSelect1.latte | 1 + .../templates/Base.dependentSelect2.latte | 1 + tests/app/router/Router.php | 64 +- tests/bootstrap.php | 48 +- tests/tests/DependentMultiSelectBoxTest.phpt | 691 +++++++++--------- tests/tests/DependentSelectBoxTest.phpt | 685 ++++++++--------- 19 files changed, 1611 insertions(+), 1546 deletions(-) create mode 100644 tests/app/presenters/templates/Base.dependentMultiSelect1.latte create mode 100644 tests/app/presenters/templates/Base.dependentMultiSelect2.latte create mode 100644 tests/app/presenters/templates/Base.dependentSelect1.latte create mode 100644 tests/app/presenters/templates/Base.dependentSelect2.latte diff --git a/client-side/dependentSelectBox.js b/client-side/dependentSelectBox.js index ded41f0..da13339 100644 --- a/client-side/dependentSelectBox.js +++ b/client-side/dependentSelectBox.js @@ -33,23 +33,35 @@ var signalLink = element.data(dsb.settings.dataLinkName); var parents = element.data(dsb.settings.dataParentsName); - if (signalLink == undefined) { + if (signalLink === undefined) { return false; } $.each(parents, function (name, id) { var parentElement = $('#' + id); + if (parentElement.length > 0) { var val; + if (parentElement.prop('type') === 'checkbox') { val = parentElement.prop('checked') ? 1 : 0; + } else { val = $(parentElement).val(); if (!val) { return; } } - signalLink = signalLink + '&' + name + '=' + val; + + + if (val instanceof Array) { + $.each(val, function (key, value) { + signalLink += '&' + name + '[]=' + value; + }); + + } else { + signalLink += '&' + name + '=' + val; + } } }); diff --git a/doc/index.md b/doc/index.md index 10b762a..c51f63b 100644 --- a/doc/index.md +++ b/doc/index.md @@ -2,7 +2,7 @@ DependentSelectBox for [Nette Framework](https://nette.org). This dependent select box support dependence for more form controls , not only for select boxes but also dependence for text input, textarea and more. ## Installation -The best way to install NasExt/DependentSelectBox is using [Composer](http://getcomposer.org/): +The best way to install AlesWita/DropzoneUploader is using [Composer](http://getcomposer.org/): ```sh $ composer require nasext/dependent-select-box ``` @@ -164,4 +164,4 @@ $form->addDependentSelectBox('city', 'City', $form['country']) }) ->setDisabledWhenEmpty(true) ->setPrompt('--- Select ---'); -``` + ``` diff --git a/src/Controls/DependentMultiSelectBox.php b/src/Controls/DependentMultiSelectBox.php index 50a965e..1441391 100644 --- a/src/Controls/DependentMultiSelectBox.php +++ b/src/Controls/DependentMultiSelectBox.php @@ -1,123 +1,124 @@ -parents = $parents; - parent::__construct($label); - } - - - /** - * @throws - * @param bool - * @return self - */ - public function setDisabled($value = true) - { - if (is_array($value)) { - throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); - } - return parent::setDisabled($value); - } - - - /** - * @param string - * @return void - */ - public function signalReceived($signal) - { - $presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); - - if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { - $parentsNames = []; - foreach ($this->parents as $parent) { - $value = $presenter->getParameter($this->getNormalizeName($parent)); - $parent->setValue($value); - - $parentsNames[$parent->getName()] = $parent->getValue(); - } - - $data = $this->getDependentData([$parentsNames]); - $presenter->payload->dependentselectbox = [ - 'id' => $this->getHtmlId(), - 'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), - 'value' => $data->getValue(), - 'prompt' => false, - 'disabledWhenEmpty' => $this->disabledWhenEmpty, - ]; - $presenter->sendPayload(); - } - } - - - /** - * @return void - */ - private function tryLoadItems() - { - if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { - $parentsValues = []; - foreach ($this->parents as $parent) { - $parentsValues[$parent->getName()] = $parent->getValue(); - } - - $data = $this->getDependentData([$parentsValues]); - $items = $data->getItems(); - - - if ($this->getForm()->isSubmitted()) { - $this->setValue($this->value); - - } elseif ($this->tempValue !== null) { - $this->setValue($this->tempValue); - - } else { - $this->setValue($data->getValue()); - } - - - $this->loadHttpData(); - $this->setItems($items); - - if (count($items) === 0) { - if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { - $this->setDisabled(); - } - } - } - } -} + $parents + */ + public function __construct($label, array $parents) + { + $this->parents = $parents; + parent::__construct($label); + } + + + /** + * @throws $value + * @param bool + * @return self + */ + public function setDisabled($value = true) + { + if (is_array($value)) { + throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); + } + + return parent::setDisabled($value); + } + + + /** + * @param string $signal + * @return void + */ + public function signalReceived($signal) + { + $presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); + + if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { + $parentsNames = []; + foreach ($this->parents as $parent) {bdump($presenter->getParameters()); + $value = $presenter->getParameter($this->getNormalizeName($parent)); + $parent->setValue($value); + + $parentsNames[$parent->getName()] = $parent->getValue(); + } + + $data = $this->getDependentData([$parentsNames]); + $presenter->payload->dependentselectbox = [ + 'id' => $this->getHtmlId(), + 'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), + 'value' => $data->getValue(), + 'prompt' => false, + 'disabledWhenEmpty' => $this->disabledWhenEmpty, + ]; + + $presenter->sendPayload(); + } + } + + + /** + * @return void + */ + private function tryLoadItems() + { + if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { + $parentsValues = []; + foreach ($this->parents as $parent) { + $parentsValues[$parent->getName()] = $parent->getValue(); + } + + $data = $this->getDependentData([$parentsValues]); + $items = $data->getItems(); + + if ($this->getForm()->isSubmitted()) { + $this->setValue($this->value); + + } elseif ($this->tempValue !== null) { + $this->setValue($this->tempValue); + + } else { + $this->setValue($data->getValue()); + } + + + $this->loadHttpData(); + $this->setItems($items); + + if (count($items) === 0) { + if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { + $this->setDisabled(); + } + } + } + } +} diff --git a/src/Controls/DependentSelectBox.php b/src/Controls/DependentSelectBox.php index 6f5e29e..7cc83a6 100644 --- a/src/Controls/DependentSelectBox.php +++ b/src/Controls/DependentSelectBox.php @@ -30,8 +30,8 @@ class DependentSelectBox extends Nette\Forms\Controls\SelectBox implements Nette /** - * @param string - * @param array[Nette\Forms\IControl] + * @param string $label + * @param array $parents */ public function __construct($label, array $parents) { @@ -41,7 +41,7 @@ public function __construct($label, array $parents) /** - * @param string + * @param string $signal * @return void */ public function signalReceived($signal) diff --git a/src/DependentData.php b/src/DependentData.php index 4076158..eaef923 100644 --- a/src/DependentData.php +++ b/src/DependentData.php @@ -1,140 +1,147 @@ -items = $items; - $this->value = $value; - $this->prompt = $prompt; - } - - - /** - * @param array - * @return self - */ - public function setItems(array $items) - { - $this->items = $items; - return $this; - } - - - /** - * @param string|int - * @return self - */ - public function setValue($value) - { - $this->value = $value; - return $this; - } - - - /** - * @param string|int - * @return self - */ - public function setPrompt($value) - { - $this->prompt = $value; - return $this; - } - - - /** - * @return array - */ - public function getItems() - { - return $this->items; - } - - - /** - * @param array - * @return array - */ - public function getPreparedItems($disabledItems = null) - { - $items = []; - foreach ($this->items as $key => $item) { - if (!($item instanceof Nette\Utils\Html)) { - $el = Nette\Utils\Html::el('option')->value($key)->setText($item); - } else { - $el = $item; - } - - // disable element - if (is_array($disabledItems) && array_key_exists($key, $disabledItems) && $disabledItems[$key] === true) { - $el->disabled(true); - } - - $items[$key] = [ - 'key' => $el->getValue(), - 'value' => $el->getText(), - ]; - - end($items); - $lKey = key($items); - foreach ($el->attrs as $attr => $val) { - $items[$lKey]['attributes'][$attr] = $val; - } - } - - return $items; - } - - - /** - * @return string|int - */ - public function getValue() - { - return $this->value; - } - - - /** - * @return string - */ - public function getPrompt() - { - return $this->prompt; - } -} +items = $items; + $this->value = $value; + $this->prompt = $prompt; + } + + + /** + * @return array + */ + public function getItems() + { + return $this->items; + } + + + /** + * @param array $items + * @return self + */ + public function setItems(array $items) + { + $this->items = $items; + return $this; + } + + + /** + * @return string|int + */ + public function getValue() + { + return $this->value; + } + + + /** + * @param string|int $value + * @return self + */ + public function setValue($value) + { + $this->value = $value; + return $this; + } + + + /** + * @return string + */ + public function getPrompt() + { + return $this->prompt; + } + + + /** + * @param string $value + * @return self + */ + public function setPrompt($value) + { + $this->prompt = $value; + return $this; + } + + + /** + * @param array $disabledItems + * @return array + */ + public function getPreparedItems($disabledItems = null) + { + $items = []; + foreach ($this->items as $key => $item) { + if (!($item instanceof Nette\Utils\Html)) { + $el = Nette\Utils\Html::el('option')->value($key)->setText($item); + + } else { + $el = $item; + } + + // disable element + if (is_array($disabledItems) && array_key_exists($key, $disabledItems) && $disabledItems[$key] === true) { + $el->disabled(true); + } + + $items[$key] = [ + 'key' => $el->getValue(), + 'value' => $el->getText(), + ]; + + end($items); + $lKey = key($items); + foreach ($el->attrs as $attr => $val) { + $items[$lKey]['attributes'][$attr] = $val; + } + } + + return $items; + } +} diff --git a/src/DependentExtension.php b/src/DependentExtension.php index 43fee54..6a20a58 100644 --- a/src/DependentExtension.php +++ b/src/DependentExtension.php @@ -1,48 +1,49 @@ - - * @author Ales Wita - * @license MIT - */ -class DependentExtension extends Nette\DI\CompilerExtension -{ - /** - * @param Nette\PhpGenerator\ClassType - * @return void - */ - public function afterCompile(Nette\PhpGenerator\ClassType $class) - { - $initialize = $class->getMethod('initialize'); - $initialize->addBody(__CLASS__ . '::registerControls();'); - } - - - /** - * @return void - */ - public static function registerControls() - { - Nette\Forms\Container::extensionMethod('addDependentSelectBox', function (Nette\Forms\Container $container, $name, $label, Nette\Forms\IControl ...$parents) { - return $container[$name] = new NasExt\Forms\Controls\DependentSelectBox($label, $parents); - }); - Nette\Forms\Container::extensionMethod('addDependentMultiSelectBox', function (Nette\Forms\Container $container, $name, $label, Nette\Forms\IControl ...$parents) { - return $container[$name] = new NasExt\Forms\Controls\DependentMultiSelectBox($label, $parents); - }); - } -} + + * @author Ales Wita + * @license MIT + */ +class DependentExtension extends Nette\DI\CompilerExtension +{ + /** + * @param Nette\PhpGenerator\ClassType $class + * @return void + */ + public function afterCompile(Nette\PhpGenerator\ClassType $class) + { + $initialize = $class->getMethod('initialize'); + $initialize->addBody(__CLASS__ . '::registerControls();'); + } + + + /** + * @return void + */ + public static function registerControls() + { + Nette\Forms\Container::extensionMethod('addDependentSelectBox', function (Nette\Forms\Container $container, $name, $label, Nette\Forms\IControl ...$parents) { + return $container[$name] = new NasExt\Forms\Controls\DependentSelectBox($label, $parents); + }); + + Nette\Forms\Container::extensionMethod('addDependentMultiSelectBox', function (Nette\Forms\Container $container, $name, $label, Nette\Forms\IControl ...$parents) { + return $container[$name] = new NasExt\Forms\Controls\DependentMultiSelectBox($label, $parents); + }); + } +} diff --git a/src/DependentTrait.php b/src/DependentTrait.php index b0b923e..81ac805 100644 --- a/src/DependentTrait.php +++ b/src/DependentTrait.php @@ -1,155 +1,153 @@ -tryLoadItems(); - - $attrs = []; - $control = parent::getControl(); - $form = $this->getForm(); - - $parents = []; - foreach ($this->parents as $parent) { - $parents[$this->getNormalizeName($parent)] = $parent->getHtmlId(); - } - - $attrs['data-dependentselectbox-parents'] = Nette\Utils\Json::encode($parents); - $attrs['data-dependentselectbox'] = $form->getPresenter()->link($this->lookupPath('Nette\\Application\\UI\\Presenter') . Nette\ComponentModel\IComponent::NAME_SEPARATOR . self::SIGNAL_NAME . '!'); - - $control->addAttributes($attrs); - return $control; - } - - - /** - * @return string|int - */ - public function getValue() - { - $this->tryLoadItems(); - - if (!in_array($this->tempValue, [null, '', []], true)) { - return $this->tempValue; - } - - return parent::getValue(); - } - - - /** - * @param string|int - * @return self - */ - public function setValue($value) - { - $this->tempValue = $value; - return $this; - } - - - /** - * @param array - * @param bool - * @return self - */ - public function setItems(array $items, $useKeys = true) - { - parent::setItems($items, $useKeys); - - if (!in_array($this->tempValue, [null, '', []], true)) { - parent::setValue($this->tempValue); - } - - return $this; - } - - - /** - * @throws NasExt\Forms\DependentCallbackException - * @param array - * @return NasExt\Forms\DependentData - */ - private function getDependentData(array $args = []) - { - if ($this->dependentCallback === null) { - throw new NasExt\Forms\DependentCallbackException('Dependent callback for "' . $this->getHtmlId() . '" must be set!'); - } - - $dependentData = Nette\Utils\Callback::invokeArgs($this->dependentCallback, $args); - - if (!($dependentData instanceof NasExt\Forms\DependentData) && !($dependentData instanceof NasExt\Forms\Controls\DependentSelectBoxData)) { - throw new NasExt\Forms\DependentCallbackException('Callback for "' . $this->getHtmlId() . '" must return NasExt\\Forms\\DependentData instance!'); - } - - return $dependentData; - } - - - /** - * @param callable - * @return self - */ - public function setDependentCallback(callable $callback) - { - $this->dependentCallback = $callback; - return $this; - } - - - /** - * @param bool - * @return self - */ - public function setDisabledWhenEmpty($value = true) - { - $this->disabledWhenEmpty = $value; - return $this; - } - - - /** - * @param Nette\Forms\Controls\BaseControl - * @return string - */ - protected function getNormalizeName(Nette\Forms\Controls\BaseControl $parent) - { - return str_replace('-', '_', $parent->getHtmlId()); - } -} +tryLoadItems(); + + $attrs = []; + $control = parent::getControl(); + $form = $this->getForm(); + + $parents = []; + foreach ($this->parents as $parent) { + $parents[$this->getNormalizeName($parent)] = $parent->getHtmlId(); + } + + $attrs['data-dependentselectbox-parents'] = Nette\Utils\Json::encode($parents); + $attrs['data-dependentselectbox'] = $form->getPresenter()->link($this->lookupPath('Nette\\Application\\UI\\Presenter') . Nette\ComponentModel\IComponent::NAME_SEPARATOR . self::SIGNAL_NAME . '!'); + + $control->addAttributes($attrs); + return $control; + } + + + /** + * @return string|int + */ + public function getValue() + { + $this->tryLoadItems(); + + if (!in_array($this->tempValue, [null, '', []], true)) { + return $this->tempValue; + } + + return parent::getValue(); + } + + + /** + * @param string|int $value + * @return self + */ + public function setValue($value) + { + $this->tempValue = $value; + return $this; + } + + + /** + * @param array $items + * @param bool $useKeys + * @return self + */ + public function setItems(array $items, $useKeys = true) + { + parent::setItems($items, $useKeys); + + if (!in_array($this->tempValue, [null, '', []], true)) { + parent::setValue($this->tempValue); + } + + return $this; + } + + + /** + * @param array $args + * @return NasExt\Forms\DependentData + * @throws NasExt\Forms\DependentCallbackException + */ + private function getDependentData(array $args = []) + { + if ($this->dependentCallback === null) { + throw new NasExt\Forms\DependentCallbackException('Dependent callback for "' . $this->getHtmlId() . '" must be set!'); + } + + $dependentData = Nette\Utils\Callback::invokeArgs($this->dependentCallback, $args); + + if (!($dependentData instanceof NasExt\Forms\DependentData) && !($dependentData instanceof NasExt\Forms\Controls\DependentSelectBoxData)) { + throw new NasExt\Forms\DependentCallbackException('Callback for "' . $this->getHtmlId() . '" must return NasExt\\Forms\\DependentData instance!'); + } + + return $dependentData; + } + + + /** + * @param callable $callback + * @return self + */ + public function setDependentCallback(callable $callback) + { + $this->dependentCallback = $callback; + return $this; + } + + + /** + * @param bool $value + * @return self + */ + public function setDisabledWhenEmpty($value = true) + { + $this->disabledWhenEmpty = $value; + return $this; + } + + /** + * @param Nette\Forms\Controls\BaseControl $parent + * @return string + */ + private function getNormalizeName(Nette\Forms\Controls\BaseControl $parent) { + return str_replace('-', '_', $parent->getHtmlId()); + } +} diff --git a/src/Exceptions.php b/src/Exceptions.php index 3ac9e2b..027efb8 100644 --- a/src/Exceptions.php +++ b/src/Exceptions.php @@ -1,30 +1,30 @@ -setView('dependentSelect2'); - } - - - /** - * @return void - */ - public function actionDependentSelect2Exception2() - { - $this['dependentSelectForm2']['dependentSelect']->setDependentCallback(function () {}); - $this->setView('dependentSelect2'); - } - - - /** - * @return void - */ - public function actionDependentSelect2Disabled1() - { - $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabled(); - - $this->setView('dependentSelect2'); - } - - - /** - * @return void - */ - public function actionDependentSelect2Disabled2() - { - $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabled([2]); - - $this->setView('dependentSelect2'); - } - - - /** - * @return void - */ - public function actionDependentSelect2Disabled3() - { - $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabledWhenEmpty(); - - $this->setView('dependentSelect2'); - } - - - /** - * @return Nette\Application\UI\Form - */ - protected function createComponentDependentSelectForm1() - { - $form = new Nette\Application\UI\Form; - - $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second']) - ->setPrompt('---'); - - $form->addDependentSelectBox('dependentSelect', 'Dependent select', $form['select']) - ->setDependentCallback([$this, 'dependentCallback']) - ->setPrompt('Select select first'); - - return $form; - } - - - /** - * @return Nette\Application\UI\Form - */ - protected function createComponentDependentSelectForm2() - { - $form = new Nette\Application\UI\Form; - - $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second', 3 => 'Third']) - ->setPrompt('---'); - - $form->addDependentSelectBox('dependentSelect', 'Dependent select', $form['select']) - ->setPrompt('Select select first'); - - return $form; - } - - - /** - * @return void - */ - public function actionDependentMultiSelect1() - { - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2() - { - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2Exception1() - { - $this->setView('dependentMultiSelect2'); - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2Exception2() - { - $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback(function () {}); - $this->setView('dependentMultiSelect2'); - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2Disabled1() - { - $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabled(); - - $this->setView('dependentMultiSelect2'); - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2Disabled2() - { - $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabled([2]); - - $this->setView('dependentMultiSelect2'); - } - - - /** - * @return void - */ - public function actionDependentMultiSelect2Disabled3() - { - $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) - ->setDisabledWhenEmpty(); - - $this->setView('dependentMultiSelect2'); - } - - - /** - * @return Nette\Application\UI\Form - */ - protected function createComponentDependentMultiSelectForm1() - { - $form = new Nette\Application\UI\Form; - - $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second']) - ->setPrompt('---'); - - $form->addDependentMultiSelectBox('dependentMultiSelect', 'Dependent multi select', $form['select']) - ->setDependentCallback([$this, 'dependentCallback']); - - return $form; - } - - - /** - * @return Nette\Application\UI\Form - */ - protected function createComponentDependentMultiSelectForm2() - { - $form = new Nette\Application\UI\Form; - - $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second', 3 => 'Third']) - ->setPrompt('---'); - - $form->addDependentMultiSelectBox('dependentMultiSelect', 'Dependent multi select', $form['select']); - - return $form; - } - - - /** - * @param array - * @return NasExt\Forms\DependentData - */ - public function dependentCallback(array $values) - { - $data = new NasExt\Forms\DependentData; - - switch ($values['select']) { - case 1: - $data->setItems([1 => 'First', 2 => 'Still first']) - ->setPrompt('---'); - break; - - case 2: - $data->setItems([3 => 'Second', 4 => 'Still second']) - ->setPrompt('---'); - break; - } - - return $data; - } -} +setView('dependentSelect2'); + } + + + /** + * @return void + */ + public function actionDependentSelect2Exception2() + { + $this['dependentSelectForm2']['dependentSelect']->setDependentCallback(function () {}); + $this->setView('dependentSelect2'); + } + + + /** + * @return void + */ + public function actionDependentSelect2Disabled1() + { + $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabled(); + + $this->setView('dependentSelect2'); + } + + + /** + * @return void + */ + public function actionDependentSelect2Disabled2() + { + $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabled([2]); + + $this->setView('dependentSelect2'); + } + + + /** + * @return void + */ + public function actionDependentSelect2Disabled3() + { + $this['dependentSelectForm2']['dependentSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabledWhenEmpty(); + + $this->setView('dependentSelect2'); + } + + + /** + * @return Nette\Application\UI\Form + */ + protected function createComponentDependentSelectForm1() + { + $form = new Nette\Application\UI\Form; + + $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second']) + ->setPrompt('---'); + + $form->addDependentSelectBox('dependentSelect', 'Dependent select', $form['select']) + ->setDependentCallback([$this, 'dependentCallback']) + ->setPrompt('Select select first'); + + return $form; + } + + + /** + * @return Nette\Application\UI\Form + */ + protected function createComponentDependentSelectForm2() + { + $form = new Nette\Application\UI\Form; + + $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second', 3 => 'Third']) + ->setPrompt('---'); + + $form->addDependentSelectBox('dependentSelect', 'Dependent select', $form['select']) + ->setPrompt('Select select first'); + + return $form; + } + + + /** + * @return void + */ + public function actionDependentMultiSelect1() + { + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2() + { + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2Exception1() + { + $this->setView('dependentMultiSelect2'); + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2Exception2() + { + $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback(function () {}); + $this->setView('dependentMultiSelect2'); + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2Disabled1() + { + $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabled(); + + $this->setView('dependentMultiSelect2'); + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2Disabled2() + { + $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabled([2]); + + $this->setView('dependentMultiSelect2'); + } + + + /** + * @return void + */ + public function actionDependentMultiSelect2Disabled3() + { + $this['dependentMultiSelectForm2']['dependentMultiSelect']->setDependentCallback([$this, 'dependentCallback']) + ->setDisabledWhenEmpty(); + + $this->setView('dependentMultiSelect2'); + } + + + /** + * @return Nette\Application\UI\Form + */ + protected function createComponentDependentMultiSelectForm1() + { + $form = new Nette\Application\UI\Form; + + $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second']) + ->setPrompt('---'); + + $form->addDependentMultiSelectBox('dependentMultiSelect', 'Dependent multi select', $form['select']) + ->setDependentCallback([$this, 'dependentCallback']); + + return $form; + } + + + /** + * @return Nette\Application\UI\Form + */ + protected function createComponentDependentMultiSelectForm2() + { + $form = new Nette\Application\UI\Form; + + $form->addSelect('select', 'Select', [1 => 'First', 2 => 'Second', 3 => 'Third']) + ->setPrompt('---'); + + $form->addDependentMultiSelectBox('dependentMultiSelect', 'Dependent multi select', $form['select']); + + return $form; + } + + + /** + * @param array $values + * @return NasExt\Forms\DependentData + */ + public function dependentCallback(array $values) + { + $data = new NasExt\Forms\DependentData; + + switch ($values['select']) { + case 1: + $data->setItems([1 => 'First', 2 => 'Still first']) + ->setPrompt('---'); + break; + + case 2: + $data->setItems([3 => 'Second', 4 => 'Still second']) + ->setPrompt('---'); + break; + } + + return $data; + } +} diff --git a/tests/app/presenters/templates/Base.dependentMultiSelect1.latte b/tests/app/presenters/templates/Base.dependentMultiSelect1.latte new file mode 100644 index 0000000..eab2637 --- /dev/null +++ b/tests/app/presenters/templates/Base.dependentMultiSelect1.latte @@ -0,0 +1 @@ +{control dependentMultiSelectForm1} diff --git a/tests/app/presenters/templates/Base.dependentMultiSelect2.latte b/tests/app/presenters/templates/Base.dependentMultiSelect2.latte new file mode 100644 index 0000000..06fddaf --- /dev/null +++ b/tests/app/presenters/templates/Base.dependentMultiSelect2.latte @@ -0,0 +1 @@ +{control dependentMultiSelectForm2} diff --git a/tests/app/presenters/templates/Base.dependentSelect1.latte b/tests/app/presenters/templates/Base.dependentSelect1.latte new file mode 100644 index 0000000..d0096ac --- /dev/null +++ b/tests/app/presenters/templates/Base.dependentSelect1.latte @@ -0,0 +1 @@ +{control dependentSelectForm1} diff --git a/tests/app/presenters/templates/Base.dependentSelect2.latte b/tests/app/presenters/templates/Base.dependentSelect2.latte new file mode 100644 index 0000000..fb973d3 --- /dev/null +++ b/tests/app/presenters/templates/Base.dependentSelect2.latte @@ -0,0 +1 @@ +{control dependentSelectForm2} diff --git a/tests/app/router/Router.php b/tests/app/router/Router.php index e651df7..a7f707f 100644 --- a/tests/app/router/Router.php +++ b/tests/app/router/Router.php @@ -1,32 +1,32 @@ -/[/]', 'Base:default'); - return $route; - } -} +/[/]', 'Base:default'); + return $route; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d967055..26deaa9 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,24 +1,24 @@ -setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - - // check form - $form = $presenter['dependentMultiSelectForm1']; - - Tester\Assert::true($form instanceof Nette\Application\UI\Form); - - - // check dependent multi select - $dependentMultiSelect = $form['dependentMultiSelect']; - - Tester\Assert::true($dependentMultiSelect instanceof NasExt\Forms\Controls\DependentMultiSelectBox); - - - // check control - $control = $dependentMultiSelect->getControl(); - - Tester\Assert::true($control instanceof Nette\Utils\Html); - - - // check source - $source = (string) $response->getSource(); - $dom = Tester\DomQuery::fromHtml($source); - - - // dependent select tag - $data = $dom->find('select[name="dependentMultiSelect[]"]'); - - Tester\Assert::count(1, $data); - - $foo = (array) $data[0]; - Tester\Assert::count(5, $foo['@attributes']); - Tester\Assert::same($control->getAttribute('name'), $foo['@attributes']['name']); - Tester\Assert::same($control->getAttribute('id'), $foo['@attributes']['id']); - Tester\Assert::same('multiple', $foo['@attributes']['multiple']); - Tester\Assert::same($control->getAttribute('data-dependentselectbox'), $foo['@attributes']['data-dependentselectbox']); - Tester\Assert::same($control->getAttribute('data-dependentselectbox-parents'), $foo['@attributes']['data-dependentselectbox-parents']); - } - - - /** - * @return void - */ - public function testTwo() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit'], ['select' => 1, 'dependentMultiSelect' => [1]]); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - - // check multi dependent select - $dependentMultiSelect = $presenter['dependentMultiSelectForm1']['dependentMultiSelect']; - - Tester\Assert::true($dependentMultiSelect instanceof NasExt\Forms\Controls\DependentMultiSelectBox); - Tester\Assert::same([1], $dependentMultiSelect->getValue()); - } - - - /** - * @throws Nette\InvalidArgumentException Values '3', '4' are out of allowed set [1, 2] in field 'dependentMultiSelect'. - * @return void - */ - public function testThree() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit'], ['select' => 1, 'dependentMultiSelect' => [3, 4]]); - $response = $presenter->run($request); - - $presenter['dependentMultiSelectForm1']->getValues();// must load values for throws exception - } - - - /** - * @return void - */ - public function testFour() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit']); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentMultiSelectForm1']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::same(['select' => null, 'dependentMultiSelect' => []], (array) $form->getValues()); - } - - - /** - * @return void - */ - public function testFive() - { - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';// make ajax request - - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'frm_dependentMultiSelectForm1_select' => 1]); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); - Tester\Assert::same([ - 'id' => 'frm-dependentMultiSelectForm1-dependentMultiSelect', - 'items' => [ - 1 => ['key' => 1, 'value' => 'First', 'attributes' => ['value' => 1]], - 2 => ['key' => 2, 'value' => 'Still first', 'attributes' => ['value' => 2]], - ], - 'value' => null, - 'prompt' => false, - 'disabledWhenEmpty' => null, - ], $response->getPayload()->dependentselectbox); - } - - - /** - * @throws NasExt\Forms\DependentCallbackException Dependent callback for "frm-dependentMultiSelectForm2-dependentMultiSelect" must be set! - * @return void - */ - public function testSix() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect2Exception1']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - $response->getSource()->render(); - } - - - /** - * @throws NasExt\Forms\DependentCallbackException Callback for "frm-dependentMultiSelectForm2-dependentMultiSelect" must return NasExt\Forms\DependentData instance! - * @return void - */ - public function testSeven() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect2Exception2']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - $response->getSource()->render(); - } - - - /** - * @return void - */ - public function testEight() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled1'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 1, 'dependentMultiSelect' => [2]]); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentMultiSelectForm2']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::same(['select' => 1], (array) $form->getValues()); - } - - - /** - * @throws Nette\InvalidArgumentException NasExt\Forms\Controls\DependentMultiSelectBox not supported disabled items! - * @return void - */ - public function testNine() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled2'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 1, 'dependentMultiSelect' => [2]]); - $response = $presenter->run($request); - - - // check form - //$form = $presenter['dependentMultiSelectForm2']; - - //Tester\Assert::true($form->isSubmitted()); - //Tester\Assert::true($form->isSuccess()); - //Tester\Assert::same(['select' => 1, 'dependentMultiSelect' => []], (array) $form->getValues()); - } - - - /** - * @return void - */ - public function testTen() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled3'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 3]); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentMultiSelectForm2']; - $form->getValues();// must load values for check if omitted - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::true($form['dependentMultiSelect']->isOmitted()); - Tester\Assert::same(['select' => 3], (array) $form->getValues()); - } -} - - -$test = new DependentMultiSelectBoxTest; -$test->run(); +setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + + // check form + $form = $presenter['dependentMultiSelectForm1']; + + Tester\Assert::true($form instanceof Nette\Application\UI\Form); + + + // check dependent multi select + $dependentMultiSelect = $form['dependentMultiSelect']; + + Tester\Assert::true($dependentMultiSelect instanceof NasExt\Forms\Controls\DependentMultiSelectBox); + + + // check control + $control = $dependentMultiSelect->getControl(); + + Tester\Assert::true($control instanceof Nette\Utils\Html); + + + // check source + $source = (string) $response->getSource(); + $dom = Tester\DomQuery::fromHtml($source); + + + // dependent select tag + $data = $dom->find('select[name="dependentMultiSelect[]"]'); + + Tester\Assert::count(1, $data); + + $foo = (array) $data[0]; + Tester\Assert::count(5, $foo['@attributes']); + Tester\Assert::same($control->getAttribute('name'), $foo['@attributes']['name']); + Tester\Assert::same($control->getAttribute('id'), $foo['@attributes']['id']); + Tester\Assert::same('multiple', $foo['@attributes']['multiple']); + Tester\Assert::same($control->getAttribute('data-dependentselectbox'), $foo['@attributes']['data-dependentselectbox']); + Tester\Assert::same($control->getAttribute('data-dependentselectbox-parents'), $foo['@attributes']['data-dependentselectbox-parents']); + } + + + /** + * @return void + */ + public function testTwo() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit'], ['select' => 1, 'dependentMultiSelect' => [1]]); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + + // check multi dependent select + $dependentMultiSelect = $presenter['dependentMultiSelectForm1']['dependentMultiSelect']; + + Tester\Assert::true($dependentMultiSelect instanceof NasExt\Forms\Controls\DependentMultiSelectBox); + Tester\Assert::same([1], $dependentMultiSelect->getValue()); + } + + + /** + * @throws Nette\InvalidArgumentException Values '3', '4' are out of allowed set [1, 2] in field 'dependentMultiSelect'. + * @return void + */ + public function testThree() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit'], ['select' => 1, 'dependentMultiSelect' => [3, 4]]); + $response = $presenter->run($request); + + $presenter['dependentMultiSelectForm1']->getValues();// must load values for throws exception + } + + + /** + * @return void + */ + public function testFour() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect1'], ['_do' => 'dependentMultiSelectForm1-submit']); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentMultiSelectForm1']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::same(['select' => null, 'dependentMultiSelect' => []], (array) $form->getValues()); + } + + + /** + * @return void + */ + public function testFive() + { + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';// make ajax request + + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'select' => 1]); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); + Tester\Assert::same([ + 'id' => 'frm-dependentMultiSelectForm1-dependentMultiSelect', + 'items' => [ + 1 => ['key' => 1, 'value' => 'First', 'attributes' => ['value' => 1]], + 2 => ['key' => 2, 'value' => 'Still first', 'attributes' => ['value' => 2]], + ], + 'value' => null, + 'prompt' => false, + 'disabledWhenEmpty' => null, + ], $response->getPayload()->dependentselectbox); + } + + + /** + * @throws NasExt\Forms\DependentCallbackException Dependent callback for "frm-dependentMultiSelectForm2-dependentMultiSelect" must be set! + * @return void + */ + public function testSix() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect2Exception1']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + $response->getSource()->render(); + } + + + /** + * @throws NasExt\Forms\DependentCallbackException Callback for "frm-dependentMultiSelectForm2-dependentMultiSelect" must return NasExt\Forms\DependentData instance! + * @return void + */ + public function testSeven() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect2Exception2']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + $response->getSource()->render(); + } + + + /** + * @return void + */ + public function testEight() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled1'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 1, 'dependentMultiSelect' => [2]]); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentMultiSelectForm2']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::same(['select' => 1], (array) $form->getValues()); + } + + + /** + * @throws Nette\InvalidArgumentException NasExt\Forms\Controls\DependentMultiSelectBox not supported disabled items! + * @return void + */ + public function testNine() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled2'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 1, 'dependentMultiSelect' => [2]]); + $response = $presenter->run($request); + + + // check form + //$form = $presenter['dependentMultiSelectForm2']; + + //Tester\Assert::true($form->isSubmitted()); + //Tester\Assert::true($form->isSuccess()); + //Tester\Assert::same(['select' => 1, 'dependentMultiSelect' => []], (array) $form->getValues()); + } + + + /** + * @return void + */ + public function testTen() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentMultiSelect2Disabled3'], ['_do' => 'dependentMultiSelectForm2-submit'], ['select' => 3]); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentMultiSelectForm2']; + $form->getValues();// must load values for check if omitted + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::true($form['dependentMultiSelect']->isOmitted()); + Tester\Assert::same(['select' => 3], (array) $form->getValues()); + } + + + /** + * @internal + */ + private function getConfig() + { + return Tester\FileMock::create(' +extensions: + dependentSelectBox: NasExt\Forms\DependentExtension + +application: + mapping: + *: NasExt\Forms\Tests\App\Presenters\*Presenter + +services: + base.presenter: + class: NasExt\Forms\Tests\App\Presenters\BasePresenter + + routing.router: NasExt\Forms\Tests\App\Router\Router::createRouter', 'neon'); + } +} + + +$test = new DependentMultiSelectBoxTest; +$test->run(); diff --git a/tests/tests/DependentSelectBoxTest.phpt b/tests/tests/DependentSelectBoxTest.phpt index e608602..65b7ef3 100644 --- a/tests/tests/DependentSelectBoxTest.phpt +++ b/tests/tests/DependentSelectBoxTest.phpt @@ -1,332 +1,353 @@ -setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - - // check form - $form = $presenter['dependentSelectForm1']; - - Tester\Assert::true($form instanceof Nette\Application\UI\Form); - - - // check dependent select - $dependentSelect = $form['dependentSelect']; - - Tester\Assert::true($dependentSelect instanceof NasExt\Forms\Controls\DependentSelectBox); - - - // check control - $control = $dependentSelect->getControl(); - - Tester\Assert::true($control instanceof Nette\Utils\Html); - - - // check source - $source = (string) $response->getSource(); - $dom = Tester\DomQuery::fromHtml($source); - - - // dependent select tag - $data = $dom->find('select[name="dependentSelect"]'); - - Tester\Assert::count(1, $data); - - $foo = (array) $data[0]; - Tester\Assert::count(4, $foo['@attributes']); - Tester\Assert::same($control->getAttribute('name'), $foo['@attributes']['name']); - Tester\Assert::same($control->getAttribute('id'), $foo['@attributes']['id']); - Tester\Assert::same($control->getAttribute('data-dependentselectbox'), $foo['@attributes']['data-dependentselectbox']); - Tester\Assert::same($control->getAttribute('data-dependentselectbox-parents'), $foo['@attributes']['data-dependentselectbox-parents']); - - Tester\Assert::same('Select select first', $foo['option']); - } - - - /** - * @return void - */ - public function testTwo() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit'], ['select' => 1, 'dependentSelect' => 1]); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - - // check dependent select - $dependentSelect = $presenter['dependentSelectForm1']['dependentSelect']; - - Tester\Assert::true($dependentSelect instanceof NasExt\Forms\Controls\DependentSelectBox); - Tester\Assert::same(1, $dependentSelect->getValue()); - } - - - /** - * @throws Nette\InvalidArgumentException Value '3' is out of allowed set [1, 2] in field 'dependentSelect'. - * @return void - */ - public function testThree() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit'], ['select' => 1, 'dependentSelect' => 3]); - $response = $presenter->run($request); - } - - - /** - * @return void - */ - public function testFour() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit']); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentSelectForm1']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::same(['select' => null, 'dependentSelect' => null], (array) $form->getValues()); - } - - - /** - * @return void - */ - public function testFive() - { - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';// make ajax request - - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'frm_dependentSelectForm1_select' => 1]); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); - Tester\Assert::same([ - 'id' => 'frm-dependentSelectForm1-dependentSelect', - 'items' => [ - 1 => ['key' => 1, 'value' => 'First', 'attributes' => ['value' => 1]], - 2 => ['key' => 2, 'value' => 'Still first', 'attributes' => ['value' => 2]], - ], - 'value' => null, - 'prompt' => '---', - 'disabledWhenEmpty' => null, - ], $response->getPayload()->dependentselectbox); - } - - - /** - * @throws NasExt\Forms\DependentCallbackException Dependent callback for "frm-dependentSelectForm2-dependentSelect" must be set! - * @return void - */ - public function testSix() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect2Exception1']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - $response->getSource()->render(); - } - - - /** - * @throws NasExt\Forms\DependentCallbackException Callback for "frm-dependentSelectForm2-dependentSelect" must return NasExt\Forms\DependentData instance! - * @return void - */ - public function testSeven() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect2Exception2']); - $response = $presenter->run($request); - - Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); - Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); - - $response->getSource()->render(); - } - - - /** - * @return void - */ - public function testEight() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled1'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 1, 'dependentSelect' => 2]); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentSelectForm2']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::same(['select' => 1], (array) $form->getValues()); - } - - - /** - * @return void - */ - public function testNine() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled2'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 1, 'dependentSelect' => 2]); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentSelectForm2']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::same(['select' => 1, 'dependentSelect' => null], (array) $form->getValues()); - } - - - /** - * @return void - */ - public function testTen() - { - $configurator = new Nette\Configurator(); - $configurator->setTempDirectory(TEMP_DIR); - $configurator->addConfig(__DIR__ . '/../app/config/config.neon'); - - $container = $configurator->createContainer(); - $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); - - $presenter = $presenterFactory->createPresenter('Base'); - $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled3'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 3]); - $response = $presenter->run($request); - - - // check form - $form = $presenter['dependentSelectForm2']; - - Tester\Assert::true($form->isSubmitted()); - Tester\Assert::true($form->isSuccess()); - Tester\Assert::true($form['dependentSelect']->isOmitted()); - Tester\Assert::same(['select' => 3], (array) $form->getValues()); - } -} - - -$test = new DependentSelectBoxTest; -$test->run(); +setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + + // check form + $form = $presenter['dependentSelectForm1']; + + Tester\Assert::true($form instanceof Nette\Application\UI\Form); + + + // check dependent select + $dependentSelect = $form['dependentSelect']; + + Tester\Assert::true($dependentSelect instanceof NasExt\Forms\Controls\DependentSelectBox); + + + // check control + $control = $dependentSelect->getControl(); + + Tester\Assert::true($control instanceof Nette\Utils\Html); + + + // check source + $source = (string) $response->getSource(); + $dom = Tester\DomQuery::fromHtml($source); + + + // dependent select tag + $data = $dom->find('select[name="dependentSelect"]'); + + Tester\Assert::count(1, $data); + + $foo = (array) $data[0]; + Tester\Assert::count(4, $foo['@attributes']); + Tester\Assert::same($control->getAttribute('name'), $foo['@attributes']['name']); + Tester\Assert::same($control->getAttribute('id'), $foo['@attributes']['id']); + Tester\Assert::same($control->getAttribute('data-dependentselectbox'), $foo['@attributes']['data-dependentselectbox']); + Tester\Assert::same($control->getAttribute('data-dependentselectbox-parents'), $foo['@attributes']['data-dependentselectbox-parents']); + + Tester\Assert::same('Select select first', $foo['option']); + } + + + /** + * @return void + */ + public function testTwo() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit'], ['select' => 1, 'dependentSelect' => 1]); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + + // check dependent select + $dependentSelect = $presenter['dependentSelectForm1']['dependentSelect']; + + Tester\Assert::true($dependentSelect instanceof NasExt\Forms\Controls\DependentSelectBox); + Tester\Assert::same(1, $dependentSelect->getValue()); + } + + + /** + * @throws Nette\InvalidArgumentException Value '3' is out of allowed set [1, 2] in field 'dependentSelect'. + * @return void + */ + public function testThree() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit'], ['select' => 1, 'dependentSelect' => 3]); + $response = $presenter->run($request); + } + + + /** + * @return void + */ + public function testFour() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect1'], ['_do' => 'dependentSelectForm1-submit']); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentSelectForm1']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::same(['select' => null, 'dependentSelect' => null], (array) $form->getValues()); + } + + + /** + * @return void + */ + public function testFive() + { + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';// make ajax request + + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'select' => 1]); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); + Tester\Assert::same([ + 'id' => 'frm-dependentSelectForm1-dependentSelect', + 'items' => [ + 1 => ['key' => 1, 'value' => 'First', 'attributes' => ['value' => 1]], + 2 => ['key' => 2, 'value' => 'Still first', 'attributes' => ['value' => 2]], + ], + 'value' => null, + 'prompt' => '---', + 'disabledWhenEmpty' => null, + ], $response->getPayload()->dependentselectbox); + } + + + /** + * @throws NasExt\Forms\DependentCallbackException Dependent callback for "frm-dependentSelectForm2-dependentSelect" must be set! + * @return void + */ + public function testSix() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect2Exception1']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + $response->getSource()->render(); + } + + + /** + * @throws NasExt\Forms\DependentCallbackException Callback for "frm-dependentSelectForm2-dependentSelect" must return NasExt\Forms\DependentData instance! + * @return void + */ + public function testSeven() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect2Exception2']); + $response = $presenter->run($request); + + Tester\Assert::true($response instanceof Nette\Application\Responses\TextResponse); + Tester\Assert::true($response->getSource() instanceof Nette\Application\UI\ITemplate); + + $response->getSource()->render(); + } + + + /** + * @return void + */ + public function testEight() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled1'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 1, 'dependentSelect' => 2]); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentSelectForm2']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::same(['select' => 1], (array) $form->getValues()); + } + + + /** + * @return void + */ + public function testNine() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled2'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 1, 'dependentSelect' => 2]); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentSelectForm2']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::same(['select' => 1, 'dependentSelect' => null], (array) $form->getValues()); + } + + + /** + * @return void + */ + public function testTen() + { + $configurator = new Nette\Configurator(); + $configurator->setTempDirectory(TEMP_DIR); + $configurator->addConfig($this->getConfig()); + + $container = $configurator->createContainer(); + $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory'); + + $presenter = $presenterFactory->createPresenter('Base'); + $presenter->autoCanonicalize = false; + $request = new Nette\Application\Request('Base', 'POST', ['action' => 'dependentSelect2Disabled3'], ['_do' => 'dependentSelectForm2-submit'], ['select' => 3]); + $response = $presenter->run($request); + + + // check form + $form = $presenter['dependentSelectForm2']; + + Tester\Assert::true($form->isSubmitted()); + Tester\Assert::true($form->isSuccess()); + Tester\Assert::true($form['dependentSelect']->isOmitted()); + Tester\Assert::same(['select' => 3], (array) $form->getValues()); + } + + + /** + * @internal + */ + private function getConfig() + { + return Tester\FileMock::create(' +extensions: + dependentSelectBox: NasExt\Forms\DependentExtension + +application: + mapping: + *: NasExt\Forms\Tests\App\Presenters\*Presenter + +services: + base.presenter: + class: NasExt\Forms\Tests\App\Presenters\BasePresenter + + routing.router: NasExt\Forms\Tests\App\Router\Router::createRouter', 'neon'); + } +} + + +$test = new DependentSelectBoxTest; +$test->run(); From 2c9e7cb56bbef65fdbe430dcf476992daf11f0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:21:26 +0100 Subject: [PATCH 02/10] Delete config.neon --- tests/app/config/config.neon | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 tests/app/config/config.neon diff --git a/tests/app/config/config.neon b/tests/app/config/config.neon deleted file mode 100644 index f1628fa..0000000 --- a/tests/app/config/config.neon +++ /dev/null @@ -1,12 +0,0 @@ -extensions: - dependentSelectBox: NasExt\Forms\DependentExtension - -application: - mapping: - *: NasExt\Forms\Tests\App\Presenters\*Presenter - -services: - base.presenter: - class: NasExt\Forms\Tests\App\Presenters\BasePresenter - - routing.router: NasExt\Forms\Tests\App\Router\Router::createRouter From 005f5cff163e34e92e1f59c624af97799b3c0bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:21:40 +0100 Subject: [PATCH 03/10] Delete dependentMultiSelect1.latte --- tests/app/presenters/templates/Base/dependentMultiSelect1.latte | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/app/presenters/templates/Base/dependentMultiSelect1.latte diff --git a/tests/app/presenters/templates/Base/dependentMultiSelect1.latte b/tests/app/presenters/templates/Base/dependentMultiSelect1.latte deleted file mode 100644 index e5283c9..0000000 --- a/tests/app/presenters/templates/Base/dependentMultiSelect1.latte +++ /dev/null @@ -1 +0,0 @@ -{control dependentMultiSelectForm1} From cb9120e26cf476758cbb7be5431429ae936fa665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:21:46 +0100 Subject: [PATCH 04/10] Delete dependentMultiSelect2.latte --- tests/app/presenters/templates/Base/dependentMultiSelect2.latte | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/app/presenters/templates/Base/dependentMultiSelect2.latte diff --git a/tests/app/presenters/templates/Base/dependentMultiSelect2.latte b/tests/app/presenters/templates/Base/dependentMultiSelect2.latte deleted file mode 100644 index 01f5eea..0000000 --- a/tests/app/presenters/templates/Base/dependentMultiSelect2.latte +++ /dev/null @@ -1 +0,0 @@ -{control dependentMultiSelectForm2} From e972da2b92b5b77672f366ab5b5667e220b0bc1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:21:51 +0100 Subject: [PATCH 05/10] Delete dependentSelect1.latte --- tests/app/presenters/templates/Base/dependentSelect1.latte | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/app/presenters/templates/Base/dependentSelect1.latte diff --git a/tests/app/presenters/templates/Base/dependentSelect1.latte b/tests/app/presenters/templates/Base/dependentSelect1.latte deleted file mode 100644 index 23030d8..0000000 --- a/tests/app/presenters/templates/Base/dependentSelect1.latte +++ /dev/null @@ -1 +0,0 @@ -{control dependentSelectForm1} From bada6c5f2164f75d1669300536e8f3d7b6f61097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:21:56 +0100 Subject: [PATCH 06/10] Delete dependentSelect2.latte --- tests/app/presenters/templates/Base/dependentSelect2.latte | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/app/presenters/templates/Base/dependentSelect2.latte diff --git a/tests/app/presenters/templates/Base/dependentSelect2.latte b/tests/app/presenters/templates/Base/dependentSelect2.latte deleted file mode 100644 index 2650953..0000000 --- a/tests/app/presenters/templates/Base/dependentSelect2.latte +++ /dev/null @@ -1 +0,0 @@ -{control dependentSelectForm2} From 8d7bd7bc89fd5b3cdfdbd79fba56638ffde9bc3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:35:26 +0100 Subject: [PATCH 07/10] Update index.md --- doc/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/index.md b/doc/index.md index c51f63b..10b762a 100644 --- a/doc/index.md +++ b/doc/index.md @@ -2,7 +2,7 @@ DependentSelectBox for [Nette Framework](https://nette.org). This dependent select box support dependence for more form controls , not only for select boxes but also dependence for text input, textarea and more. ## Installation -The best way to install AlesWita/DropzoneUploader is using [Composer](http://getcomposer.org/): +The best way to install NasExt/DependentSelectBox is using [Composer](http://getcomposer.org/): ```sh $ composer require nasext/dependent-select-box ``` @@ -164,4 +164,4 @@ $form->addDependentSelectBox('city', 'City', $form['country']) }) ->setDisabledWhenEmpty(true) ->setPrompt('--- Select ---'); - ``` +``` From a533d8661265194099290dda6b548a11ab90bfaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:40:29 +0100 Subject: [PATCH 08/10] Add files via upload --- tests/tests/DependentMultiSelectBoxTest.phpt | 2 +- tests/tests/DependentSelectBoxTest.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/DependentMultiSelectBoxTest.phpt b/tests/tests/DependentMultiSelectBoxTest.phpt index 33fe046..5c9fb8f 100644 --- a/tests/tests/DependentMultiSelectBoxTest.phpt +++ b/tests/tests/DependentMultiSelectBoxTest.phpt @@ -179,7 +179,7 @@ final class DependentMultiSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', '_do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); diff --git a/tests/tests/DependentSelectBoxTest.phpt b/tests/tests/DependentSelectBoxTest.phpt index 65b7ef3..c2aca8b 100644 --- a/tests/tests/DependentSelectBoxTest.phpt +++ b/tests/tests/DependentSelectBoxTest.phpt @@ -178,7 +178,7 @@ final class DependentSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', '_do' => 'dependentSelectForm1-dependentSelect-load', 'select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); From 63cbd9815f0d48cce8ff20fb5e5fbb914aca0afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:50:57 +0100 Subject: [PATCH 09/10] Add files via upload --- tests/tests/DependentMultiSelectBoxTest.phpt | 2 +- tests/tests/DependentSelectBoxTest.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/DependentMultiSelectBoxTest.phpt b/tests/tests/DependentMultiSelectBoxTest.phpt index 5c9fb8f..5077c47 100644 --- a/tests/tests/DependentMultiSelectBoxTest.phpt +++ b/tests/tests/DependentMultiSelectBoxTest.phpt @@ -179,7 +179,7 @@ final class DependentMultiSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', '_do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'frm_dependentMultiSelect_select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); diff --git a/tests/tests/DependentSelectBoxTest.phpt b/tests/tests/DependentSelectBoxTest.phpt index c2aca8b..d9dcb78 100644 --- a/tests/tests/DependentSelectBoxTest.phpt +++ b/tests/tests/DependentSelectBoxTest.phpt @@ -178,7 +178,7 @@ final class DependentSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', '_do' => 'dependentSelectForm1-dependentSelect-load', 'select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'frm_dependentSelect_select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); From 6ef7033d336f86b03fe2ad079d9715b3f8ea201a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Tue, 30 Jan 2018 08:54:34 +0100 Subject: [PATCH 10/10] Add files via upload --- tests/tests/DependentMultiSelectBoxTest.phpt | 2 +- tests/tests/DependentSelectBoxTest.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/DependentMultiSelectBoxTest.phpt b/tests/tests/DependentMultiSelectBoxTest.phpt index 5077c47..47ad0b2 100644 --- a/tests/tests/DependentMultiSelectBoxTest.phpt +++ b/tests/tests/DependentMultiSelectBoxTest.phpt @@ -179,7 +179,7 @@ final class DependentMultiSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'frm_dependentMultiSelect_select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentMultiSelect1', 'do' => 'dependentMultiSelectForm1-dependentMultiSelect-load', 'frm_dependentMultiSelectForm1_select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse); diff --git a/tests/tests/DependentSelectBoxTest.phpt b/tests/tests/DependentSelectBoxTest.phpt index d9dcb78..01b0fe2 100644 --- a/tests/tests/DependentSelectBoxTest.phpt +++ b/tests/tests/DependentSelectBoxTest.phpt @@ -178,7 +178,7 @@ final class DependentSelectBoxTest extends Tester\TestCase $presenter = $presenterFactory->createPresenter('Base'); $presenter->autoCanonicalize = false; - $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'frm_dependentSelect_select' => 1]); + $request = new Nette\Application\Request('Base', 'GET', ['action' => 'dependentSelect1', 'do' => 'dependentSelectForm1-dependentSelect-load', 'frm_dependentSelectForm1_select' => 1]); $response = $presenter->run($request); Tester\Assert::true($response instanceof Nette\Application\Responses\JsonResponse);