Completed
Push — issue#666 ( 3afa36...5bb037 )
by Guilherme
03:30
created

testOnRemoteClaimNotFound()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 30
rs 8.8571
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\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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimFetcherInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\RemoteClaim...teClaimFetcherInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Log\LoggerInterface.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OpenIDBundle\Event\AuthorizationEvent.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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()
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...
93
    {
94
        $remoteClaim = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface');
95
96
        return $remoteClaim;
97
    }
98
}
99