diff --git a/config/module.config.php b/config/module.config.php index f32cf03..f3f63ff 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -37,13 +37,14 @@ 'form_elements' => array( 'factories' => array( - 'BaconUser\Form\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory' + 'BaconUser\Form\UserFieldset' => 'BaconUser\Form\Factory\UserFieldsetFactory', + 'BaconUser\Form\User\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory' ) ), 'hydrators' => array( 'factories' => array( - 'BaconUser\Hydrator\RegistrationHydrator' => 'BaconUser\Hydrator\Factory\RegistrationHydratorFactory' + 'BaconUser\Hydrator\UserHydrator' => 'BaconUser\Hydrator\Factory\UserHydrator' ) ), diff --git a/src/BaconUser/Form/Factory/RegistrationFormFactory.php b/src/BaconUser/Form/Factory/RegistrationFormFactory.php index 6766b03..ec826f3 100644 --- a/src/BaconUser/Form/Factory/RegistrationFormFactory.php +++ b/src/BaconUser/Form/Factory/RegistrationFormFactory.php @@ -9,7 +9,7 @@ namespace BaconUser\Form\Factory; -use BaconUser\Form\RegistrationForm; +use BaconUser\Form\User\RegistrationForm; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -28,10 +28,8 @@ class RegistrationFormFactory implements FactoryInterface public function createService(ServiceLocatorInterface $serviceLocator) { $parentLocator = $serviceLocator->getServiceLocator(); - $options = $parentLocator->get('BaconUser\Options\UserOptions'); - $form = new RegistrationForm($options); - $form->setHydrator($parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\RegistrationHydrator')); + $form = new RegistrationForm(); $form->setInputFilter($parentLocator->get('InputFilterManager')->get('BaconUser\InputFilter\RegistrationFilter')); return $form; diff --git a/src/BaconUser/Form/Factory/UserFieldsetFactory.php b/src/BaconUser/Form/Factory/UserFieldsetFactory.php new file mode 100644 index 0000000..08d5062 --- /dev/null +++ b/src/BaconUser/Form/Factory/UserFieldsetFactory.php @@ -0,0 +1,38 @@ +getServiceLocator(); + $options = $parentLocator->get('BaconUser\Options\UserOptions'); + $hydrator = $parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\UserHydrator'); + + $fieldset = new UserFieldset($hydrator, $options); + + return $fieldset; + } +} diff --git a/src/BaconUser/Form/User/RegistrationForm.php b/src/BaconUser/Form/User/RegistrationForm.php new file mode 100644 index 0000000..138daea --- /dev/null +++ b/src/BaconUser/Form/User/RegistrationForm.php @@ -0,0 +1,59 @@ +setName('registration-form'); + + $this->add(array( + 'type' => 'BaconUser\Form\UserFieldset', + 'name' => 'user', + 'options' => array( + 'use_as_base_fieldset' => true + ) + )); + + $this->add(array( + 'type' => 'Csrf', + 'name' => 'csrf' + )); + + $this->add(array( + 'type' => 'Submit', + 'name' => 'submit', + 'options' => array( + 'label' => 'Register', + ) + )); + + // Add specific registration elements + + $userFieldset = $this->get('user'); + $userFieldset->add(array( + 'type' => 'Password', + 'name' => 'passwordVerification', + 'options' => array( + 'label' => 'Verify password' + ), + 'attributes' => array( + 'required' => 'required' + ) + )); + } +} diff --git a/src/BaconUser/Form/RegistrationForm.php b/src/BaconUser/Form/UserFieldset.php similarity index 53% rename from src/BaconUser/Form/RegistrationForm.php rename to src/BaconUser/Form/UserFieldset.php index a22797d..a4057a6 100644 --- a/src/BaconUser/Form/RegistrationForm.php +++ b/src/BaconUser/Form/UserFieldset.php @@ -10,16 +10,24 @@ namespace BaconUser\Form; use BaconUser\Options\UserOptionsInterface; -use Zend\Form\Form; +use Zend\Form\Fieldset; +use Zend\Stdlib\Hydrator\HydratorInterface; /** - * Generic registration form. + * Base fieldset for user */ -class RegistrationForm extends Form +class UserFieldset extends Fieldset { - public function __construct(UserOptionsInterface $options) + /** + * Constructor + * + * @param HydratorInterface $hydrator + * @param UserOptionsInterface $options + */ + public function __construct(HydratorInterface $hydrator, UserOptionsInterface $options) { - parent::__construct(null); + parent::__construct('user'); + $this->setHydrator($hydrator); if ($options->getEnableUsername()) { $this->add(array( @@ -28,51 +36,31 @@ public function __construct(UserOptionsInterface $options) 'label' => 'Username', ), 'attributes' => array( - 'type' => 'text', + 'required' => 'required', ), )); } $this->add(array( + 'type' => 'Email', 'name' => 'email', 'options' => array( 'label' => 'Email', ), 'attributes' => array( - 'type' => 'text' - ), + 'required' => 'required' + ) )); $this->add(array( + 'type' => 'Password', 'name' => 'password', 'options' => array( 'label' => 'Password', ), 'attributes' => array( - 'type' => 'password' - ), - )); - - $this->add(array( - 'name' => 'password_verification', - 'options' => array( - 'label' => 'Verify password', - ), - 'attributes' => array( - 'type' => 'password' - ), - )); - - $this->add(array( - 'name' => 'submit', - 'options' => array( - 'label' => 'Register', - ), - 'attributes' => array( - 'type' => 'submit', - ), - ), array( - 'priority' => -100 + 'required' => 'required' + ) )); } } diff --git a/src/BaconUser/Hydrator/Factory/RegistrationHydratorFactory.php b/src/BaconUser/Hydrator/Factory/UserHydratorFactory.php similarity index 78% rename from src/BaconUser/Hydrator/Factory/RegistrationHydratorFactory.php rename to src/BaconUser/Hydrator/Factory/UserHydratorFactory.php index 1d95ba3..4923e67 100644 --- a/src/BaconUser/Hydrator/Factory/RegistrationHydratorFactory.php +++ b/src/BaconUser/Hydrator/Factory/UserHydratorFactory.php @@ -10,27 +10,27 @@ namespace BaconUser\Hydrator\Factory; use BaconUser\Form\PasswordHashingStrategy; -use BaconUser\Hydrator\RegistrationHydrator; +use BaconUser\Hydrator\UserHydrator; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; /** - * Service factory that instantiates {@see RegistrationHydrator}. + * Service factory that instantiates {@see UserHydrator}. */ -class RegistrationHydratorFactory implements FactoryInterface +class UserHydratorFactory implements FactoryInterface { /** * createService(): defined by FactoryInterface. * * @see FactoryInterface::createService() * @param ServiceLocatorInterface $serviceLocator - * @return RegistrationHydrator + * @return UserHydrator */ public function createService(ServiceLocatorInterface $serviceLocator) { $parentLocator = $serviceLocator->getServiceLocator(); - $hydrator = new RegistrationHydrator( + $hydrator = new UserHydrator( new PasswordHashingStrategy( $parentLocator->get('BaconUser\Password\HandlerInterface') ) diff --git a/src/BaconUser/Hydrator/RegistrationHydrator.php b/src/BaconUser/Hydrator/UserHydrator.php similarity index 71% rename from src/BaconUser/Hydrator/RegistrationHydrator.php rename to src/BaconUser/Hydrator/UserHydrator.php index a42387e..afe0bcf 100644 --- a/src/BaconUser/Hydrator/RegistrationHydrator.php +++ b/src/BaconUser/Hydrator/UserHydrator.php @@ -9,35 +9,33 @@ namespace BaconUser\Hydrator; +use Zend\Stdlib\Exception; use Zend\Stdlib\Hydrator\ClassMethods; use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; /** * Hydrator for the {@see RegistrationForm}. */ -class RegistrationHydrator extends ClassMethods +class UserHydrator extends ClassMethods { /** * @param StrategyInterface $passwordHashingStrategy */ public function __construct(StrategyInterface $passwordHashingStrategy) { - parent::__construct(true); - $this->addStrategy('password_hash', $passwordHashingStrategy); + parent::__construct(false); + $this->addStrategy('passwordHash', $passwordHashingStrategy); } /** - * hydrate(): defined by ClassMethods. - * - * @see ClassMethods::hydrate() - * @param array $data + * @param array $data * @param object $object * @return object */ public function hydrate(array $data, $object) { if (isset($data['password'])) { - $data['password_hash'] = $data['password']; + $data['passwordHash'] = $data['password']; } return parent::hydrate($data, $object); diff --git a/src/BaconUser/InputFilter/Factory/RegistrationFilterFactory.php b/src/BaconUser/InputFilter/Factory/RegistrationFilterFactory.php index a1fc230..a086007 100644 --- a/src/BaconUser/InputFilter/Factory/RegistrationFilterFactory.php +++ b/src/BaconUser/InputFilter/Factory/RegistrationFilterFactory.php @@ -32,18 +32,16 @@ public function createService(ServiceLocatorInterface $serviceLocator) $userRepository = $parentLocator->get('BaconUser\Repository\UserRepository'); $options = $parentLocator->get('BaconUser\Options\UserOptions'); - $inputFilter = new RegistrationFilter( - new NoObjectExists(array( - 'object_repository' => $userRepository, - 'fields' => 'email', - )), - new NoObjectExists(array( - 'object_repository' => $userRepository, - 'fields' => 'username', - )), - $options - ); + $usernameValidator = new NoObjectExists(array( + 'object_repository' => $userRepository, + 'fields' => 'username' + )); - return $inputFilter; + $emailValidator = new NoObjectExists(array( + 'object_repository' => $userRepository, + 'fields' => 'email' + )); + + return new RegistrationFilter($userRepository, $usernameValidator, $emailValidator, $options); } } diff --git a/src/BaconUser/InputFilter/RegistrationFilter.php b/src/BaconUser/InputFilter/RegistrationFilter.php index 662b6a2..6edde81 100644 --- a/src/BaconUser/InputFilter/RegistrationFilter.php +++ b/src/BaconUser/InputFilter/RegistrationFilter.php @@ -10,81 +10,49 @@ namespace BaconUser\InputFilter; use BaconUser\Options\UserOptionsInterface; -use Zend\InputFilter\InputFilter; +use Doctrine\Common\Persistence\ObjectRepository; use Zend\Validator\ValidatorInterface; /** * Input filter for the {@see RegistrationForm}. */ -class RegistrationFilter extends InputFilter +class RegistrationFilter extends UserFilter { /** - * @param ValidatorInterface $emailUniqueValidator - * @param ValidatorInterface $usernameUniqueValidator + * @param ObjectRepository $objectRepository + * @param ValidatorInterface $usernameValidator + * @param ValidatorInterface $emailValidator * @param UserOptionsInterface $options */ public function __construct( - ValidatorInterface $emailUniqueValidator, - ValidatorInterface $usernameUniqueValidator, + ObjectRepository $objectRepository, + ValidatorInterface $usernameValidator, + ValidatorInterface $emailValidator, UserOptionsInterface $options ) { + parent::__construct($options); + if ($options->getEnableUsername()) { - $this->add(array( - 'name' => 'username', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( - 'min' => 3, - 'max' => 255, - ), - ), - $usernameUniqueValidator, - ), - )); + $usernameInput = $this->get('username'); + $usernameInput->getValidatorChain()->attach($usernameValidator); } - $this->add(array( - 'name' => 'email', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'EmailAddress' - ), - $emailUniqueValidator, - ), - )); - - $this->add(array( - 'name' => 'password', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( - 'min' => 6, - ), - ), - ), - )); + $emailInput = $this->get('email'); + $emailInput->getValidatorChain()->attach($emailValidator); $this->add(array( - 'name' => 'password_verification', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), + 'name' => 'passwordVerification', + 'required' => true, + 'filters' => array(array('name' => 'StringTrim')), 'validators' => array( array( - 'name' => 'StringLength', + 'name' => 'StringLength', 'options' => array( 'min' => 6, ), ), array( - 'name' => 'Identical', + 'name' => 'Identical', 'options' => array( 'token' => 'password', ), diff --git a/src/BaconUser/InputFilter/UserFilter.php b/src/BaconUser/InputFilter/UserFilter.php new file mode 100644 index 0000000..f2a288b --- /dev/null +++ b/src/BaconUser/InputFilter/UserFilter.php @@ -0,0 +1,67 @@ +getEnableUsername()) { + $this->add(array( + 'name' => 'username', + 'required' => true, + 'filters' => array(array('name' => 'StringTrim')), + 'validators' => array( + array( + 'name' => 'StringLength', + 'options' => array( + 'min' => 3, + 'max' => 255, + ), + ) + ), + )); + } + + $this->add(array( + 'name' => 'email', + 'required' => true, + 'filters' => array(array('name' => 'StringTrim')), + 'validators' => array( + array( + 'name' => 'EmailAddress' + ) + ), + )); + + $this->add(array( + 'name' => 'password', + 'required' => true, + 'filters' => array(array('name' => 'StringTrim')), + 'validators' => array( + array( + 'name' => 'StringLength', + 'options' => array( + 'min' => 6, + ), + ), + ), + )); + } +} diff --git a/src/BaconUser/Service/Factory/RegistrationServiceFactory.php b/src/BaconUser/Service/Factory/RegistrationServiceFactory.php index bf22872..3b76bd0 100644 --- a/src/BaconUser/Service/Factory/RegistrationServiceFactory.php +++ b/src/BaconUser/Service/Factory/RegistrationServiceFactory.php @@ -28,7 +28,7 @@ class RegistrationServiceFactory implements FactoryInterface public function createService(ServiceLocatorInterface $serviceLocator) { $registrationService = new RegistrationService( - $serviceLocator->get('BaconUser\Form\RegistrationForm'), + $serviceLocator->get('BaconUser\Form\User\RegistrationForm'), $serviceLocator->get('BaconUser\ObjectManager'), $serviceLocator->get('BaconUser\Options\UserOptions') ); diff --git a/tests/BaconUserTest/Form/Factory/RegistrationFormFactoryTest.php b/tests/BaconUserTest/Form/Factory/RegistrationFormFactoryTest.php index 2ea2a69..0a74b37 100644 --- a/tests/BaconUserTest/Form/Factory/RegistrationFormFactoryTest.php +++ b/tests/BaconUserTest/Form/Factory/RegistrationFormFactoryTest.php @@ -24,21 +24,15 @@ class RegistrationFormFactoryTest extends TestCase public function testFactoryProcessesWithoutErrors() { $options = $this->getMock('BaconUser\Options\UserOptionsInterface'); - $hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'); $inputFilter = $this->getMock('Zend\InputFilter\InputFilterInterface'); $parentLocator = new ServiceManager(); $parentLocator->setService('BaconUser\Options\UserOptions', $options); - $hydratorManager = new HydratorPluginManager(); - $hydratorManager->setServiceLocator($parentLocator); - $hydratorManager->setService('BaconUser\Hydrator\RegistrationHydrator', $hydrator); - $inputFilterManager = new InputFilterPluginManager(); $inputFilterManager->setServiceLocator($parentLocator); $inputFilterManager->setService('BaconUser\InputFilter\RegistrationFilter', $inputFilter); - $parentLocator->setService('HydratorManager', $hydratorManager); $parentLocator->setService('InputFilterManager', $inputFilterManager); $formManager = new FormElementManager(); diff --git a/tests/BaconUserTest/Form/Factory/UserFieldsetFactoryTest.php b/tests/BaconUserTest/Form/Factory/UserFieldsetFactoryTest.php new file mode 100644 index 0000000..b693a56 --- /dev/null +++ b/tests/BaconUserTest/Form/Factory/UserFieldsetFactoryTest.php @@ -0,0 +1,43 @@ +getMock('BaconUser\Options\UserOptionsInterface'); + + $parentLocator = new ServiceManager(); + $parentLocator->setService('BaconUser\Options\UserOptions', $options); + + $hydratorManager = new HydratorPluginManager(); + $hydratorManager->setServiceLocator($parentLocator); + $hydratorManager->setService('BaconUser\Hydrator\UserHydrator', $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface')); + + $parentLocator->setService('HydratorManager', $hydratorManager); + + $formManager = new FormElementManager(); + $formManager->setServiceLocator($parentLocator); + + $factory = new UserFieldsetFactory(); + $factory->createService($formManager); + // Expect no exceptions or errors. + } +} diff --git a/tests/BaconUserTest/Form/User/RegistrationFormTest.php b/tests/BaconUserTest/Form/User/RegistrationFormTest.php new file mode 100644 index 0000000..e6cd89c --- /dev/null +++ b/tests/BaconUserTest/Form/User/RegistrationFormTest.php @@ -0,0 +1,54 @@ +setFactory('BaconUser\Form\UserFieldset', function($serviceLocator) { + return new UserFieldset($this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'), new UserOptions()); + }); + + $this->registrationForm = $formElementManager->get('BaconUser\Form\User\RegistrationForm'); + } + + public function testFormHasDefaultName() + { + $this->assertEquals('registration-form', $this->registrationForm->getName()); + } + + public function testAssertUserFieldsetIsConfiguredAsBaseFieldset() + { + $this->assertTrue($this->registrationForm->get('user')->useAsBaseFieldset(), 'User fieldset is not configured as base fieldset'); + } + + public function testContextSpecificElements() + { + $this->assertTrue($this->registrationForm->has('csrf'), 'CSRF field missing'); + $this->assertTrue($this->registrationForm->has('submit'), 'Submit field missing'); + $this->assertTrue($this->registrationForm->get('user')->has('passwordVerification'), 'Password verification field missing'); + } +} diff --git a/tests/BaconUserTest/Form/RegistrationFormTest.php b/tests/BaconUserTest/Form/UserFieldsetTest.php similarity index 62% rename from tests/BaconUserTest/Form/RegistrationFormTest.php rename to tests/BaconUserTest/Form/UserFieldsetTest.php index e4d57a3..dc7f6df 100644 --- a/tests/BaconUserTest/Form/RegistrationFormTest.php +++ b/tests/BaconUserTest/Form/UserFieldsetTest.php @@ -9,35 +9,35 @@ namespace BaconUserTest\Form; -use BaconUser\Form\RegistrationForm; +use BaconUser\Form\UserFieldset; use BaconUser\Options\UserOptions; use PHPUnit_Framework_TestCase as TestCase; /** - * @covers BaconUser\Form\RegistrationForm + * @covers BaconUser\Form\UserFieldset */ -class RegistrationFormTest extends TestCase +class UserFieldsetTest extends TestCase { - public function testFormWithAllFieldsEnabled() + public function testFieldsetWithAllFieldsEnabled() { - $form = new RegistrationForm( + $form = new UserFieldset( + $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'), new UserOptions(array( - 'enable_username' => true, + 'enable_username' => true, )) ); $this->assertTrue($form->has('username'), 'Username field missing'); $this->assertTrue($form->has('email'), 'Email field missing'); $this->assertTrue($form->has('password'), 'Password field missing'); - $this->assertTrue($form->has('password_verification'), 'Password verification field missing'); - $this->assertTrue($form->has('submit'), 'Submit field missing'); } public function testFormWithUsernameDisabled() { - $form = new RegistrationForm( + $form = new UserFieldset( + $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'), new UserOptions(array( - 'enable_username' => false, + 'enable_username' => false, )) ); diff --git a/tests/BaconUserTest/Hydrator/Factory/RegistrationHydratorFactoryTest.php b/tests/BaconUserTest/Hydrator/Factory/UserHydratorFactoryTest.php similarity index 79% rename from tests/BaconUserTest/Hydrator/Factory/RegistrationHydratorFactoryTest.php rename to tests/BaconUserTest/Hydrator/Factory/UserHydratorFactoryTest.php index c90d6ac..75cbb47 100644 --- a/tests/BaconUserTest/Hydrator/Factory/RegistrationHydratorFactoryTest.php +++ b/tests/BaconUserTest/Hydrator/Factory/UserHydratorFactoryTest.php @@ -9,15 +9,15 @@ namespace BaconUserTest\Hydrator\Factory; -use BaconUser\Hydrator\Factory\RegistrationHydratorFactory; +use BaconUser\Hydrator\Factory\UserHydratorFactory; use PHPUnit_Framework_TestCase as TestCase; use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\Hydrator\HydratorPluginManager; /** - * @covers BaconUser\Hydrator\Factory\RegistrationHydratorFactory + * @covers BaconUser\Hydrator\Factory\UserHydratorFactory */ -class RegistrationHydratorFactoryTest extends TestCase +class UserHydratorFactoryTest extends TestCase { public function testFactoryProcessesWithoutErrors() { @@ -29,7 +29,7 @@ public function testFactoryProcessesWithoutErrors() $hydratorManager = new HydratorPluginManager(); $hydratorManager->setServiceLocator($parentLocator); - $factory = new RegistrationHydratorFactory(); + $factory = new UserHydratorFactory(); $factory->createService($hydratorManager); // Expect no exceptions or errors. } diff --git a/tests/BaconUserTest/Hydrator/RegistrationHydratorTest.php b/tests/BaconUserTest/Hydrator/UserHydratorTest.php similarity index 84% rename from tests/BaconUserTest/Hydrator/RegistrationHydratorTest.php rename to tests/BaconUserTest/Hydrator/UserHydratorTest.php index 21647a6..0f0106d 100644 --- a/tests/BaconUserTest/Hydrator/RegistrationHydratorTest.php +++ b/tests/BaconUserTest/Hydrator/UserHydratorTest.php @@ -10,13 +10,13 @@ namespace BaconUserTest\Hydrator; use BaconUser\Entity\User; -use BaconUser\Hydrator\RegistrationHydrator; +use BaconUser\Hydrator\UserHydrator; use PHPUnit_Framework_TestCase as TestCase; /** - * @covers BaconUser\Hydrator\RegistrationHydrator + * @covers BaconUser\Hydrator\UserHydrator */ -class RegistrationHydratorTest extends TestCase +class UserHydratorTest extends TestCase { public function testHydrate() { @@ -26,7 +26,7 @@ public function testHydrate() ->with($this->equalTo('foobar')) ->will($this->returnValue('bazbat')); - $hydrator = new RegistrationHydrator($hashingStrategy); + $hydrator = new UserHydrator($hashingStrategy); $user = new User(); $hydrator->hydrate( diff --git a/tests/BaconUserTest/InputFilter/RegistrationFilterTest.php b/tests/BaconUserTest/InputFilter/RegistrationFilterTest.php index d3c29eb..688eb2c 100644 --- a/tests/BaconUserTest/InputFilter/RegistrationFilterTest.php +++ b/tests/BaconUserTest/InputFilter/RegistrationFilterTest.php @@ -26,13 +26,13 @@ public static function formDataProvider() 'username' => 'foobar', 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), array( 'username' => 'foobar', 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), ), 'space-padded-valid-input' => array( @@ -41,21 +41,21 @@ public static function formDataProvider() 'email' => ' foobar@example.com ', // @todo Bug with identical validator (compares filtered against raw value) 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), array( 'username' => 'foobar', 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), ), 'duplicate-email' => array( array( 'username' => 'foobar', - 'email' => 'foobar@example.com', + 'email' => 'nfoobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), null, true, @@ -67,7 +67,7 @@ public static function formDataProvider() 'username' => 'foobar', 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), null, true, @@ -79,26 +79,26 @@ public static function formDataProvider() 'username' => 'foobar', 'email' => 'foobar@example.com', 'password' => 'foobar', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), ), 'invalid-with-enabled-username' => array( array( 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), ), 'valid-with-disabled-username' => array( array( 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), array( 'email' => 'foobar@example.com', 'password' => 'bazbat', - 'password_verification' => 'bazbat', + 'passwordVerification' => 'bazbat', ), false ), @@ -121,11 +121,14 @@ public function testInputFilter( $emailIsUnique = true, $usernameIsUnique = true ) { + $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); + $filter = new RegistrationFilter( - $this->getMockValidator($emailIsUnique), + $objectRepository, $this->getMockValidator($usernameIsUnique), + $this->getMockValidator($emailIsUnique), new UserOptions(array( - 'enable_username' => $enableUsername, + 'enable_username' => $enableUsername, )) ); diff --git a/tests/BaconUserTest/InputFilter/UserFilterTest.php b/tests/BaconUserTest/InputFilter/UserFilterTest.php new file mode 100644 index 0000000..85422e9 --- /dev/null +++ b/tests/BaconUserTest/InputFilter/UserFilterTest.php @@ -0,0 +1,95 @@ + array( + array( + 'username' => 'foobar', + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + array( + 'username' => 'foobar', + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + ), + 'space-padded-valid-input' => array( + array( + 'username' => ' foobar ', + 'email' => ' foobar@example.com ', + 'password' => 'bazbat', + ), + array( + 'username' => 'foobar', + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + ), + 'invalid-with-enabled-username' => array( + array( + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + ), + 'valid-with-disabled-username' => array( + array( + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + array( + 'email' => 'foobar@example.com', + 'password' => 'bazbat', + ), + false + ), + ); + } + + /** + * @dataProvider formDataProvider + * @param array $input + * @param array|null $output + * @param bool $enableUsername + * @return void + */ + public function testInputFilter( + array $input, + array $output = null, + $enableUsername = true + ) { + $filter = new UserFilter( + new UserOptions(array( + 'enable_username' => $enableUsername, + )) + ); + + $filter->setData($input); + + if ($output === null) { + $this->assertFalse($filter->isValid(), 'Input must not be valid'); + } else { + $this->assertTrue($filter->isValid(), 'Input must be valid'); + $this->assertEquals($output, $filter->getValues()); + } + } +} diff --git a/tests/BaconUserTest/Service/Factory/RegistrationServiceFactoryTest.php b/tests/BaconUserTest/Service/Factory/RegistrationServiceFactoryTest.php index 5d19d1e..d771c29 100644 --- a/tests/BaconUserTest/Service/Factory/RegistrationServiceFactoryTest.php +++ b/tests/BaconUserTest/Service/Factory/RegistrationServiceFactoryTest.php @@ -26,7 +26,7 @@ public function testFactoryProcessesWithoutErrors() $userPrototype = $this->getMock('BaconUser\Entity\UserInterface'); $locator = new ServiceManager(); - $locator->setService('BaconUser\Form\RegistrationForm', $registrationForm); + $locator->setService('BaconUser\Form\User\RegistrationForm', $registrationForm); $locator->setService('BaconUser\ObjectManager', $objectManager); $locator->setService('BaconUser\Options\UserOptions', $options); $locator->setService('BaconUser\Entity\UserPrototype', $userPrototype);