CircuitBreakerTest::makeBreaker()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace DeGraciaMathieu\EasyBreaker\Tests;
4
5
use Exception;
6
use PHPUnit\Framework\TestCase;
7
use DeGraciaMathieu\EasyBreaker\Breaker;
8
use DeGraciaMathieu\EasyBreaker\CircuitBreaker;
9
10
class CircuitBreakerTest extends TestCase
11
{
12
    /**
13
     * @test
14
     */
15
    public function process_with_break()
16
    {
17
        $firstBreaker = $this->makeBreaker($message = "it's really broken.", $exception = Exception::class);
0 ignored issues
show
Unused Code introduced by
The call to CircuitBreakerTest::makeBreaker() has too many arguments starting with $exception = \Exception::class.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
18
        $secondBreaker = $this->makeBreakerWithCustomException($customMessage = "it's really broken.", CustomException::class);
19
        $thirdBreaker = $this->makeBreaker($message = "it's really really broken.", $exception = Exception::class);
0 ignored issues
show
Unused Code introduced by
The call to CircuitBreakerTest::makeBreaker() has too many arguments starting with $exception = \Exception::class.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
20
21
        $results = (new CircuitBreaker())
22
            ->addBreaker($firstBreaker)
23
            ->addBreaker($secondBreaker)
24
            ->addBreaker($thirdBreaker)
25
            ->closure(function(){
26
                throw new Exception();
27
            });
28
29
30
        $this->assertNotNull($results);
31
        $this->assertCount(2, $results);
32
        $this->assertEquals($results[0], "it's really broken.");
33
        $this->assertEquals($results[1], "it's really really broken.");
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function process_without_break()
40
    {
41
        $firstBreaker = $this->makeBreaker($message = "it's really broken.", $exception = Exception::class);
0 ignored issues
show
Unused Code introduced by
The call to CircuitBreakerTest::makeBreaker() has too many arguments starting with $exception = \Exception::class.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
43
        $results = (new CircuitBreaker())
44
            ->addBreaker($firstBreaker)
45
            ->closure(function(){
46
                return;
47
            });
48
49
50
        $this->assertNull($results);
51
    }
52
53
    /**
54
     * @param  strng $message
55
     * @return \DeGraciaMathieu\EasyBreaker\Breaker
56
     */
57
    protected function makeBreaker(string $message) :Breaker
58
    {
59
        return $this->makeBreakerWithCustomException($message, Exception::class);
60
    }
61
62
    /**
63
     * @param  string $message
64
     * @param  string $exception
65
     * @return \DeGraciaMathieu\EasyBreaker\Breaker
66
     */
67
    protected function makeBreakerWithCustomException(string $message, string $exception) :Breaker
68
    {
69
        return (new Breaker)
70
            ->when($exception)
71
            ->do(function(Exception $e) use($message) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
                return $message;
73
            });
74
    }
75
}
76