Passed
Push — master ( 5153a0...c7b3db )
by Josh
03:56
created

MockActionResponse::assertDownload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace JoshGaber\NovaUnit\Actions;
4
5
use JoshGaber\NovaUnit\Constraints\IsActionResponseType;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
8
class MockActionResponse
9
{
10
    private $response;
11
12 21
    public function __construct($response)
13
    {
14 21
        $this->response = $response;
15 21
    }
16
17
    /**
18
     * Asserts the handle response is of the given type.
19
     *
20
     * @param string $type
21
     * @param string $message
22
     * @return $this
23
     */
24 14
    public function assertResponseType(string $type, string $message = ''): self
25
    {
26 14
        PHPUnit::assertThat(
27 14
            $this->response,
28 14
            new IsActionResponseType($type),
29 14
            $message
30
        );
31
32 7
        return $this;
33
    }
34
35
    /**
36
     * Asserts the handle response is of type "message".
37
     *
38
     * @param string $message
39
     * @return $this
40
     */
41 2
    public function assertMessage(string $message = ''): self
42
    {
43 2
        return $this->assertResponseType('message', $message);
44
    }
45
46
    /**
47
     * Asserts the handle response is of type "danger".
48
     *
49
     * @param string $message
50
     * @return $this
51
     */
52 2
    public function assertDanger(string $message = ''): self
53
    {
54 2
        return $this->assertResponseType('danger', $message);
55
    }
56
57
    /**
58
     * Asserts the handle response is of type "deleted".
59
     *
60
     * @param string $message
61
     * @return $this
62
     */
63 2
    public function assertDeleted(string $message = ''): self
64
    {
65 2
        return $this->assertResponseType('deleted', $message);
66
    }
67
68
    /**
69
     * Asserts the handle response is of type "redirect".
70
     *
71
     * @param string $message
72
     * @return $this
73
     */
74 2
    public function assertRedirect(string $message = ''): self
75
    {
76 2
        return $this->assertResponseType('redirect', $message);
77
    }
78
79
    /**
80
     * Asserts the handle response is of type "push".
81
     *
82
     * @param string $message
83
     * @return $this
84
     */
85 2
    public function assertPush(string $message = ''): self
86
    {
87 2
        return $this->assertResponseType('push', $message);
88
    }
89
90
    /**
91
     * Asserts the handle response is of type "openInNewTab".
92
     *
93
     * @param string $message
94
     * @return $this
95
     */
96 2
    public function assertOpenInNewTab(string $message = ''): self
97
    {
98 2
        return $this->assertResponseType('openInNewTab', $message);
99
    }
100
101
    /**
102
     * Asserts the handle response is of type "download".
103
     *
104
     * @param string $message
105
     * @return $this
106
     */
107 2
    public function assertDownload(string $message = ''): self
108
    {
109 2
        return $this->assertResponseType('download', $message);
110
    }
111
112 6
    private function assertResponseContains(string $contents, string $type, string $message = ''): self
113
    {
114 6
        PHPUnit::assertThat(
115 6
            $this->response[$type] ?? '',
116 6
            PHPUnit::logicalAnd(
117 6
                PHPUnit::logicalNot(PHPUnit::isEmpty()),
118 6
                PHPUnit::stringContains($contents, true)
119
            ),
120 6
            $message
121
        );
122
123 2
        return $this;
124
    }
125
126
    /**
127
     * Asserts the handle response is a "message" and contains the given text.
128
     *
129
     * @param string $contents The text to assert is in the response
130
     * @param string $message
131
     * @return $this
132
     */
133 3
    public function assertMessageContains(string $contents, string $message = ''): self
134
    {
135 3
        return $this->assertResponseContains($contents, 'message', $message);
136
    }
137
138
    /**
139
     * Asserts the handle response is a "danger" and contains the given text.
140
     *
141
     * @param string $contents The text to assert is in the response
142
     * @param string $message
143
     * @return $this
144
     */
145 3
    public function assertDangerContains(string $contents, string $message = ''): self
146
    {
147 3
        return $this->assertResponseContains($contents, 'danger', $message);
148
    }
149
}
150