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

it_generates_numeric_pins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
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 Prophecy\Argument;
18
use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface;
19
use Sylius\Component\User\Security\Checker\UniquenessCheckerInterface;
20
use Sylius\Component\User\Security\Generator\GeneratorInterface;
21
use Sylius\Component\User\Security\Generator\UniquePinGenerator;
22
23
/**
24
 * @author Jan Góralski <[email protected]>
25
 */
26
final class UniquePinGeneratorSpec extends ObjectBehavior
27
{
28
    function let(RandomnessGeneratorInterface $generator, UniquenessCheckerInterface $checker)
29
    {
30
        $this->beConstructedWith($generator, $checker, 6);
31
    }
32
33
    function it_implements_generator_interface()
34
    {
35
        $this->shouldImplement(GeneratorInterface::class);
36
    }
37
38
    function it_throws_invalid_argument_exception_on_instantiation_with_non_integer_length(
39
        RandomnessGeneratorInterface $generator,
40
        UniquenessCheckerInterface $checker
41
    ) {
42
        $this->beConstructedWith($generator, $checker, 'a string');
43
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
44
        $this->beConstructedWith($generator, $checker, '8');
45
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
46
        $this->beConstructedWith($generator, $checker, []);
47
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
48
        $this->beConstructedWith($generator, $checker, new \StdClass());
49
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
50
    }
51
52
    function it_throws_invalid_argument_exception_on_instantiation_with_an_out_of_range_length(
53
        RandomnessGeneratorInterface $generator,
54
        UniquenessCheckerInterface $checker
55
    ) {
56
        $this->beConstructedWith($generator, $checker, -1);
57
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
58
        $this->beConstructedWith($generator, $checker, 0);
59
        $this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
60
    }
61
62
    function it_generates_pins_with_length_stated_on_instantiation(
63
        RandomnessGeneratorInterface $generator,
64
        UniquenessCheckerInterface $checker
65
    ) {
66
        $pin = '001100';
67
68
        $generator->generateNumeric(6)->willReturn($pin);
69
        $checker->isUnique($pin)->willReturn(true);
70
71
        $this->generate()->shouldHaveLength(6);
72
    }
73
74
    function it_generates_string_pins(RandomnessGeneratorInterface $generator, UniquenessCheckerInterface $checker)
75
    {
76
        $pin = '636363';
77
78
        $generator->generateNumeric(6)->willReturn($pin);
79
        $checker->isUnique($pin)->willReturn(true);
80
81
        $this->generate()->shouldBeString();
82
    }
83
84
    function it_generates_numeric_pins(RandomnessGeneratorInterface $generator, UniquenessCheckerInterface $checker)
85
    {
86
        $pin = '424242';
87
88
        $generator->generateNumeric(6)->willReturn($pin);
89
        $checker->isUnique($pin)->willReturn(true);
90
91
        $this->generate()->shouldBeNumeric();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getMatchers(): array
98
    {
99
        return [
100
            'haveLength' => function ($subject, $key) {
101
                return $key === strlen($subject);
102
            },
103
        ];
104
    }
105
}
106