ThrottleFactoryTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 4
eloc 13
c 4
b 0
f 2
dl 0
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A construct_with_custom_prefix() 0 6 1
A for_with_default_prefix() 0 6 1
A for_with_custom_prefix() 0 6 1
A construct_with_default_prefix() 0 6 1
1
<?php
2
3
namespace Zenstruck\Governator\Tests\Unit;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\Governator\Store\MemoryStore;
7
use Zenstruck\Governator\ThrottleFactory;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
final class ThrottleFactoryTest extends TestCase
13
{
14
    /**
15
     * @test
16
     */
17
    public function construct_with_default_prefix(): void
18
    {
19
        $throttle = (new ThrottleFactory(new MemoryStore()))->create('foo', 5, 60);
20
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
21
22
        $this->assertSame("throttle_{$resource}560", (string) $throttle->acquire()->key());
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function for_with_default_prefix(): void
29
    {
30
        $throttle = ThrottleFactory::for('memory')->create('foo', 5, 60);
31
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
32
33
        $this->assertSame("throttle_{$resource}560", (string) $throttle->acquire()->key());
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function construct_with_custom_prefix(): void
40
    {
41
        $throttle = (new ThrottleFactory(new MemoryStore(), 'my-prefix-'))->create('foo', 5, 60);
42
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
43
44
        $this->assertSame("my-prefix-{$resource}560", (string) $throttle->acquire()->key());
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function for_with_custom_prefix(): void
51
    {
52
        $throttle = ThrottleFactory::for('memory', 'my-prefix-')->create('foo', 5, 60);
53
        $resource = \rtrim(\strtr(\base64_encode('foo'), '+/', '-_'), '=');
54
55
        $this->assertSame("my-prefix-{$resource}560", (string) $throttle->acquire()->key());
56
    }
57
}
58