|
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\RemoteClaimsBundle\Tests\EventSubscriber; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OpenIDBundle\Event\AuthorizationEvent; |
|
14
|
|
|
use LoginCidadao\OpenIDBundle\LoginCidadaoOpenIDEvents; |
|
15
|
|
|
use LoginCidadao\RemoteClaimsBundle\EventSubscriber\AuthorizationSubscriber; |
|
16
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Psr\Log\LogLevel; |
|
19
|
|
|
|
|
20
|
|
|
class AuthorizationSubscriberTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testGetSubscribedEvents() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->assertEquals( |
|
25
|
|
|
[LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION_REQUEST => 'onNewAuthorizationRequest'], |
|
26
|
|
|
AuthorizationSubscriber::getSubscribedEvents() |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testOnNewAuthorizationRequest() |
|
31
|
|
|
{ |
|
32
|
|
|
$scope = [ |
|
33
|
|
|
'openid', |
|
34
|
|
|
'simple_claim', |
|
35
|
|
|
'https://claim.provider.example.com/my-claim', |
|
36
|
|
|
'tag:example.com,2017:my_claim', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
$remoteClaims = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|RemoteClaimFetcherInterface $fetcher */ |
|
42
|
|
|
$fetcher = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface'); |
|
43
|
|
|
$fetcher->expects($this->atLeastOnce())->method('getRemoteClaim') |
|
|
|
|
|
|
44
|
|
|
->willReturnCallback(function ($scope) use (&$remoteClaims) { |
|
45
|
|
|
$remoteClaims[] = $scope; |
|
46
|
|
|
|
|
47
|
|
|
return $this->getRemoteClaim(); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|AuthorizationEvent $event */ |
|
51
|
|
|
$event = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Event\AuthorizationEvent') |
|
52
|
|
|
->disableOriginalConstructor()->getMock(); |
|
53
|
|
|
$event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$subscriber = new AuthorizationSubscriber($fetcher); |
|
56
|
|
|
$subscriber->onNewAuthorizationRequest($event); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertCount(2, $remoteClaims); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testOnRemoteClaimNotFound() |
|
62
|
|
|
{ |
|
63
|
|
|
$scope = [ |
|
64
|
|
|
'openid', |
|
65
|
|
|
'simple_claim', |
|
66
|
|
|
'https://not.actually.a.remote.claim/fake', |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
$remoteClaims = []; |
|
70
|
|
|
|
|
71
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|RemoteClaimFetcherInterface $fetcher */ |
|
72
|
|
|
$fetcher = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface'); |
|
73
|
|
|
$fetcher->expects($this->atLeastOnce())->method('getRemoteClaim') |
|
|
|
|
|
|
74
|
|
|
->willThrowException(new \RuntimeException('Random error')); |
|
75
|
|
|
|
|
76
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|LoggerInterface $logger */ |
|
77
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
78
|
|
|
$logger->expects($this->once())->method('log')->with(LogLevel::ERROR); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|AuthorizationEvent $event */ |
|
81
|
|
|
$event = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Event\AuthorizationEvent') |
|
82
|
|
|
->disableOriginalConstructor()->getMock(); |
|
83
|
|
|
$event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$subscriber = new AuthorizationSubscriber($fetcher); |
|
86
|
|
|
$subscriber->setLogger($logger); |
|
87
|
|
|
$subscriber->onNewAuthorizationRequest($event); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEmpty($remoteClaims); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getRemoteClaim() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$remoteClaim = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface'); |
|
95
|
|
|
|
|
96
|
|
|
return $remoteClaim; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: