Completed
Push — revert-symfony-336 ( 87e565 )
by Kamil
36:02
created

UniqueTokenGeneratorSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Component\User\Security\Generator;
15
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface;
18
use Sylius\Component\User\Security\Checker\UniquenessCheckerInterface;
19
use Sylius\Component\User\Security\Generator\GeneratorInterface;
20
use Sylius\Component\User\Security\Generator\UniqueTokenGenerator;
21
22
/**
23
 * @author Jan Góralski <[email protected]>
24
 */
25
final class UniqueTokenGeneratorSpec extends ObjectBehavior
26
{
27
    function let(RandomnessGeneratorInterface $generator, UniquenessCheckerInterface $checker)
28
    {
29
        $this->beConstructedWith($generator, $checker, 12);
30
    }
31
32
    function it_implements_generator_interface()
33
    {
34
        $this->shouldImplement(GeneratorInterface::class);
35
    }
36
37
    function it_throws_invalid_argument_exception_on_instantiation_with_non_integer_length(
38
        RandomnessGeneratorInterface $generator,
39
        UniquenessCheckerInterface $checker
40
    ) {
41
        $this->beConstructedWith($generator, $checker, 'a string');
42
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
43
        $this->beConstructedWith($generator, $checker, '12');
44
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
45
        $this->beConstructedWith($generator, $checker, []);
46
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
47
        $this->beConstructedWith($generator, $checker, new \StdClass());
48
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
49
    }
50
51
    function it_throws_invalid_argument_exception_on_instantiation_with_an_out_of_range_length(
52
        RandomnessGeneratorInterface $generator,
53
        UniquenessCheckerInterface $checker
54
    ) {
55
        $this->beConstructedWith($generator, $checker, -1);
56
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
57
        $this->beConstructedWith($generator, $checker, 0);
58
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
59
    }
60
61
    function it_generates_tokens_with_length_stated_on_instantiation(
62
        RandomnessGeneratorInterface $generator,
63
        UniquenessCheckerInterface $checker
64
    ) {
65
        $token = 'vanquishable';
66
67
        $generator->generateUriSafeString(12)->willReturn($token);
68
        $checker->isUnique($token)->willReturn(true);
69
70
        $this->generate()->shouldHaveLength(12);
71
    }
72
73
    function it_generates_string_tokens(RandomnessGeneratorInterface $generator, UniquenessCheckerInterface $checker)
74
    {
75
        $token = 'vanquishable';
76
77
        $generator->generateUriSafeString(12)->willReturn($token);
78
        $checker->isUnique($token)->willReturn(true);
79
80
        $this->generate()->shouldBeString();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getMatchers(): array
87
    {
88
        return [
89
            'haveLength' => function ($subject, $key) {
90
                return $key === strlen($subject);
91
            },
92
        ];
93
    }
94
}
95