Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the generateId for userid was not autoinc; Added unit tests and…
… fixed some bugs
- Loading branch information
Showing
9 changed files
with
308 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
To change this license header, choose License Headers in Project Properties. | ||
To change this template file, choose Tools | Templates | ||
and open the template in the editor. | ||
--> | ||
|
||
<!-- see http://www.phpunit.de/wiki/Documentation --> | ||
<phpunit bootstrap="./vendor/autoload.php" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
stopOnFailure="true"> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace ByJG\Authenticate\Exception; | ||
|
||
class UserExistsException extends \Exception | ||
{ | ||
//put your code here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
namespace ByJG\Authenticate; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: jg | ||
* Date: 24/04/16 | ||
* Time: 20:21 | ||
*/ | ||
class UsersAnyDatasetTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var UsersAnyDataset | ||
*/ | ||
protected $object; | ||
|
||
protected $prefix = ""; | ||
|
||
public function setUp() | ||
{ | ||
$this->prefix = "user"; | ||
|
||
$this->object = new UsersAnyDataset('php://memory'); | ||
$this->object->addUser('User 1', 'user1', 'user1@gmail.com', 'pwd1'); | ||
$this->object->addUser('User 2', 'user2', 'user2@gmail.com', 'pwd2'); | ||
$this->object->addUser('User 3', 'user3', 'user3@gmail.com', 'pwd3'); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
parent::tearDown(); // TODO: Change the autogenerated stub | ||
} | ||
|
||
public function testAddUser() | ||
{ | ||
$this->object->addUser('John Doe', 'john', 'johndoe@gmail.com', 'mypassword'); | ||
|
||
$user = $this->object->getByUsername('john'); | ||
$this->assertEquals('john', $user->getField($this->object->getUserTable()->id)); | ||
$this->assertEquals('John Doe', $user->getField($this->object->getUserTable()->name)); | ||
$this->assertEquals('john', $user->getField($this->object->getUserTable()->username)); | ||
$this->assertEquals('johndoe@gmail.com', $user->getField($this->object->getUserTable()->email)); | ||
$this->assertEquals('91DFD9DDB4198AFFC5C194CD8CE6D338FDE470E2', $user->getField($this->object->getUserTable()->password)); | ||
} | ||
|
||
/** | ||
* @expectedException \ByJG\Authenticate\Exception\UserExistsException | ||
*/ | ||
public function testAddUserError() | ||
{ | ||
$this->object->addUser('some user with same username', 'user2', 'bla@gmail.com', 'mypassword'); | ||
} | ||
|
||
public function testAddUser_generatedId() | ||
{ | ||
$mock = $this->getMockBuilder('\ByJG\Authenticate\UsersAnyDataset') | ||
->setMethods( [ 'generateUserId' ] ) | ||
->setConstructorArgs( [null] ) | ||
->getMock(); | ||
|
||
$mock->expects($this->once()) | ||
->method('generateUserId') | ||
->will($this->returnValue(1234)); | ||
|
||
$mock->addUser('John Doe', 'john', 'johndoe@gmail.com', 'mypassword'); | ||
|
||
$user = $mock->getByUsername('john'); | ||
$this->assertEquals('1234', $user->getField($this->object->getUserTable()->id)); | ||
$this->assertEquals('John Doe', $user->getField($this->object->getUserTable()->name)); | ||
$this->assertEquals('john', $user->getField($this->object->getUserTable()->username)); | ||
$this->assertEquals('johndoe@gmail.com', $user->getField($this->object->getUserTable()->email)); | ||
$this->assertEquals('91DFD9DDB4198AFFC5C194CD8CE6D338FDE470E2', $user->getField($this->object->getUserTable()->password)); | ||
} | ||
|
||
public function testAddProperty() | ||
{ | ||
// Add one property | ||
$this->object->addProperty($this->prefix . '2', 'city', 'Rio de Janeiro'); | ||
$user = $this->object->getByUsername('user2'); | ||
$this->assertEquals('Rio de Janeiro', $user->getField('city')); | ||
|
||
// Add another property (cannot change) | ||
$this->object->addProperty($this->prefix . '2', 'city', 'Belo Horizonte'); | ||
$user = $this->object->getByUsername('user2'); | ||
$this->assertEquals('Rio de Janeiro', $user->getField('city')); | ||
|
||
// Get Property | ||
$this->assertEquals(['Rio de Janeiro', 'Belo Horizonte'], $this->object->getProperty($this->prefix . '2', 'city')); | ||
|
||
// Add another property | ||
$this->object->addProperty($this->prefix . '2', 'state', 'RJ'); | ||
$user = $this->object->getByUsername('user2'); | ||
$this->assertEquals('RJ', $user->getField('state')); | ||
|
||
// Remove Property | ||
$this->object->removeProperty($this->prefix . '2', 'state', 'RJ'); | ||
$user = $this->object->getByUsername('user2'); | ||
$this->assertEmpty($user->getField('state')); | ||
|
||
// Remove Property Again | ||
$this->object->removeProperty($this->prefix . '2', 'city', 'Rio de Janeiro'); | ||
$this->assertEquals(['Belo Horizonte'], $this->object->getProperty($this->prefix . '2', 'city')); | ||
|
||
} | ||
|
||
public function testRemoveUserName() | ||
{ | ||
$user = $this->object->getByUsername('user1'); | ||
$this->assertNotNull($user); | ||
|
||
$this->object->removeUserName('user1'); | ||
$user = $this->object->getByUsername('user1'); | ||
$this->assertNull($user); | ||
|
||
} | ||
|
||
public function testEditUser() | ||
{ | ||
// Getting data | ||
$user = $this->object->getByUsername('user1'); | ||
$this->assertEquals('User 1', $user->getField($this->object->getUserTable()->name)); | ||
|
||
// Change and Persist data | ||
$user->setField($this->object->getUserTable()->name, 'Other name'); | ||
$this->object->save(); | ||
|
||
// Check if data persists | ||
$user = $this->object->getById($this->prefix . '1'); | ||
$this->assertEquals('Other name', $user->getField($this->object->getUserTable()->name)); | ||
} | ||
|
||
public function testIsValidUser() | ||
{ | ||
// User Exists! | ||
$user = $this->object->isValidUser('user3', 'pwd3'); | ||
$this->assertEquals('User 3', $user->getField($this->object->getUserTable()->name)); | ||
|
||
// User Does not Exists! | ||
$user = $this->object->isValidUser('user55', 'pwd5'); | ||
$this->assertNull($user); | ||
} | ||
|
||
public function testIsAdmin() | ||
{ | ||
// Set the Admin Flag | ||
$user = $this->object->getByUsername('user3'); | ||
$user->setField($this->object->getUserTable()->admin, 'Y'); | ||
$this->object->save(); | ||
|
||
// Check is Admin | ||
$this->assertTrue($this->object->isAdmin($this->prefix . '3')); | ||
|
||
|
||
} | ||
|
||
public function testCreateAuthToken() | ||
{ | ||
|
||
} | ||
} |
Oops, something went wrong.