Completed
Push — master ( 32e949...cc7d0f )
by Leonardo
22s queued 12s
created

GuzzleMiddlewareTest::testSuccessRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 9.8666
cc 1
nc 1
nop 0
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
}