MockActionResponse::assertRedirect()   A
last analyzed

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