Passed
Push — master ( 773906...23c2fd )
by Iman
10:34 queued 07:39
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 38
    public function __construct(Chain $chain, $phpunit)
18
    {
19 38
        $this->chain = $chain;
20 38
        $this->phpunit = $phpunit;
21 38
    }
22
23 38
    public function check()
24
    {
25 38
        $this->expectExceptions();
26 38
        $this->fireEvents();
27 37
        $this->checkResponse();
28 37
    }
29
30 37
    private function checkResponse()
31
    {
32 37
        if (!$this->chain->http) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->chain->http of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            return;
34
        }
35 37
        $this->checkResponses($this->sendRequest());
36 37
    }
37
38
    /**
39
     * @return mixed
40
     */
41 37
    private function sendRequest()
42
    {
43 37
        $method = $this->chain->http['method'];
44 37
        $data = $this->chain->http;
45 37
        if ($method == 'get' or $method == 'getJson') {
46 27
            $response = $this->phpunit->$method($data['uri'], $data['headers']);
47
        } else {
48 10
            $response = $this->phpunit->$method($data['uri'], $data['data'], $data['headers']);
49
        }
50
51 37
        return $response;
52
    }
53
54
    /**
55
     * @param $response
56
     */
57 37
    private function checkResponses($response)
58
    {
59 37
        foreach ($this->chain->assertion as $assertion) {
60 29
            $type = $assertion['type'];
61 29
            $response->$type($assertion['value']);
62
        }
63 37
    }
64
65 38
    private function fireEvents()
66
    {
67 38
        if ($this->chain->event) {
68 1
            event($this->chain->event);
69
        }
70 37
    }
71
72 38
    private function expectExceptions()
73
    {
74 38
        if ($this->chain->exception) {
75 1
            $this->phpunit->expectException($this->chain->exception);
76
        }
77 38
    }
78
}
79