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
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\ArraySubset; |
17
|
|
|
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\MatchesJsonSchema; |
18
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
19
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @see \Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait |
23
|
|
|
* |
24
|
|
|
* @experimental |
25
|
|
|
*/ |
26
|
|
|
trait ApiTestAssertionsTrait |
27
|
|
|
{ |
28
|
|
|
use BrowserKitAssertionsTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Asserts that the retrieved JSON contains has the specified subset. |
32
|
|
|
* This method delegates to self::assertArraySubset(). |
33
|
|
|
* |
34
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
35
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface |
36
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
37
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
38
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
39
|
|
|
*/ |
40
|
|
|
public static function assertJsonContains(array $subset, bool $checkForObjectIdentity = true, string $message = ''): void |
41
|
|
|
{ |
42
|
|
|
static::assertArraySubset($subset, self::getHttpResponse()->toArray(false), $checkForObjectIdentity, $message); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Asserts that the retrieved JSON is equal to the following array. |
47
|
|
|
* Both values are canonicalized before the comparision. |
48
|
|
|
*/ |
49
|
|
|
public static function assertJsonEquals(array $json, string $message = ''): void |
50
|
|
|
{ |
51
|
|
|
static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Asserts that an array has a specified subset. |
56
|
|
|
* |
57
|
|
|
* Imported from dms/phpunit-arraysubset, because the original constraint has been deprecated. |
58
|
|
|
* |
59
|
|
|
* @copyright Sebastian Bergmann <[email protected]> |
60
|
|
|
* @copyright Rafael Dohms <[email protected]> |
61
|
|
|
* |
62
|
|
|
* @see https://github.com/sebastianbergmann/phpunit/issues/3494 |
63
|
|
|
* |
64
|
|
|
* @param iterable $subset |
65
|
|
|
* @param iterable $array |
66
|
|
|
* |
67
|
|
|
* @throws ExpectationFailedException |
68
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void |
72
|
|
|
{ |
73
|
|
|
$constraint = new ArraySubset($subset, $checkForObjectIdentity); |
74
|
|
|
static::assertThat($array, $constraint, $message); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array|string $jsonSchema |
79
|
|
|
*/ |
80
|
|
|
public static function assertMatchesJsonSchema($jsonSchema, ?int $checkMode = null, string $message = ''): void |
81
|
|
|
{ |
82
|
|
|
$constraint = new MatchesJsonSchema($jsonSchema, $checkMode); |
83
|
|
|
static::assertThat(self::getHttpResponse()->toArray(false), $constraint, $message); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function getHttpClient(Client $newClient = null): ?Client |
87
|
|
|
{ |
88
|
|
|
static $client; |
89
|
|
|
|
90
|
|
|
if (0 < \func_num_args()) { |
91
|
|
|
return $client = $newClient; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$client instanceof Client) { |
95
|
|
|
static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $client; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private static function getHttpResponse(): ResponseInterface |
102
|
|
|
{ |
103
|
|
|
if (!$response = self::getHttpClient()->getResponse()) { |
|
|
|
|
104
|
|
|
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $response; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
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.