Skip to content

Instantly share code, notes, and snippets.

@alihammad-gist
Created February 1, 2015 14:56
Show Gist options
  • Save alihammad-gist/c6b2e650690638a3783b to your computer and use it in GitHub Desktop.
Save alihammad-gist/c6b2e650690638a3783b to your computer and use it in GitHub Desktop.
Line 181 of LoginUserTest.php produces an error for phpunit
PHPUnit 4.4.4 by Sebastian Bergmann.
Configuration read from /****/phpunit.xml
............E
Time: 82 ms, Memory: 3.00Mb
There was 1 error:
1) PostitTest\Interactor\LoginUserTest::testInvoke_DbException_Returns_InternalError
Postit\Db\Exception:
/***/LoginUserTest.php:181
FAILURES!
Tests: 13, Assertions: 38, Errors: 1.
<?php
namespace PostitTest\Interactor;
use Postit\Request\LoginUser as Request;
use Postit\Response\LoginUser as Response;
use Postit\Interactor\LoginUser;
use Postit\Entity\User as UserEntity;
use Postit\Error;
use Postit\Exception\UserNotFound;
use Postit\Db\Exception as DbException;
class LoginUserTest extends \PHPUnit_Framework_TestCase
{
// normal username password login (social login downstairs)
public function testInvoke_normal_proceedure_username_password()
{
// returned user from repo
$user = new UserEntity(
'username123',
'hashedPassword',
null,
null,
null,
null
);
$user->setId(1);
$username = 'username';
$password = 'password';
// request must contain username and password
$req = new Request(
$username,
$password,
null,
null
);
$repoStub = $this->getMockBuilder('Postit\Db\UserRepo')
->getMock();
// fetchByUsername will be called with provided username and password
$repoStub->expects($this->once())
->method('fetchByUsername')
->with($username)
->will($this->returnValue($user));
$pwdEnc = $this->getMockBuilder('Postit\Crypto\PasswordEncoder')
->getMock();
$pwdEnc->expects($this->once())
->method('isValid')
->with($user->getPassword())
->will($this->returnValue(true));
$loginUser = new LoginUser($repoStub, $pwdEnc);
$resp = $loginUser($req);
$this->assertTrue($resp instanceof Response);
$this->assertEquals($resp->username, $user->getUsername());
}
public function testInvoke_invalid_username()
{
$un = 'username';
$repoStub = $this->getMockBuilder('Postit\Db\UserRepo')
->getMock();
$repoStub->expects($this->once())
->method('fetchByUsername')
->with($un)
->will($this->throwException(new UserNotFound));
$loginUser = new LoginUser(
$repoStub,
$this->getMock('Postit\Crypto\PasswordEncoder')
);
$resp = $loginUser(new Request(
'username',
'password',
null,
null
));
$this->assertTrue($resp instanceof Error\Request);
}
public function testInvoke_invalid_password()
{
$un = 'username';
$pd = 'password';
$u = new UserEntity(
'username',
'hashedPassword',
null,
null,
null,
null
);
$req = new Request(
'username',
'password',
null,
null
);
$repoStub = $this->getMock('Postit\Db\UserRepo');
$repoStub->expects($this->once())
->method('fetchByUsername')
->with($un)
->will($this->returnValue($u));
$pwdEcnStub = $this->getMock('Postit\Crypto\PasswordEncoder');
$pwdEcnStub->expects($this->once())
->method('isValid')
->with($u->getPassword())
->will($this->returnValue(false));
$loginUser = new LoginUser($repoStub, $pwdEcnStub);
$resp = $loginUser($req);
$this->assertTrue($resp instanceof Error\Request);
}
// social login (hybridauth)
public function testInvoke_normal_proceedure_hybridAuth()
{
$req = new Request(
null,
null,
'Twitter',
1231231
);
$u = new UserEntity(
'username', null, null, null, null, null
);
$repoStub = $this->getMock('Postit\Db\UserRepo');
$repoStub->expects($this->once())
->method('fetchSocialUser')
->with($req->hauthProvider, $req->hauthIdentifier)
->will($this->returnValue($u));
$loginUser = new LoginUser($repoStub, $this->getMock('Postit\Crypto\PasswordEncoder'));
$resp = $loginUser($req);
$this->assertTrue($resp instanceof Response);
}
public function testInvoke_invalid_hauthProviderId_hybridAuth()
{
$req = new Request(
null,
null,
'Facebook',
123213
);
$repoStub = $this->getMock('Postit\Db\UserRepo');
$repoStub->expects($this->once())
->method('fetchSocialUser')
->with($req->hauthProvider, $req->hauthIdentifier)
->will($this->throwException(new UserNotFound));
$loginUser = new LoginUser($repoStub, $this->getMock('Postit\Crypto\PasswordEncoder'));
$resp = $loginUser($req);
$this->assertTrue($resp instanceof Error\Request);
}
public function testInvoke_DbException_Returns_InternalError()
{
$req = new Request(
null,
null,
'Facebook',
123213
);
$repoStub = $this->getMock('Postit\Db\UserRepo');
$repoStub->expects($this->once())
->method('fetchSocialUser')
->with($req->hauthProvider, $req->hauthIdentifier)
->will($this->throwException(new DbException));
$loginUser = new LoginUser($repoStub, $this->getMock('Postit\Crypto\PasswordEncoder'));
$resp = $loginUser($req);
$this->assertTrue($resp instanceof Error\Internal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment