1 | <?php |
||
2 | namespace RazonYang\TokenBucket\Tests; |
||
3 | |||
4 | use Psr\Log\NullLogger; |
||
5 | use RazonYang\TokenBucket\Manager; |
||
6 | use RazonYang\TokenBucket\SerializerInterface; |
||
7 | use RazonYang\TokenBucket\Serializer\JsonSerializer; |
||
8 | use RazonYang\TokenBucket\Serializer\PhpSerializer; |
||
9 | use RazonYang\TokenBucket\Serializer\RawSerializer; |
||
10 | |||
11 | class ManagerTest extends TestCase |
||
12 | { |
||
13 | public function createManager($capacity, $rate, ?SerializerInterface $serializer = null): TestManager |
||
14 | { |
||
15 | return new TestManager($capacity, $rate, new NullLogger(), $serializer); |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * @dataProvider dataProviderSetUp |
||
20 | */ |
||
21 | public function testSetUp(int $capacity, float $rate, ?SerializerInterface $serializer = null): void |
||
22 | { |
||
23 | $manager = $this->createManager($capacity, $rate, $serializer); |
||
24 | $this->assertSame($capacity, $manager->getCapacity()); |
||
25 | $this->assertSame($rate, $manager->getRate()); |
||
26 | $property = new \ReflectionProperty(Manager::class, 'serializer'); |
||
27 | $property->setAccessible(true); |
||
28 | if ($serializer) { |
||
29 | $this->assertSame($serializer, $property->getValue($manager)); |
||
30 | } else { |
||
31 | $this->assertNull($property->getValue($manager)); |
||
32 | } |
||
33 | } |
||
34 | |||
35 | public function dataProviderSetUp(): array |
||
36 | { |
||
37 | return [ |
||
38 | [1, 0.1, new JsonSerializer(0)], |
||
0 ignored issues
–
show
|
|||
39 | [2, 0.5, new JsonSerializer()], |
||
40 | [3, 1, new PhpSerializer()], |
||
41 | [4, 2, new RawSerializer()], |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @dataProvider dataProviderLimit |
||
47 | */ |
||
48 | public function testGetLimit(int $capacity, float $rate, int $period, int $limit) |
||
49 | { |
||
50 | $manager = $this->createManager($capacity, $rate); |
||
51 | $this->assertSame($limit, $manager->getLimit($period)); |
||
52 | } |
||
53 | |||
54 | public function dataProviderLimit(): array |
||
55 | { |
||
56 | return [ |
||
57 | [60, 1, 10, 10], |
||
58 | [60, 1, 60, 60], |
||
59 | [60, 1, 120, 60], |
||
60 | ]; |
||
61 | } |
||
62 | |||
63 | public function testConsume() |
||
64 | { |
||
65 | $capacity = 2; |
||
66 | $rate = 2; |
||
67 | $manager = $this->createManager($capacity, $rate); |
||
68 | $name = 'test'; |
||
69 | for ($i = 0; $i < $capacity; $i++) { |
||
70 | $this->assertTrue($manager->consume($name)); |
||
71 | } |
||
72 | $this->assertFalse($manager->consume($name)); |
||
73 | |||
74 | sleep($rate); |
||
75 | $this->assertTrue($manager->consume($name)); |
||
76 | } |
||
77 | |||
78 | public function testlaodAllowance(): void |
||
79 | { |
||
80 | $capacity = 10; |
||
81 | $manager = $this->createManager($capacity, 1); |
||
82 | |||
83 | $name = 'test'; |
||
84 | $manager->data[$name] = 'invalid data'; |
||
85 | $data = $manager->laodAllowance($name); |
||
86 | $this->assertSame($capacity, $data[0]); |
||
87 | } |
||
88 | } |
||
89 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.