Skip to content

Commit

Permalink
added new tests for Entity/User
Browse files Browse the repository at this point in the history
  • Loading branch information
kokspflanze committed Apr 15, 2015
1 parent 7f1cb93 commit 64e5364
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/PServerCMS/Service/Donate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function getStatisticData( $lastDays = 10 )
$range = DateTimer::getDateRange4Period($dateTime, new \DateTime());
foreach($range as $date){
foreach($typList as $type){
$result[$date->format('Y-m-d')][$type] = ['amount' => 0, 'coins' => 0];
$result[$date->format('Y-m-d')][$type] = [
'amount' => 0,
'coins' => 0
];
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/PServerCMS/Service/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PServerCMS\Entity\UserCodes;
use PServerCMS\Entity\UserInterface;
use SmallUser\Entity\UserInterface as SmallUserInterface;
use PServerCMS\Entity\User as Entity;
use PServerCMS\Entity\Repository\AvailableCountries as RepositoryAvailableCountries;
use PServerCMS\Entity\Repository\CountryList;
Expand Down Expand Up @@ -324,10 +325,10 @@ protected function setAvailableCountries4User( UserInterface $user, $ip)
}

/**
* @param UserInterface $user
* @param SmallUserInterface $user
* @return bool
*/
protected function isValidLogin( UserInterface $user )
protected function isValidLogin( SmallUserInterface $user )
{
$result = true;
if (!$this->isCountryAllowed( $user )) {
Expand Down Expand Up @@ -400,9 +401,9 @@ protected function getUser4Id( $userId )
}

/**
* @param UserInterface $user
* @param SmallUserInterface $user
*/
protected function doLogin( UserInterface $user )
protected function doLogin( SmallUserInterface $user )
{
parent::doLogin( $user );
$entityManager = $this->getEntityManager();
Expand All @@ -419,10 +420,10 @@ protected function doLogin( UserInterface $user )
}

/**
* @param UserInterface $user
* @param SmallUserInterface $user
* @return bool
*/
protected function handleInvalidLogin( UserInterface $user )
protected function handleInvalidLogin( SmallUserInterface $user )
{
$maxTries = $this->getConfigService()->get( 'pserver.login.exploit.try' );

Expand Down
68 changes: 65 additions & 3 deletions tests/PServerCMSTest/Entity/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
namespace PServerCMSTest\Entity;

use PServerCMS\Entity\User;
use PServerCMS\Entity\UserExtension;
use PServerCMS\Entity\UserRole;
use PServerCMSTest\Util\TestBase;
use Zend\Crypt\Password\Bcrypt;

class UserTest extends \PHPUnit_Framework_TestCase
class UserTest extends TestBase
{
public function testConstruct()
{
Expand Down Expand Up @@ -127,9 +129,32 @@ public function testGetRoles()
$this->assertInstanceOf('Zend\Permissions\Acl\Role\RoleInterface', $result[0]);
}

public function testHashPassword()
public function testUserExtension()
{
$this->markTestIncomplete('missing mock servicemanager');
$entity = new User();
$entityExtension = new UserExtension();

$entity->addUserExtension($entityExtension);
$entity->addUserExtension($entityExtension);

$this->assertEquals(2, count($entity->getUserExtension()));

$entity->removeUserExtension($entityExtension);

$this->assertEquals(1, count($entity->getUserExtension()));
}

public function testHashPasswordTrue()
{
$userService = $this->getMockBuilder('PServerCMS\Service\User')
->disableOriginalConstructor()
->getMock();

$userService->expects($this->any())
->method('isSamePasswordOption')
->will($this->returnValue(true));

$this->serviceManager->setService('small_user_service', $userService);

$entity = new User();
$password = 'foobar';
Expand All @@ -141,5 +166,42 @@ public function testHashPassword()
$this->assertTrue($result);
}

public function testHashPasswordFalse()
{
// We need a mocking of GameBackend =)
$gameService = $this->getMockBuilder('GameBackend\DataService\DataServiceInterface')
->disableOriginalConstructor()
->getMock();

$gameService->expects($this->any())
->method('isPasswordSame')
->will($this->returnValue(false));

// Mock UserService
$userService = $this->getMockBuilder('PServerCMS\Service\User')
->disableOriginalConstructor()
->getMock();

$userService->expects($this->any())
->method('isSamePasswordOption')
->will($this->returnValue(false));

$userService->expects($this->any())
->method('getGameBackendService')
->will($this->returnValue($gameService));


$this->serviceManager->setService('small_user_service', $userService);

$entity = new User();
$password = 'foobar';
$bCrypt = new Bcrypt();

$entity->setPassword($bCrypt->create( $password ));
$result = User::hashPassword($entity, $password);

$this->assertFalse($result);
}


}
2 changes: 2 additions & 0 deletions tests/PServerCMSTest/Util/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PServerCMSTest\Util;

use PHPUnit_Framework_TestCase as TestCase;
use PServerCMS\Service\ServiceManager;

class TestBase extends TestCase
{
Expand All @@ -16,6 +17,7 @@ public function setUp()
{
parent::setUp();
$this->serviceManager = ServiceManagerFactory::getServiceManager();
ServiceManager::setInstance($this->serviceManager);
}

/**
Expand Down

0 comments on commit 64e5364

Please sign in to comment.