Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

testPublicSubjectIdentifierNullMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Tests\Service;
12
13
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
14
15
class SubjectIdentifierServiceTest extends \PHPUnit_Framework_TestCase
16
{
17
    private function getRepo()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
18
    {
19
        $repo = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository')
20
            ->disableOriginalConstructor()
21
            ->getMock();
22
23
        return $repo;
24
    }
25
26
    private function getPerson($id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
27
    {
28
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
29
        if ($id) {
30
            $person->expects($this->once())->method('getId')->willReturn($id);
31
        }
32
33
        return $person;
34
    }
35
36
    private function getClientMetadata($type = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
37
    {
38
        $metadata = $this->getMock('LoginCidadao\OpenIDBundle\Entity\ClientMetadata');
39
        if (!$type) {
40
            return $metadata;
41
        }
42
43
        $metadata->expects($this->once())->method('getSubjectType')->willReturn($type);
44
        if ($type === 'pairwise') {
45
            $metadata->expects($this->once())->method('getSectorIdentifier')
46
                ->willReturn('https://example.com/sector.identifier');
47
        }
48
49
        return $metadata;
50
    }
51
52
    public function testPairwiseSubjectIdentifier()
53
    {
54
        $id = 123456;
55
        $secret = 'my.secret';
56
57
        $repo = $this->getRepo();
58
        $person = $this->getPerson($id);
59
        $metadata = $this->getClientMetadata('pairwise');
60
61
        $service = new SubjectIdentifierService($repo, $secret);
62
        $sub = $service->getSubjectIdentifier($person, $metadata);
63
64
        $this->assertNotNull($sub);
65
        $this->assertNotEquals($id, $sub);
66
    }
67
68
    public function testPublicSubjectIdentifier()
69
    {
70
        $id = 654321;
71
        $secret = 'my.secret';
72
73
        $repo = $this->getRepo();
74
        $person = $this->getPerson($id);
75
        $metadata = $this->getClientMetadata('public');
76
77
        $service = new SubjectIdentifierService($repo, $secret);
78
        $sub = $service->getSubjectIdentifier($person, $metadata);
79
80
        $this->assertNotNull($sub);
81
        $this->assertEquals($id, $sub);
82
    }
83
84
    public function testPublicSubjectIdentifierNullMetadata()
85
    {
86
        $id = 654321;
87
        $secret = 'my.secret';
88
89
        $repo = $this->getRepo();
90
        $person = $this->getPerson($id);
91
92
        $service = new SubjectIdentifierService($repo, $secret);
93
        $sub = $service->getSubjectIdentifier($person, null);
94
95
        $this->assertNotNull($sub);
96
        $this->assertEquals($id, $sub);
97
    }
98
99
    public function testFetchSubjectIdentifier()
100
    {
101
        $id = 123456;
102
        $secret = 'my.secret';
103
        $expectedSub = 'my_sub_id';
104
105
        $subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier');
106
        $subId->expects($this->once())->method('getSubjectIdentifier')->willReturn($expectedSub);
107
108
        $repo = $this->getRepo();
109
        $repo->expects($this->once())->method('findOneBy')->willReturn($subId);
110
111
        $person = $this->getPerson();
112
        $metadata = $this->getClientMetadata();
113
114
        $service = new SubjectIdentifierService($repo, $secret);
115
        $sub = $service->getSubjectIdentifier($person, $metadata);
116
117
        $this->assertNotNull($sub);
118
        $this->assertNotEquals($id, $sub);
119
    }
120
121
    public function testIsSubjectIdentifierPersisted()
122
    {
123
        $secret = 'my.secret';
124
125
        $subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier');
126
        $client = $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface');
127
128
        $repo = $this->getRepo();
129
        $repo->expects($this->once())->method('findOneBy')->willReturn($subId);
130
131
        $service = new SubjectIdentifierService($repo, $secret);
132
133
        $this->assertTrue($service->isSubjectIdentifierPersisted($this->getPerson(), $client));
134
    }
135
}
136