This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
AuthCodeRepositoryTest.php
83 lines (68 loc) · 3.36 KB
/
AuthCodeRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/
declare(strict_types=1);
namespace ZendTest\Expressive\Authentication\OAuth2\Repository\Pdo;
use DateTime;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use PDOStatement;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Zend\Expressive\Authentication\OAuth2\Entity\AuthCodeEntity;
use Zend\Expressive\Authentication\OAuth2\Repository\Pdo\AuthCodeRepository;
use Zend\Expressive\Authentication\OAuth2\Repository\Pdo\PdoService;
use function time;
class AuthCodeRepositoryTest extends TestCase
{
public function setUp()
{
$this->pdo = $this->prophesize(PdoService::class);
$this->repo = new AuthCodeRepository($this->pdo->reveal());
}
public function testPeristNewAuthCodeRaisesExceptionWhenStatementExecutionFails()
{
$client = $this->prophesize(ClientEntityInterface::class);
$client->getIdentifier()->willReturn('client_id');
$scope = $this->prophesize(ScopeEntityInterface::class);
$scope->getIdentifier()->willReturn('authentication');
$time = time();
$date = $this->prophesize(DateTime::class);
$date->getTimestamp()->willReturn($time);
$authCode = $this->prophesize(AuthCodeEntity::class);
$authCode->getIdentifier()->willReturn('id');
$authCode->getUserIdentifier()->willReturn('user_id');
$authCode->getClient()->will([$client, 'reveal']);
$authCode->getScopes()->willReturn([$scope->reveal()]);
$authCode->getExpiryDateTime()->will([$date, 'reveal']);
$statement = $this->prophesize(PDOStatement::class);
$statement->bindValue(':id', 'id')->shouldBeCalled();
$statement->bindValue(':user_id', 'user_id')->shouldBeCalled();
$statement->bindValue(':client_id', 'client_id')->shouldBeCalled();
$statement->bindValue(':scopes', 'authentication')->shouldBeCalled();
$statement->bindValue(':revoked', false)->shouldBeCalled();
$statement->bindValue(':expires_at', $time)->shouldBeCalled();
$statement->execute()->willReturn(false);
$this->pdo
->prepare(Argument::containingString('INSERT INTO oauth_auth_codes'))
->will([$statement, 'reveal']);
$this->expectException(UniqueTokenIdentifierConstraintViolationException::class);
$this->repo->persistNewAuthCode($authCode->reveal());
}
public function testIsAuthCodeRevokedReturnsFalseForStatementExecutionFailure()
{
$statement = $this->prophesize(PDOStatement::class);
$statement->bindParam(':codeId', 'code_identifier')->shouldBeCalled();
$statement->execute()->willReturn(false);
$statement->fetch()->shouldNotBeCalled();
$this->pdo
->prepare(Argument::containingString('SELECT revoked FROM oauth_auth_codes'))
->will([$statement, 'reveal']);
$this->assertFalse($this->repo->isAuthCodeRevoked('code_identifier'));
}
}