Passed
Push — master ( 7561a2...876065 )
by Kevin
01:33
created

ThrottleBuilderTest::resource_is_required()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
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\Store\MemoryStore;
7
use Zenstruck\Governator\ThrottleBuilder;
8
use Zenstruck\Governator\ThrottleFactory;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class ThrottleBuilderTest extends TestCase
14
{
15
    /**
16
     * @test
17
     */
18
    public function multiple_resources_are_converted_to_string(): void
19
    {
20
        $key = ThrottleFactory::for('memory')
21
            ->throttle('a', 'b')
22
            ->with('c', 'd')
23
            ->with('e')
24
            ->allow(5)
25
            ->every(60)
26
            ->hit()
27
            ->key()
28
        ;
29
30
        $this->assertSame('abcde', $key->resource());
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function limit_is_required(): void
37
    {
38
        $this->expectException(\LogicException::class);
39
        $this->expectExceptionMessage(\sprintf('You must set a "Limit" for the throttle via "%s::allow($limit)"', ThrottleBuilder::class));
40
41
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore()), 'foo'))->every(10)->create();
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function ttl_is_required(): void
48
    {
49
        $this->expectException(\LogicException::class);
50
        $this->expectExceptionMessage(\sprintf('You must set a "TTL" for the throttle via "%s::every($ttl)"', ThrottleBuilder::class));
51
52
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore()), 'foo'))->allow(10)->create();
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function resource_is_required(): void
59
    {
60
        $this->expectException(\LogicException::class);
61
        $this->expectExceptionMessage('The resource for the throttle cannot be blank.');
62
63
        (new ThrottleBuilder(new ThrottleFactory(new MemoryStore())))->allow(10)->every(60)->create();
64
    }
65
}
66