Completed
Push — master ( 3f58ef...2b3071 )
by Yann
02:01
created

DoctrineORMTokenRepositoryTest::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\Repository;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Yokai\SecurityTokenBundle\Entity\Token;
9
use Yokai\SecurityTokenBundle\Repository\DoctrineORMTokenRepository;
10
11
/**
12
 * @author Yann Eugoné <[email protected]>
13
 */
14
class DoctrineORMTokenRepositoryTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var EntityManager|ObjectProphecy
18
     */
19
    private $manager;
20
21
    /**
22
     * @var EntityRepository|ObjectProphecy
23
     */
24
    private $repository;
25
26
    protected function setUp()
27
    {
28
        $this->manager = $this->prophesize(EntityManager::class);
29
        $this->repository = $this->prophesize(EntityRepository::class);
30
    }
31
32
    protected function tearDown()
33
    {
34
        unset(
35
            $this->manager,
36
            $this->repository
37
        );
38
    }
39
40
    protected function repository()
41
    {
42
        return new DoctrineORMTokenRepository($this->manager->reveal(), $this->repository->reveal());
43
    }
44
45
    /**
46
     * @test
47
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenNotFoundException
48
     */
49
    public function it_throw_exception_if_token_not_found()
50
    {
51
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
52
            ->shouldBeCalledTimes(1)
53
            ->willReturn(null);
54
55
        $this->repository()->get('unique', 'init_password');
56
    }
57
58
    /**
59
     * @test
60
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenExpiredException
61
     */
62
    public function it_throw_exception_if_token_expired()
63
    {
64
        $token = new Token('string', 'jdoe','unique', 'init_password',  '-1 day', []);
65
66
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
67
            ->shouldBeCalledTimes(1)
68
            ->willReturn($token);
69
70
        $this->repository()->get('unique', 'init_password');
71
    }
72
73
    /**
74
     * @test
75
     * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenUsedException
76
     */
77
    public function it_throw_exception_if_token_used()
78
    {
79
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
80
        $token->setUsedAt(new \DateTime());
81
        $token->setUsedInformation(['info']);
82
83
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
84
            ->shouldBeCalledTimes(1)
85
            ->willReturn($token);
86
87
        $this->repository()->get('unique', 'init_password');
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function it_get_valid_token()
94
    {
95
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
96
97
        $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password'])
98
            ->shouldBeCalledTimes(1)
99
            ->willReturn($token);
100
101
        $got = $this->repository()->get('unique', 'init_password');
102
103
        self::assertSame($token, $got);
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function it_create_token()
110
    {
111
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
112
113
        $this->manager->persist($token)
114
            ->shouldBeCalledTimes(1);
115
        $this->manager->flush($token)
116
            ->shouldBeCalledTimes(1);
117
118
        $this->repository()->create($token);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function it_update_token()
125
    {
126
        $token = new Token('string', 'jdoe','unique', 'init_password',  '+1 day', []);
127
128
        $this->manager->persist($token)
129
            ->shouldBeCalledTimes(1);
130
        $this->manager->flush($token)
131
            ->shouldBeCalledTimes(1);
132
133
        $this->repository()->update($token);
134
    }
135
}
136