Issues (17)

src/Messages/AssertableResponse.php (1 issue)

Severity
1
<?php
2
3
namespace Muzzle\Messages;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
6
7
class AssertableResponse extends DecodableResponse
8
{
9
10
    use ContentAssertions;
11
    use Statusable;
12
13
    /**
14
     * Assert that the response has a successful status code.
15
     *
16
     * @return $this
17
     */
18
    public function assertSuccessful()
19
    {
20
21
        PHPUnit::assertTrue(
22
            $this->isSuccessful(),
23
            "Response status code [{$this->getStatusCode()}] is not a successful status code."
24
        );
25
26
        return $this;
27
    }
28
29
    /**
30
     * Assert that the response has the given status code.
31
     *
32
     * @param  int $status
33
     * @return $this
34
     */
35
    public function assertStatus($status)
36
    {
37
38
        $actual = $this->getStatusCode();
39
40
        PHPUnit::assertTrue(
41
            $actual === $status,
42
            "Expected status code {$status} but received {$actual}."
43
        );
44
45
        return $this;
46
    }
47
48
    /**
49
     * Assert whether the response is redirecting to a given URI.
50
     *
51
     * @param  string $uri
52
     * @return $this
53
     */
54
    public function assertRedirect($uri = null)
55
    {
56
57
        PHPUnit::assertTrue(
58
            $this->isRedirect(),
59
            "Response status code [{$this->getStatusCode()}] is not a redirect status code."
60
        );
61
62
        if (! is_null($uri)) {
63
            PHPUnit::assertContains(app('url')->to($uri), $this->getHeader('Location'), '', true);
0 ignored issues
show
The call to Muzzle\Messages\app() has too many arguments starting with 'url'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            PHPUnit::assertContains(/** @scrutinizer ignore-call */ app('url')->to($uri), $this->getHeader('Location'), '', true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * Asserts that the response contains the given header and equals the optional value.
71
     *
72
     * @param  string $headerName
73
     * @param  mixed $value
74
     * @return $this
75
     */
76
    public function assertHeader($headerName, $value = null)
77
    {
78
79
        PHPUnit::assertTrue(
80
            $this->hasHeader($headerName),
81
            "Header [{$headerName}] not present on response."
82
        );
83
84
        $actual = $this->getHeader($headerName);
85
86
        if (! is_null($value)) {
87
            $message = sprintf(
88
                "Header [%s] was found, but value(s) [%s] does not match [%s].",
89
                $headerName,
90
                implode(', ', $actual),
91
                $value
92
            );
93
            PHPUnit::assertContains($value, $actual, $message);
94
        }
95
96
        return $this;
97
    }
98
}
99