Passed
Push — master ( 876065...f10f6d )
by Kevin
01:15
created

ThrottleBuilderTest::can_call_reset_directly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Governator\Tests\Unit;
4
5
use PHPUnit\Framework\TestCase;
6
use Zenstruck\Governator\Exception\QuotaExceeded;
7
use Zenstruck\Governator\Store\MemoryStore;
8
use Zenstruck\Governator\ThrottleBuilder;
9
use Zenstruck\Governator\ThrottleFactory;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ThrottleBuilderTest extends TestCase
15
{
16
    /**
17
     * @test
18
     */
19
    public function can_call_acquire_directly(): void
20
    {
21
        $builder = ThrottleFactory::for('memory')->throttle('foo')->allow(2)->every(10);
22
23
        $builder->acquire();
24
        $builder->acquire();
25
26
        try {
27
            $builder->acquire();
28
        } catch (QuotaExceeded $e) {
29
            $this->assertSame(3, $e->hits());
30
31
            return;
32
        }
33
34
        $this->fail('Exception not thrown');
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function can_call_reset_directly(): void
41
    {
42
        $builder = ThrottleFactory::for('memory')->throttle('foo')->allow(5)->every(10);
43
44
        $builder->acquire();
45
        $builder->acquire();
46
        $builder->reset();
47
48
        $this->assertSame(1, $builder->acquire()->hits());
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function can_call_status_directly(): void
55
    {
56
        $builder = ThrottleFactory::for('memory')->throttle('foo')->allow(2)->every(10);
57
58
        $builder->hit();
59
        $builder->hit();
60
        $builder->hit();
61
62
        $this->assertSame(3, $builder->status()->hits());
63
64
        try {
65
            $builder->status()->check();
66
        } catch (QuotaExceeded $e) {
67
            $this->assertSame(3, $e->hits());
68
69
            return;
70
        }
71
72
        $this->fail('Exception not thrown');
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function can_call_hit_directly(): void
79
    {
80
        $builder = ThrottleFactory::for('memory')->throttle('foo')->allow(2)->every(10);
81
82
        $builder->hit();
83
        $builder->hit();
84
        $quota = $builder->hit();
85
86
        $this->assertSame(3, $quota->hits());
87
        $this->assertTrue($quota->hasBeenExceeded());
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function multiple_resources_are_converted_to_string(): void
94
    {
95
        $key = ThrottleFactory::for('memory')
96
            ->throttle('a', 'b')
97
            ->with('c', 'd')
98
            ->with('e')
99
            ->allow(5)
100
            ->every(60)
101
            ->acquire()
102
            ->key()
103
        ;
104
105
        $this->assertSame('abcde', $key->resource());
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function limit_is_required(): void
112
    {
113
        $this->expectException(\LogicException::class);
114
        $this->expectExceptionMessage(\sprintf('You must set a "Limit" for the throttle via "%s::allow($limit)"', ThrottleBuilder::class));
115
116
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore()), 'foo'))->every(10)->create();
117
    }
118
119
    /**
120
     * @test
121
     */
122
    public function ttl_is_required(): void
123
    {
124
        $this->expectException(\LogicException::class);
125
        $this->expectExceptionMessage(\sprintf('You must set a "TTL" for the throttle via "%s::every($ttl)"', ThrottleBuilder::class));
126
127
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore()), 'foo'))->allow(10)->create();
128
    }
129
130
    /**
131
     * @test
132
     */
133
    public function resource_is_required(): void
134
    {
135
        $this->expectException(\LogicException::class);
136
        $this->expectExceptionMessage('The resource for the throttle cannot be blank.');
137
138
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore())))->allow(10)->every(60)->create();
139
    }
140
}
141