KeyTest::ttl_must_be_positive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 3
c 3
b 0
f 1
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Zenstruck\Governator\Tests\Unit;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\Governator\Key;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class KeyTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @dataProvider invalidNumberProvider
16
     */
17
    public function ttl_must_be_positive($number): void
18
    {
19
        $this->expectException(\InvalidArgumentException::class);
20
        $this->expectExceptionMessage('A positive number is required for a throttle\'s "time to live".');
21
22
        new Key('foo', 5, $number);
23
    }
24
25
    /**
26
     * @test
27
     * @dataProvider invalidNumberProvider
28
     */
29
    public function limit_must_be_positive($number): void
30
    {
31
        $this->expectException(\InvalidArgumentException::class);
32
        $this->expectExceptionMessage('A positive integer is required for a throttle\'s "limit".');
33
34
        new Key('foo', $number, 60);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function resource_must_not_be_empty(): void
41
    {
42
        $this->expectException(\InvalidArgumentException::class);
43
        $this->expectExceptionMessage('A non-empty string is required for a throttle\'s "resource".');
44
45
        new Key('', 5, 60);
46
    }
47
48
    public static function invalidNumberProvider(): iterable
49
    {
50
        yield [0];
51
        yield [-1];
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function can_access_resource(): void
58
    {
59
        $this->assertSame('foo', (new Key('foo', 10, 60))->resource());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function resource_does_not_include_prefix(): void
66
    {
67
        $this->assertSame('foo', (new Key('foo', 10, 60, 'my-prefix-'))->resource());
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function can_convert_to_string(): void
74
    {
75
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
76
77
        $this->assertSame($resource.'1060', (string) new Key('foo', 10, 60));
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function converting_to_string_includes_prefix(): void
84
    {
85
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
86
87
        $this->assertSame("my-prefix-{$resource}1060", (string) new Key('foo', 10, 60, 'my-prefix-'));
88
    }
89
}
90