|
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\Validator\Constraints; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OAuthBundle\Entity\Organization; |
|
14
|
|
|
use LoginCidadao\OAuthBundle\Entity\OrganizationRepository; |
|
15
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\Validator\Constraints\SectorIdentifierUri; |
|
17
|
|
|
use LoginCidadao\OpenIDBundle\Validator\Constraints\SectorIdentifierUriValidator; |
|
18
|
|
|
use LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker; |
|
19
|
|
|
|
|
20
|
|
|
class SectorIdentifierUriValidatorTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testValidate() |
|
23
|
|
|
{ |
|
24
|
|
|
$uri = 'https://example.com'; |
|
25
|
|
|
$metadata = (new ClientMetadata()) |
|
26
|
|
|
->setSectorIdentifierUri($uri); |
|
27
|
|
|
$constraint = new SectorIdentifierUri(); |
|
28
|
|
|
$organization = new Organization(); |
|
29
|
|
|
|
|
30
|
|
|
/** @var OrganizationRepository|\PHPUnit_Framework_MockObject_MockObject $repo */ |
|
31
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\OrganizationRepository') |
|
32
|
|
|
->disableOriginalConstructor()->getMock(); |
|
33
|
|
|
$repo->expects($this->once()) |
|
|
|
|
|
|
34
|
|
|
->method('findOneBy')->with(['sectorIdentifierUri' => $uri]) |
|
35
|
|
|
->willReturn($organization); |
|
36
|
|
|
|
|
37
|
|
|
/** @var SectorIdentifierUriChecker|\PHPUnit_Framework_MockObject_MockObject $uriChecker */ |
|
38
|
|
|
$uriChecker = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker') |
|
39
|
|
|
->disableOriginalConstructor()->getMock(); |
|
40
|
|
|
$uriChecker->expects($this->once())->method('check')->with($metadata, $uri)->willReturn(true); |
|
41
|
|
|
|
|
42
|
|
|
$validator = new SectorIdentifierUriValidator($repo, $uriChecker); |
|
43
|
|
|
$validator->validate($metadata, $constraint); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertSame($organization, $metadata->getOrganization()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testNoUri() |
|
49
|
|
|
{ |
|
50
|
|
|
$metadata = new ClientMetadata(); |
|
51
|
|
|
$constraint = new SectorIdentifierUri(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var OrganizationRepository|\PHPUnit_Framework_MockObject_MockObject $repo */ |
|
54
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\OrganizationRepository') |
|
55
|
|
|
->disableOriginalConstructor()->getMock(); |
|
56
|
|
|
$repo->expects($this->never())->method('findOneBy'); |
|
57
|
|
|
|
|
58
|
|
|
/** @var SectorIdentifierUriChecker|\PHPUnit_Framework_MockObject_MockObject $uriChecker */ |
|
59
|
|
|
$uriChecker = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker') |
|
60
|
|
|
->disableOriginalConstructor()->getMock(); |
|
61
|
|
|
$uriChecker->expects($this->never())->method('check'); |
|
62
|
|
|
|
|
63
|
|
|
$validator = new SectorIdentifierUriValidator($repo, $uriChecker); |
|
64
|
|
|
$validator->validate($metadata, $constraint); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertNull($metadata->getOrganization()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testCheckFailed() |
|
70
|
|
|
{ |
|
71
|
|
|
$uri = 'https://example.com'; |
|
72
|
|
|
$metadata = (new ClientMetadata()) |
|
73
|
|
|
->setSectorIdentifierUri($uri); |
|
74
|
|
|
$constraint = new SectorIdentifierUri(); |
|
75
|
|
|
$organization = new Organization(); |
|
76
|
|
|
|
|
77
|
|
|
/** @var OrganizationRepository|\PHPUnit_Framework_MockObject_MockObject $repo */ |
|
78
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\OrganizationRepository') |
|
79
|
|
|
->disableOriginalConstructor()->getMock(); |
|
80
|
|
|
$repo->expects($this->once()) |
|
81
|
|
|
->method('findOneBy')->with(['sectorIdentifierUri' => $uri]) |
|
82
|
|
|
->willReturn($organization); |
|
83
|
|
|
|
|
84
|
|
|
/** @var SectorIdentifierUriChecker|\PHPUnit_Framework_MockObject_MockObject $uriChecker */ |
|
85
|
|
|
$uriChecker = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker') |
|
86
|
|
|
->disableOriginalConstructor()->getMock(); |
|
87
|
|
|
$uriChecker->expects($this->once())->method('check')->with($metadata, $uri)->willReturn(false); |
|
88
|
|
|
|
|
89
|
|
|
$validator = new SectorIdentifierUriValidator($repo, $uriChecker); |
|
90
|
|
|
$validator->validate($metadata, $constraint); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertNull($metadata->getOrganization()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|