CheckExpectations::expectExceptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
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\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 14
    public function __construct(Chain $chain, $phpunit)
18
    {
19 14
        $this->chain = $chain;
20 14
        $this->phpunit = $phpunit;
21 14
    }
22
23 14
    public function check()
24
    {
25 14
        $this->expectExceptions();
26 14
        $this->fireEvents();
27 14
        $this->checkResponse();
28 14
    }
29
30 14
    private function checkResponse()
31
    {
32 14
        if (isset($this->chain->data['http'])) {
33 11
            $response = $this->sendRequest(...$this->chain->data['http']);
0 ignored issues
show
Bug introduced by
The call to sendRequest() misses a required argument $data.

This check looks for function calls that miss required arguments.

Loading history...
34 11
            $this->checkResponses($response);
35
        }
36 14
    }
37
38
    /**
39
     * @param $method
40
     * @param $data
41
     *
42
     * @return mixed
43
     */
44 11
    private function sendRequest($method, $data)
45
    {
46 11
        return $this->phpunit->$method(...$data);
47
    }
48
49
    /**
50
     * @param $response
51
     */
52 11
    private function checkResponses($response)
53
    {
54 11
        foreach ($this->chain->data['assertion'] ?? [] as [$type, $value]) {
55 3
            $response->$type($value);
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
        }
57 11
    }
58
59 14
    private function fireEvents()
60
    {
61 14
        if (isset($this->chain->data['event'])) {
62
            event($this->chain->data['event']);
63
        }
64 14
    }
65
66 14
    private function expectExceptions()
67
    {
68 14
        if (isset($this->chain->data['exception'])) {
69
            $this->phpunit->expectException($this->chain->data['exception']);
70
        }
71 14
    }
72
}
73