Completed
Pull Request — master (#7)
by Yann
02:14
created

TokenFactoryTest::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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