diff --git a/composer.json b/composer.json index 25fbc8a..c763e49 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,9 @@ "adamwathan/form": "~0.8.9", "zendframework/zend-view": "^2.8", "zendframework/zend-modulemanager": "^2.7.2", - "zendframework/zend-servicemanager": "^3.1" + "zendframework/zend-servicemanager": "^3.1", + "zendframework/zend-validator": "^2.8", + "zendframework/zend-inputfilter": "^2.7.2" }, "require-dev": { "phpunit/phpunit": "^5.5", diff --git a/src/FormAbstract.php b/src/FormAbstract.php index 8fdd448..fdb2d6d 100644 --- a/src/FormAbstract.php +++ b/src/FormAbstract.php @@ -61,7 +61,7 @@ public function isValid() /** * Returns values from the input filter. - * @param boolean $onlyValid If set `true` then returns only validated values. + * @param boolean $onlyValid If set as `true` will returned raw values (no filtered). * @return array */ public function getValues($onlyValid = true) diff --git a/tests/Asset/SignInFilter.php b/tests/Asset/SignInFilter.php new file mode 100644 index 0000000..76f424f --- /dev/null +++ b/tests/Asset/SignInFilter.php @@ -0,0 +1,36 @@ +add([ + 'name' => 'email', + 'required' => true, + 'validators' => [ + [ + 'name' => 'EmailAddress', + ], + ], + ]); + $this->add([ + 'name' => 'password', + 'required' => true, + 'filters' => [ + [ + 'name' => 'StringTrim', + ], + ], + ]); + } +} diff --git a/tests/Asset/SignInForm.php b/tests/Asset/SignInForm.php new file mode 100644 index 0000000..86389d1 --- /dev/null +++ b/tests/Asset/SignInForm.php @@ -0,0 +1,38 @@ + 'email', + 'required' => true, + 'validators' => [ + [ + 'name' => 'EmailAddress', + ], + ], + ], + [ + 'name' => 'password', + 'required' => true, + 'filters' => [ + [ + 'name' => 'StringTrim', + ] + ], + ], + ]; + } +} \ No newline at end of file diff --git a/tests/FormAbstractTest.php b/tests/FormAbstractTest.php new file mode 100644 index 0000000..2a7c310 --- /dev/null +++ b/tests/FormAbstractTest.php @@ -0,0 +1,81 @@ + + * @since 1.0.0 + */ +class FormAstractTest extends PHPUnit_Framework_TestCase +{ + /** + * Testing inputs data. + */ + public function testInputs() + { + // valid + $signInForm = new SignInForm; + $signInForm->setValues([ + 'email' => 'test@gmail.com', + 'password' => '12q34e56t78', + ]); + $this->assertTrue($signInForm->isValid()); + + // invalid + $signInForm = new SignInForm; + $signInForm->setValues([ + 'email' => 'test@gmail.never', + 'password' => '', + ]); + $this->assertFalse($signInForm->isValid()); + $errors = $signInForm->getErrors(); + $this->assertTrue(!empty($errors)); + } + + /** + * Testing outputs data. + */ + public function testOutputs() + { + // valid + $signInForm = new SignInForm; + $signInForm->setValues([ + 'email' => 'test@gmail.com', + 'password' => ' 12q34e56t78 ', + ]); + $this->assertTrue($signInForm->isValid()); + $values = $signInForm->getValues(); + $this->assertEquals('12q34e56t78', $values['password']); + $values = $signInForm->getValues(false); + $this->assertEquals(' 12q34e56t78 ', $values['password']); + + // invalid + $signInForm = new SignInForm; + $signInForm->setValues([ + 'email' => 'test@gmail.co2m', + 'password' => ' 12q34e56t78 ', + ]); + $this->assertFalse($signInForm->isValid()); + $values = $signInForm->getValues(); + $this->assertEquals('12q34e56t78', $values['password']); + $values = $signInForm->getValues(false); + $this->assertEquals(' 12q34e56t78 ', $values['password']); + } + + /** + * Testing instance of input filter. + */ + public function testInstanceInputFilter() + { + $signInForm = new SignInForm; + $signInForm->setInputFilter(new SignInFilter); + $this->assertInstanceOf(SignInFilter::class, $signInForm->getInputFilter()); + $signInForm->setValues([]); + $this->assertFalse($signInForm->isValid()); + } +} + diff --git a/tests/FormBuilderFactoryTest.php b/tests/FormBuilderFactoryTest.php index 632c2d2..ecf9696 100644 --- a/tests/FormBuilderFactoryTest.php +++ b/tests/FormBuilderFactoryTest.php @@ -17,7 +17,7 @@ class FormBuilderFactoryTest extends PHPUnit_Framework_TestCase */ public function testInstance() { - $moduleConfig = require __DIR__ . '/../config/module.config.php'; + $moduleConfig = require __DIR__ . '/../config/module.config.php'; $serviceManager = new ServiceManager($moduleConfig['service_manager']); $formBuilder1 = $serviceManager->get('Bupy7\Form\FormBuilder'); diff --git a/tests/ModuleTest.php b/tests/ModuleTest.php index e157c9c..d411bd8 100644 --- a/tests/ModuleTest.php +++ b/tests/ModuleTest.php @@ -26,11 +26,11 @@ public function testLoader() 'module_paths' => [ __DIR__ . '/../src' ], - 'config_cache_enabled' => false, - 'module_map_cache_enabled' => false, - 'check_dependencies' => true, + 'config_cache_enabled' => false, + 'module_map_cache_enabled' => false, + 'check_dependencies' => true, ], ]); $this->assertInstanceOf(Module::class, $moduleLoader->getModule('Bupy7\Form')); } -} +} \ No newline at end of file diff --git a/tests/View/Helper/FormBuilderHelperFactoryTest.php b/tests/View/Helper/FormBuilderHelperFactoryTest.php index 92ae361..849022e 100644 --- a/tests/View/Helper/FormBuilderHelperFactoryTest.php +++ b/tests/View/Helper/FormBuilderHelperFactoryTest.php @@ -7,6 +7,7 @@ use Zend\ServiceManager\ServiceManager; use AdamWathan\Form\FormBuilder; use Bupy7\Form\View\Helper\FormBuilderHelper; +use Bupy7\Form\Tests\Asset\SignInForm; /** * @author Vasilij Belosludcev @@ -19,28 +20,21 @@ class FormBuilderHelperFactoryTest extends PHPUnit_Framework_TestCase */ public function testInstance() { - $moduleConfig = require __DIR__ . '/../../../config/module.config.php'; - $serviceManager = new ServiceManager($moduleConfig['service_manager']); - $helperPluginManager = new HelperPluginManager($serviceManager, $moduleConfig['view_helpers']); + $moduleConfig = require __DIR__ . '/../../../config/module.config.php'; + $serviceManager = new ServiceManager($moduleConfig['service_manager']); + $helperPluginManager = new HelperPluginManager($serviceManager, $moduleConfig['view_helpers']); + $signInForm = new SignInForm; $formBuilderHelper1 = $helperPluginManager->get('formBuilder'); + $formBuilderHelper1()->setToken('test token 1'); $this->assertInstanceOf(FormBuilder::class, $formBuilderHelper1()); - $formBuilderHelper2 = $helperPluginManager->get('formBuilder'); - $this->assertInstanceOf(FormBuilder::class, $formBuilderHelper2()); - - $formBuilderHelper3 = $helperPluginManager->get(FormBuilderHelper::class); - $this->assertInstanceOf(FormBuilder::class, $formBuilderHelper3()); - - $formBuilderHelper4 = $helperPluginManager->get(FormBuilderHelper::class); - $this->assertInstanceOf(FormBuilder::class, $formBuilderHelper4()); - - $formBuilderHelper1()->setToken('test token 1'); + $formBuilderHelper2 = $helperPluginManager->get(FormBuilderHelper::class); $formBuilderHelper2()->setToken('test token 2'); - $formBuilderHelper3()->setToken('test token 3'); - $formBuilderHelper4()->setToken('test token 4'); + $this->assertInstanceOf(FormBuilder::class, $formBuilderHelper2($signInForm)); + $this->assertNotEquals($formBuilderHelper1(), $formBuilderHelper2()); - $this->assertNotEquals($formBuilderHelper3(), $formBuilderHelper4()); + } }