Passed
Push — master ( 4bfeda...efa341 )
by Kévin
05:01
created

assertRequestAttributeValueSame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
/*
15
 * This file is part of the Symfony package.
16
 *
17
 * (c) Fabien Potencier <[email protected]>
18
 *
19
 * For the full copyright and license information, please view the LICENSE
20
 * file that was distributed with this source code.
21
 */
22
23
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test;
24
25
use PHPUnit\Framework\Constraint\LogicalAnd;
26
use PHPUnit\Framework\Constraint\LogicalNot;
27
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
28
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiPlatform\Core\Bridge\...ny\Bundle\Test\Response. 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...
31
use Symfony\Component\HttpFoundation\Test\Constraint as ResponseConstraint;
32
33
/**
34
 * Copied from Symfony, to remove when https://github.com/symfony/symfony/pull/32207 will be merged.
35
 *
36
 * @internal
37
 */
38
trait BrowserKitAssertionsTrait
39
{
40
    public static function assertResponseIsSuccessful(string $message = ''): void
41
    {
42
        self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
43
    }
44
45
    public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
46
    {
47
        self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
48
    }
49
50
    public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
51
    {
52
        $constraint = new ResponseConstraint\ResponseIsRedirected();
53
        if ($expectedLocation) {
54
            $constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseHeaderSame('Location', $expectedLocation));
55
        }
56
        if ($expectedCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedCode of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57
            $constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode));
58
        }
59
60
        self::assertThat(self::getResponse(), $constraint, $message);
61
    }
62
63
    public static function assertResponseHasHeader(string $headerName, string $message = ''): void
64
    {
65
        self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
66
    }
67
68
    public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void
69
    {
70
        self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
71
    }
72
73
    public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
74
    {
75
        self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
76
    }
77
78
    public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
79
    {
80
        self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
81
    }
82
83
    public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
84
    {
85
        self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message);
86
    }
87
88
    public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
89
    {
90
        self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message);
91
    }
92
93
    public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void
94
    {
95
        self::assertThat(self::getResponse(), LogicalAnd::fromConstraints(
96
            new ResponseConstraint\ResponseHasCookie($name, $path, $domain),
97
            new ResponseConstraint\ResponseCookieValueSame($name, $expectedValue, $path, $domain)
98
        ), $message);
99
    }
100
101
    public static function assertBrowserHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
102
    {
103
        self::assertThat(self::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getClient() targeting ApiPlatform\Core\Bridge\...tionsTrait::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
    }
105
106
    public static function assertBrowserNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
107
    {
108
        self::assertThat(self::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getClient() targeting ApiPlatform\Core\Bridge\...tionsTrait::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
    }
110
111
    public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', string $domain = null, string $message = ''): void
112
    {
113
        self::assertThat(self::getClient(), LogicalAnd::fromConstraints(
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getClient() targeting ApiPlatform\Core\Bridge\...tionsTrait::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
114
            new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain),
115
            new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain)
116
        ), $message);
117
    }
118
119
    public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
120
    {
121
        self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
122
    }
123
124
    public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void
125
    {
126
        $constraint = new ResponseConstraint\RequestAttributeValueSame('_route', $expectedRoute);
127
        $constraints = [];
128
        foreach ($parameters as $key => $value) {
129
            $constraints[] = new ResponseConstraint\RequestAttributeValueSame($key, $value);
130
        }
131
        if ($constraints) {
132
            $constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
133
        }
134
135
        self::assertThat(self::getRequest(), $constraint, $message);
136
    }
137
138
    private static function getClient(KernelBrowser $newClient = null): ?KernelBrowser
139
    {
140
        static $client;
141
142
        if (0 < \func_num_args()) {
143
            return $client = $newClient;
144
        }
145
146
        if (!$client instanceof KernelBrowser) {
147
            static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__));
148
        }
149
150
        return $client;
151
    }
152
153
    private static function getResponse(): Response
154
    {
155
        if (!$response = self::getClient()->getResponse()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getClient() targeting ApiPlatform\Core\Bridge\...tionsTrait::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
156
            static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?');
157
        }
158
159
        return $response;
160
    }
161
162
    private static function getRequest(): Request
163
    {
164
        if (!$request = self::getClient()->getRequest()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getClient() targeting ApiPlatform\Core\Bridge\...tionsTrait::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
165
            static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?');
166
        }
167
168
        return $request;
169
    }
170
}
171