Skip to content

Commit

Permalink
added unit test for Form\RegisterFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
kokspflanze committed May 29, 2015
1 parent 3ac9e38 commit db4390c
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 53 deletions.
72 changes: 31 additions & 41 deletions src/PServerCMS/Form/RegisterFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@ class RegisterFilter extends ProvidesEventsInputFilter
protected $serviceManager;
protected $entityManager;

/** @var AbstractRecord */
protected $usernameValidator;
/** @var Validator\StriposExists */
protected $striposValidator;


/**
* @param ServiceManager $serviceManager
*/
public function __construct( ServiceManager $serviceManager )
{

$this->setServiceManager($serviceManager);

/** @var $repositoryUser \Doctrine\Common\Persistence\ObjectRepository */
$repositoryUser = $serviceManager->get('Doctrine\ORM\EntityManager')->getRepository($this->getEntityOptions()->getUser());
$this->setUsernameValidator( new Validator\NoRecordExists( $repositoryUser, 'username' ) );
$this->setStriposValidator( new Validator\StriposExists($serviceManager, Validator\StriposExists::TYPE_EMAIL) );
$userNameBackendNotExists = new Validator\UserNameBackendNotExists($serviceManager);

$this->add(array(
'name' => 'username',
'required' => true,
Expand All @@ -45,7 +35,7 @@ public function __construct( ServiceManager $serviceManager )
'name' => 'Alnum',
),
$this->getUsernameValidator(),
$userNameBackendNotExists
$this->getUserNameBackendNotExistsValidator()
),
));

Expand Down Expand Up @@ -161,7 +151,8 @@ public function __construct( ServiceManager $serviceManager )
*
* @return $this
*/
public function setServiceManager( ServiceManager $oServiceManager ) {
public function setServiceManager( ServiceManager $oServiceManager )
{
$this->serviceManager = $oServiceManager;

return $this;
Expand All @@ -170,14 +161,16 @@ public function setServiceManager( ServiceManager $oServiceManager ) {
/**
* @return ServiceManager
*/
protected function getServiceManager() {
protected function getServiceManager()
{
return $this->serviceManager;
}

/**
* @return array
*/
protected function getSecretQuestionList(){
protected function getSecretQuestionList()
{
/** @var \PServerCMS\Entity\Repository\SecretQuestion $secret */
$secret = $this->getEntityManager()->getRepository('PServerCMS\Entity\SecretQuestion');
$secretQuestion = $secret->getQuestions();
Expand All @@ -193,41 +186,37 @@ protected function getSecretQuestionList(){
/**
* @return AbstractRecord
*/
public function getUsernameValidator() {
return $this->usernameValidator;
}
public function getUsernameValidator()
{
/** @var $repositoryUser \Doctrine\Common\Persistence\ObjectRepository */
$repositoryUser = $this->getServiceManager()
->get('Doctrine\ORM\EntityManager')
->getRepository($this->getEntityOptions()->getUser());

/**
* @param AbstractRecord $usernameValidator
*
* @return $this
*/
public function setUsernameValidator($usernameValidator) {
$this->usernameValidator = $usernameValidator;
return $this;
return new Validator\NoRecordExists( $repositoryUser, 'username' );
}

/**
* @return Validator\StriposExists
*/
public function getStriposValidator() {
return $this->striposValidator;
public function getStriposValidator()
{
return new Validator\StriposExists($this->getServiceManager(), Validator\StriposExists::TYPE_EMAIL);
}

/**
* @param Validator\StriposExists $striposValidator
*
* @return $this
*/
public function setStriposValidator($striposValidator) {
$this->striposValidator = $striposValidator;
return $this;
}
/**
* @return Validator\UserNameBackendNotExists
*/
public function getUserNameBackendNotExistsValidator()
{
return new Validator\UserNameBackendNotExists($this->getServiceManager());
}

/**
* @return \Doctrine\ORM\EntityManager
*/
protected function getEntityManager() {
protected function getEntityManager()
{
if (!$this->entityManager) {
$this->entityManager = $this->getServiceManager()->get('Doctrine\ORM\EntityManager');
}
Expand All @@ -238,7 +227,8 @@ protected function getEntityManager() {
/**
* @return \PServerCMS\Options\EntityOptions
*/
protected function getEntityOptions(){
protected function getEntityOptions()
{
return $this->getServiceManager()->get('pserver_entity_options');
}
}
80 changes: 80 additions & 0 deletions tests/PServerCMSTest/Form/RegisterFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php


namespace PServerCMSTest\Form;


use PServerCMSTest\Util\TestBase;

class RegisterFilterTest extends TestBase
{
/** @var string */
protected $className = '\PServerCMS\Form\RegisterFilter';

public function testIsValid()
{
$this->mockedMethodList = [
'getUsernameValidator',
'getUserNameBackendNotExistsValidator'
];

$noRecordExistsMock = $this->getMockBuilder('PServerCMS\Validator\NoRecordExists')
->disableOriginalConstructor()
->setMethods(['isValid'])
->getMock();

$noRecordExistsMock->expects($this->any())
->method('isValid')
->willReturn(true);

$UserNameBackendNotExistsMock = $this->getMockBuilder('PServerCMS\Validator\UserNameBackendNotExists')
->disableOriginalConstructor()
->setMethods(['isValid'])
->getMock();

$UserNameBackendNotExistsMock->expects($this->any())
->method('isValid')
->willReturn(true);

$class = $this->getClass();

$class->expects($this->any())
->method('getUsernameValidator')
->willReturn($noRecordExistsMock);

$class->expects($this->any())
->method('getUserNameBackendNotExistsValidator')
->willReturn($UserNameBackendNotExistsMock);

$class->__construct($this->serviceManager);

$class->setData([
'username' => 'fodfgo',
'email' => 'fodfgo@travel.com',
'emailVerify' => 'fodfgo@travel.com',
'password' => 'fodfgo',
'passwordVerify' => 'fodfgo',
]);

$this->assertTrue($class->isValid());
}

/**
* @return \PServerCMS\Form\RegisterFilter|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getClass()
{
if (!$this->class) {
/** @var \Zend\ServiceManager\ServiceManagerAwareInterface $class */
$class = $this->getMockBuilder($this->className)
->disableOriginalConstructor()
->setMethods($this->getMockedMethodList())
->getMock();

$this->class = $class;
}

return $this->class;
}

}
17 changes: 17 additions & 0 deletions tests/PServerCMSTest/Service/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace PServerCMSTest\Service;


use PServerCMSTest\Util\TestBase;

class UserTest extends TestBase
{

public function testRegister()
{
$this->markTestSkipped('need to refactor the method first');
}

}
26 changes: 16 additions & 10 deletions tests/PServerCMSTest/Util/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class TestBase extends TestCase
protected $serviceManager;
/** @var string */
protected $className;
/** @var array */
protected $mockedMethods = [];
/** @var object */
/** @var array|null */
protected $mockedMethodList = null;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $class;

public function setUp()
Expand All @@ -37,24 +37,30 @@ protected function getMethod($methodName) {
}

/**
* @return \Zend\ServiceManager\ServiceManagerAwareInterface
* @return \Zend\ServiceManager\ServiceManagerAwareInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getClass()
{
if (!$this->class) {
/** @var \Zend\ServiceManager\ServiceManagerAwareInterface */
$this->class = new $this->className;
$this->class->setServiceManager($this->serviceManager);
/** @var \Zend\ServiceManager\ServiceManagerAwareInterface $class */
$class = $this->getMockBuilder($this->className)
->disableOriginalConstructor()
->setMethods($this->getMockedMethodList())
->getMock();

$class->setServiceManager($this->serviceManager);

$this->class = $class;
}

return $this->class;
}

/**
* @return array
* @return array|null
*/
protected function getMockedMethods()
protected function getMockedMethodList()
{
return $this->mockedMethods;
return $this->mockedMethodList;
}
}
4 changes: 2 additions & 2 deletions tests/PServerCMSTest/Validator/ValidUserExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ValidUserExistsTest extends TestBase

public function testIsValid()
{
$this->mockedMethods = [
$this->mockedMethodList = [
'query'
];

Expand Down Expand Up @@ -54,7 +54,7 @@ public function getClass($newInstance = false)
if (!$this->class || $newInstance) {
$this->class = $this->getMockBuilder($this->className)
->disableOriginalConstructor()
->setMethods($this->getMockedMethods())
->setMethods($this->getMockedMethodList())
->getMock();
}

Expand Down

0 comments on commit db4390c

Please sign in to comment.