Completed
Pull Request — master (#57)
by Iman
12:24 queued 08:57
created

CheckExpectations::checkResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Imanghafoori\HeyMan\MakeSure;
4
5
class CheckExpectations
6
{
7
    private $chain;
8
9
    private $phpunit;
10
11
    /**
12
     * CheckExpectations constructor.
13
     *
14
     * @param $chain
15
     * @param $phpunit
16
     */
17 32
    public function __construct($chain, $phpunit)
18
    {
19 32
        $this->chain = $chain;
20 32
        $this->phpunit = $phpunit;
21 32
    }
22
23 32
    public function check()
24
    {
25 32
        $this->expectExceptions();
26 32
        $this->fireEvents();
27 31
        $this->checkResponse();
28 31
    }
29
30 31
    private function checkResponse()
31
    {
32 31
        if (!$this->chain->http) {
33
            return;
34
        }
35 31
        $this->checkResponses($this->sendRequest());
36 31
    }
37
38
    /**
39
     * @return mixed
40
     */
41 31
    private function sendRequest()
42
    {
43 31
        $method = $this->chain->http['method'];
44
45 31
        $response = $this->phpunit->$method($this->chain->http['url'], $this->chain->http['data']);
46
47 31
        return $response;
48
    }
49
50
    /**
51
     * @param $response
52
     */
53 31
    private function checkResponses($response)
54
    {
55 31
        foreach ($this->chain->assertion as $assertion) {
56 27
            $type = $assertion['type'];
57 27
            $response->$type($assertion['value']);
58
        }
59 31
    }
60
61 32
    private function fireEvents()
62
    {
63 32
        if ($this->chain->event) {
64 1
            event($this->chain->event);
65
        }
66 31
    }
67
68 32
    private function expectExceptions()
69
    {
70 32
        if ($this->chain->exception) {
71 1
            $this->phpunit->expectException($this->chain->exception);
72
        }
73 32
    }
74
}
75