Passed
Pull Request — master (#10)
by Leonardo
01:41
created

GuzzleMiddlewareTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 33
c 2
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCircuitIsNotAvailable() 0 17 1
A testFailureRequest() 0 18 1
A testSuccessRequest() 0 22 1
1
<?php declare(strict_types=1);
2
3
use GuzzleHttp\Client;
4
use GuzzleHttp\HandlerStack;
5
use LeoCarmo\CircuitBreaker\Adapters\SwooleTableAdapter;
6
use LeoCarmo\CircuitBreaker\CircuitBreaker;
7
use LeoCarmo\CircuitBreaker\CircuitBreakerException;
8
use LeoCarmo\CircuitBreaker\GuzzleMiddleware;
9
use PHPUnit\Framework\TestCase;
10
11
class GuzzleMiddlewareTest extends TestCase
12
{
13
    public function testSuccessRequest()
14
    {
15
        $circuit = new CircuitBreaker(new SwooleTableAdapter(), 'testSuccessRequest');
16
17
        // Set the first failure and the failure threshold
18
        $circuit->setSettings(['failureRateThreshold' => 2]);
19
        $circuit->failure();
20
21
        $handler = new GuzzleMiddleware($circuit);
22
        $handlers = HandlerStack::create();
23
        $handlers->push($handler);
24
25
        $client = new Client(['handler' => $handlers, 'verify' => false]);
26
        $response = $client->get('leocarmo.dev');
27
28
        // After a success response the failures must be reset and the circuit is available
29
        $this->assertEquals(200, $response->getStatusCode());
30
        $this->assertTrue($circuit->isAvailable());
31
32
        // Set another failure to ensure that the previous failure was reset and a new fail will not open the circuit
33
        $circuit->failure();
34
        $this->assertTrue($circuit->isAvailable());
35
    }
36
37
    public function testCircuitIsNotAvailable()
38
    {
39
        $circuit = new CircuitBreaker(new SwooleTableAdapter(), 'testCircuitIsNotAvailable');
40
41
        $circuit->setSettings(['failureRateThreshold' => 2]);
42
        $circuit->failure();
43
        $circuit->failure();
44
45
        $handler = new GuzzleMiddleware($circuit);
46
        $handlers = HandlerStack::create();
47
        $handlers->push($handler);
48
49
        $client = new Client(['handler' => $handlers, 'verify' => false]);
50
51
        $this->expectException(CircuitBreakerException::class);
52
53
        $client->get('leocarmo.dev');
54
    }
55
56
    public function testFailureRequest()
57
    {
58
        $circuit = new CircuitBreaker(new SwooleTableAdapter(), 'testFailureRequest');
59
60
        $circuit->setSettings(['failureRateThreshold' => 2]);
61
        $circuit->failure();
62
63
        $handler = new GuzzleMiddleware($circuit);
64
        $handlers = HandlerStack::create();
65
        $handlers->push($handler);
66
67
        $client = new Client(['handler' => $handlers, 'verify' => false]);
68
69
        $this->expectException(\GuzzleHttp\Exception\ClientException::class);
70
71
        $client->get('leocarmo.dev/undefined');
72
73
        $this->assertFalse($circuit->isAvailable());
74
    }
75
}