ThrottlingServiceTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
dl 0
loc 52
c 3
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidWithFilesCache() 0 10 1
A testInvalidRate() 0 5 1
A testValidRate() 0 5 1
A setUp() 0 8 1
1
<?php
2
3
namespace Services;
4
5
use Gvera\Cache\Cache;
6
use Gvera\Exceptions\ThrottledException;
7
use Gvera\Helpers\config\Config;
8
use Gvera\Services\ThrottlingService;
9
use PHPUnit\Framework\TestCase;
10
11
class ThrottlingServiceTest extends TestCase
12
{
13
    private $service;
14
    private $ip = "123";
15
16
    /**
17
     * @throws \Exception
18
     */
19
    public function setUp(): void
20
    {
21
        parent::setUp();
22
        $config = new Config(__DIR__.'/../../config/config.yml');
23
        Cache::setConfig($config);
24
        $this->service = new ThrottlingService();
25
        $this->service->setAllowedRequestsPerSecond(1);
26
        $this->service->setIp($this->ip);
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function testInvalidRate()
33
    {
34
        $this->expectException(ThrottledException::class);
35
        $this->service->validateRate();
36
        $this->service->validateRate();
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function testValidRate()
43
    {
44
        sleep(1);
45
        $this->service->validateRate();
46
        $this->assertTrue(Cache::getCache()->exists(ThrottlingService::PREFIX_THROTTLING.$this->ip));
47
    }
48
49
    /**
50
     * @test
51
     * @throws \Exception
52
     */
53
    public function testValidWithFilesCache()
54
    {
55
        $config = new Config(__DIR__.'/../../config/config.yml');
56
        $config->overrideKey('cache_type', 'files');
57
        Cache::setConfig($config);
58
        $this->service->setAllowedRequestsPerSecond(1);
59
        $this->service->setIp($this->ip);
60
61
        $this->service->validateRate();
62
        $this->service->validateRate();
63
    }
64
65
}