Issues (26)

src/Checks/ResponseCheck.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Checks;
5
6
use PHPUnit\Framework\Assert;
7
use Pitchart\Phlunit\Checks\HttpResponse\HttpHeaderCheck;
8
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
9
use Pitchart\Phlunit\Checks\Mixin\FluentChecks;
10
use Pitchart\Phlunit\Checks\Mixin\TypeCheck;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pitchart\Phlunit\Checks\TypeCheck. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
12
use Pitchart\Phlunit\Constraint\HttpResponse\HasContentType;
13
use Pitchart\Phlunit\Constraint\HttpResponse\HasHeader;
14
use Pitchart\Phlunit\Constraint\HttpResponse\HasReason;
15
use Pitchart\Phlunit\Constraint\HttpResponse\HasStatusCode;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class ResponseCheck
20
 *
21
 * @package Pitchart\Phlunit\Checks
22
 *
23
 * @author Julien VITTE <[email protected]>
24
 *
25
 * @implements FluentCheck<ResponseInterface>
26
 */
27
class ResponseCheck implements FluentCheck
28
{
29 1
    use TypeCheck, FluentChecks, ConstraintCheck, WithMessage;
30
31
    /**
32
     * @var ResponseInterface
33
     */
34
    private $value;
35
36
    /**
37
     * ResponseCheck constructor.
38
     *
39
     * @param ResponseInterface $value
40
     */
41 17
    public function __construct(ResponseInterface $value)
42
    {
43 17
        $this->value = $value;
44 17
    }
45
46 3
    public function hasHeader(string $header): self
47
    {
48 3
        Assert::assertThat($this->value, new HasHeader($header), $this->message);
49 3
        $this->resetMessage();
50 3
        return $this;
51
    }
52
53 1
    public function whoseHeader(string $header): HttpHeaderCheck
54
    {
55 1
        $this->hasHeader($header);
56 1
        return new HttpHeaderCheck($this->value, $header);
57
    }
58
59
    /**
60
     * @param int|string $value
61
     *
62
     * @return ResponseCheck
63
     */
64 2
    public function hasStatus($value): self
65
    {
66 2
        Assert::assertThat($this->value, new HasStatusCode($value), $this->message);
67 2
        $this->resetMessage();
68 2
        return $this;
69
    }
70
71 2
    public function hasReason(string $reason): self
72
    {
73 2
        Assert::assertThat($this->value, new HasReason($reason), $this->message);
74 2
        $this->resetMessage();
75 2
        return $this;
76
    }
77
78 1
    public function isJson(): self
79
    {
80 1
        Assert::assertThat($this->value, new HasContentType('application/json'), $this->message);
81 1
        $this->resetMessage();
82 1
        return $this;
83
    }
84
85 1
    public function isHtml(): self
86
    {
87 1
        Assert::assertThat($this->value, new HasContentType('text/html'), $this->message);
88 1
        $this->resetMessage();
89 1
        return $this;
90
    }
91
92 1
    public function isXml(): self
93
    {
94 1
        Assert::assertThat($this->value, new HasContentType('application/xml'), $this->message);
95 1
        $this->resetMessage();
96 1
        return $this;
97
    }
98
99 1
    public function asJson(): JsonCheck
100
    {
101 1
        $content = $this->value->getBody()->getContents();
102 1
        Assert::assertJson($content);
103 1
        return new JsonCheck($content);
104
    }
105
106 1
    public function asXml(): XmlCheck
107
    {
108 1
        $content = $this->value->getBody()->getContents();
109 1
        return new XmlCheck($content);
110
    }
111
}
112