Completed
Pull Request — master (#7)
by Yann
05:59
created

TokenManagerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 129
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 9 1
A manager() 0 9 1
A it_get_token_from_repository() 0 10 1
B it_create_unique_token() 0 25 1
A it_consume_token() 0 16 1
A it_extract_user_from_token() 0 12 1
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Tests\Manager;
4
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Yokai\SecurityTokenBundle\Entity\Token;
8
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface;
9
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
10
use Yokai\SecurityTokenBundle\Manager\TokenManager;
11
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface;
12
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface;
13
14
/**
15
 * @author Yann Eugoné <[email protected]>
16
 */
17
class TokenManagerTest extends TestCase
18
{
19
    /**
20
     * @var \PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $factory;
23
24
    /**
25
     * @var TokenRepositoryInterface|ObjectProphecy
26
     */
27
    private $repository;
28
29
    /**
30
     * @var InformationGuesserInterface|ObjectProphecy
31
     */
32
    private $informationGuesser;
33
34
    /**
35
     * @var UserManagerInterface|ObjectProphecy
36
     */
37
    private $userManager;
38
39
    protected function setUp()
40
    {
41
        $this->factory = $this->createMock(TokenFactoryInterface::class);
42
        $this->repository = $this->prophesize(TokenRepositoryInterface::class);
43
        $this->informationGuesser = $this->prophesize(InformationGuesserInterface::class);
44
        $this->userManager = $this->prophesize(UserManagerInterface::class);
45
    }
46
47
    protected function tearDown()
48
    {
49
        unset(
50
            $this->factory,
51
            $this->repository,
52
            $this->informationGuesser,
53
            $this->userManager
54
        );
55
    }
56
57
    protected function manager()
58
    {
59
        return new TokenManager(
60
            $this->factory,
61
            $this->repository->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Yokai\SecurityTokenBundl...okenRepositoryInterface.

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...
62
            $this->informationGuesser->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Yokai\SecurityTokenBundl...rmationGuesserInterface.

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...
63
            $this->userManager->reveal()
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Yokai\SecurityTokenBundl...er\UserManagerInterface.

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...
64
        );
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function it_get_token_from_repository()
71
    {
72
        $this->repository->get('unique-token', 'forgot_password')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\SecurityTokenBundl...okenRepositoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
73
            ->shouldBeCalledTimes(1)
74
            ->willReturn($expected = $this->prophesize(Token::class)->reveal());
75
76
        $token = $this->manager()->get('forgot_password', 'unique-token');
77
78
        self::assertSame($expected, $token);
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function it_create_unique_token()
85
    {
86
        $token1 = new Token('string', 'jdoe','unique-token-1', 'reset-password',  '+1 day', []);
87
        $token2 = new Token('string', 'jdoe','unique-token-2', 'reset-password',  '+1 day', []);
88
89
        $this->factory->expects($this->exactly(2))
90
                      ->method('create')
91
                      ->will($this->onConsecutiveCalls($token1, $token2));
92
93
        $this->repository->exists('unique-token-1', 'forgot_password')
0 ignored issues
show
Bug introduced by
The method exists does only exist in Yokai\SecurityTokenBundl...okenRepositoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
94
            ->shouldBeCalledTimes(1)
95
            ->willReturn(true);
96
        $this->repository->exists('unique-token-2', 'forgot_password')
97
            ->shouldBeCalledTimes(1)
98
            ->willReturn(false);
99
100
        $this->repository->create($token1)
0 ignored issues
show
Bug introduced by
The method create does only exist in Yokai\SecurityTokenBundl...okenRepositoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
101
            ->shouldNotBeCalled();
102
        $this->repository->create($token2)
103
            ->shouldBeCalledTimes(1);
104
105
        $token = $this->manager()->create('forgot_password', 'john-doe');
106
107
        self::assertSame($token2, $token);
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function it_consume_token()
114
    {
115
        $token = new Token('string', 'jdoe','unique-token', 'reset-password',  '+1 day', []);
116
117
        $this->informationGuesser->get()
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\SecurityTokenBundl...rmationGuesserInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
118
            ->shouldBeCalledTimes(1)
119
            ->willReturn(['some', 'precious', 'information']);
120
121
        $this->repository->update($token)
0 ignored issues
show
Bug introduced by
The method update does only exist in Yokai\SecurityTokenBundl...okenRepositoryInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
122
            ->shouldBeCalledTimes(1);
123
124
        $this->manager()->setUsed($token);
125
126
        self::assertSame(['some', 'precious', 'information'], $token->getUsedInformation());
127
        self::assertInstanceOf(\DateTime::class, $token->getUsedAt());
128
    }
129
130
    /**
131
     * @test
132
     */
133
    public function it_extract_user_from_token()
134
    {
135
        $token = new Token('string', 'jdoe','unique-token', 'reset-password',  '+1 day', []);
136
137
        $this->userManager->get('string', 'jdoe')
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\SecurityTokenBundl...er\UserManagerInterface, but not in Prophecy\Prophecy\ObjectProphecy.

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...
138
            ->shouldBeCalledTimes(1)
139
            ->willReturn('john doe');
140
141
        $user = $this->manager()->getUser($token);
142
143
        self::assertSame('john doe', $user);
144
    }
145
}
146