Completed
Push — master ( 3e963f...fbd0cf )
by Yann
02:04
created

TokenFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Tests\Factory;
4
5
use DateTime;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Yokai\SecurityTokenBundle\Configuration\TokenConfiguration;
8
use Yokai\SecurityTokenBundle\Configuration\TokenConfigurationRegistry;
9
use Yokai\SecurityTokenBundle\Entity\Token;
10
use Yokai\SecurityTokenBundle\Factory\TokenFactory;
11
use Yokai\SecurityTokenBundle\Generator\TokenGeneratorInterface;
12
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
13
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface;
14
15
/**
16
 * @author Yann Eugoné <[email protected]>
17
 */
18
class TokenFactoryTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var InformationGuesserInterface|ObjectProphecy
22
     */
23
    private $informationGuesser;
24
25
    /**
26
     * @var UserManagerInterface|ObjectProphecy
27
     */
28
    private $userManager;
29
30
    protected function setUp()
31
    {
32
        $this->informationGuesser = $this->prophesize(InformationGuesserInterface::class);
33
        $this->userManager = $this->prophesize(UserManagerInterface::class);
34
    }
35
36
    protected function tearDown()
37
    {
38
        unset(
39
            $this->informationGuesser,
40
            $this->userManager
41
        );
42
    }
43
44
    protected function factory(array $configuration)
45
    {
46
        return new TokenFactory(
47
            new TokenConfigurationRegistry($configuration),
48
            $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...
49
            $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...
50
        );
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function it_create_token_according_to_configuration()
57
    {
58
        /** @var TokenGeneratorInterface|ObjectProphecy $generator */
59
        $generator1 = $this->prophesize(TokenGeneratorInterface::class);
60
        $generator1->generate()
61
            ->shouldBeCalledTimes(1)
62
            ->willReturn('uniquetoken-1');
63
        $user1 = 'user-1';
64
65
        /** @var TokenGeneratorInterface|ObjectProphecy $generator */
66
        $generator2 = $this->prophesize(TokenGeneratorInterface::class);
67
        $generator2->generate()
68
            ->shouldBeCalledTimes(1)
69
            ->willReturn('uniquetoken-2');
70
        $user2 = 'user-2';
71
72
        $configuration = [
73
            new TokenConfiguration('test-1', $generator1->reveal(), '+1 minute'),
74
            new TokenConfiguration('test-2', $generator2->reveal(), '+2 minute'),
75
        ];
76
77
        $this->userManager->getClass($user1)
0 ignored issues
show
Bug introduced by
The method getClass 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...
78
            ->shouldBeCalledTimes(1)
79
            ->willReturn('string');
80
        $this->userManager->getClass($user2)
81
            ->shouldBeCalledTimes(1)
82
            ->willReturn('string');
83
84
        $this->userManager->getId($user1)
0 ignored issues
show
Bug introduced by
The method getId 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...
85
            ->shouldBeCalledTimes(1)
86
            ->willReturn('u1');
87
        $this->userManager->getId($user2)
88
            ->shouldBeCalledTimes(1)
89
            ->willReturn('u2');
90
91
        $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...
92
            ->shouldBeCalledTimes(2)
93
            ->willReturn(['some', 'precious', 'information']);
94
95
        $token1 = $this->factory($configuration)->create($user1, 'test-1');
96
97
        self::assertInstanceOf(Token::class, $token1);
98
        self::assertSame('string', $token1->getUserClass());
99
        self::assertSame('u1', $token1->getUserId());
100
        self::assertSame(['some', 'precious', 'information'], $token1->getCreatedInformation());
101
        self::assertSame('test-1', $token1->getPurpose());
102
        self::assertSame('uniquetoken-1', $token1->getValue());
103
        self::assertInstanceOf(DateTime::class, $token1->getCreatedAt());
104
        self::assertInstanceOf(DateTime::class, $token1->getExpiresAt());
105
        self::assertNull($token1->getUsedAt());
106
        self::assertNull($token1->getUsedInformation());
107
108
        $token2 = $this->factory($configuration)->create($user2, 'test-2');
109
110
        self::assertInstanceOf(Token::class, $token2);
111
        self::assertSame('string', $token2->getUserClass());
112
        self::assertSame('u2', $token2->getUserId());
113
        self::assertSame(['some', 'precious', 'information'], $token2->getCreatedInformation());
114
        self::assertSame('test-2', $token2->getPurpose());
115
        self::assertSame('uniquetoken-2', $token2->getValue());
116
        self::assertInstanceOf(DateTime::class, $token2->getCreatedAt());
117
        self::assertInstanceOf(DateTime::class, $token2->getExpiresAt());
118
        self::assertNull($token2->getUsedAt());
119
        self::assertNull($token2->getUsedInformation());
120
    }
121
}
122