Muzzle::expectations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\HandlerStack;
9
use Muzzle\Assertions\AssertionRules;
10
use Muzzle\Messages\Transaction;
11
use Muzzle\Middleware\Assertable;
12
use Muzzle\Middleware\History;
13
14
class Muzzle implements ClientInterface
15
{
16
17
    use WrapsGuzzle;
18
19
    /**
20
     * @var Transactions
21
     */
22
    protected $history;
23
    /**
24
     * @var ClientInterface
25
     */
26
    protected $client;
27
    /**
28
     * @var HandlerStack
29
     */
30
    protected $stack;
31
    /**
32
     * @var MockHandler
33
     */
34
    protected $handler;
35
    /**
36
     * @var Transactions
37
     */
38
    protected $transactions;
39
    protected $assertionsHaveRun;
40
41
    public function __construct(array $options = [])
42
    {
43
44
        $this->transactions = new Transactions;
45
        $this->handler = new MockHandler;
46
        $this->stack = HandlerStack::create($this->handler);
47
        $this->client = new GuzzleClient(array_merge($options, ['handler' => $this->stack]));
48
        $this->setHistory(new Transactions);
49
        $this->stack->push(new Assertable, 'assertable');
50
        $this->stack->push(new History($this->history()), 'history');
51
52
        Container::push($this);
53
    }
54
55
    public static function make(array $options = []) : Muzzle
56
    {
57
58
        return new static($options);
59
    }
60
61
    public static function builder() : MuzzleBuilder
62
    {
63
64
        return new MuzzleBuilder;
65
    }
66
67
    public function updateConfig(array $config) : Muzzle
68
    {
69
70
        $this->client = new GuzzleClient(array_merge(
71
            $this->client->getConfig(),
72
            array_except($config, ['handler'])
73
        ));
74
75
        return $this;
76
    }
77
78
    public function makeAssertions() : void
79
    {
80
81
        $this->assertionsHaveRun = true;
82
        AssertionRules::new($this)->runAssertions();
83
    }
84
85
    public function append(Transaction ...$transactions) : Muzzle
86
    {
87
88
        foreach ($transactions as $transaction) {
89
            $this->transactions->push($transaction);
90
91
            $this->handler->append($transaction->response() ?: $transaction->error());
92
        }
93
94
        return $this;
95
    }
96
97
    public function addMiddleware(callable ...$middlewares) : Muzzle
98
    {
99
100
        foreach ($middlewares as $middleware) {
101
            $this->stack->push($middleware);
102
        }
103
104
        return $this;
105
    }
106
107
    public function removeMiddleware(string $middleware) : Muzzle
108
    {
109
110
        $this->stack->remove($middleware);
111
112
        return $this;
113
    }
114
115
    public function setHistory(Transactions $history) : Muzzle
116
    {
117
118
        $this->history = $history;
119
120
        return $this;
121
    }
122
123
    public function history() : Transactions
124
    {
125
126
        return $this->history;
127
    }
128
129
    public function expectations() : Transactions
130
    {
131
132
        return $this->transactions;
133
    }
134
135
    public function __destruct()
136
    {
137
138
        if (! $this->assertionsHaveRun) {
139
            $this->makeAssertions();
140
        }
141
    }
142
}
143