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 | * |
||
33 | * This method delegates to self::assertArraySubset(). |
||
34 | * |
||
35 | * @param array|string $subset |
||
36 | * |
||
37 | * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
||
38 | * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface |
||
39 | * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
||
40 | * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
||
41 | * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
||
42 | */ |
||
43 | public static function assertJsonContains($subset, bool $checkForObjectIdentity = true, string $message = ''): void |
||
44 | { |
||
45 | if (\is_string($subset)) { |
||
46 | $subset = json_decode($subset, true); |
||
47 | } |
||
48 | if (!\is_array($subset)) { |
||
49 | throw new \InvalidArgumentException('$subset must be array or string (JSON array or JSON object)'); |
||
50 | } |
||
51 | |||
52 | static::assertArraySubset($subset, self::getHttpResponse()->toArray(false), $checkForObjectIdentity, $message); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Asserts that the retrieved JSON is equal to $json. |
||
57 | * |
||
58 | * Both values are canonicalized before the comparision. |
||
59 | * |
||
60 | * @param array|string $json |
||
61 | */ |
||
62 | public static function assertJsonEquals($json, string $message = ''): void |
||
63 | { |
||
64 | if (\is_string($json)) { |
||
65 | $json = json_decode($json, true); |
||
66 | } |
||
67 | if (!\is_array($json)) { |
||
68 | throw new \InvalidArgumentException('$json must be array or string (JSON array or JSON object)'); |
||
69 | } |
||
70 | |||
71 | static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Asserts that an array has a specified subset. |
||
76 | * |
||
77 | * Imported from dms/phpunit-arraysubset, because the original constraint has been deprecated. |
||
78 | * |
||
79 | * @copyright Sebastian Bergmann <[email protected]> |
||
80 | * @copyright Rafael Dohms <[email protected]> |
||
81 | * |
||
82 | * @see https://github.com/sebastianbergmann/phpunit/issues/3494 |
||
83 | * |
||
84 | * @param iterable $subset |
||
85 | * @param iterable $array |
||
86 | * |
||
87 | * @throws ExpectationFailedException |
||
88 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
89 | * @throws \Exception |
||
90 | */ |
||
91 | public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void |
||
92 | { |
||
93 | $constraint = new ArraySubset($subset, $checkForObjectIdentity); |
||
94 | static::assertThat($array, $constraint, $message); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array|string $jsonSchema |
||
99 | */ |
||
100 | public static function assertMatchesJsonSchema($jsonSchema, ?int $checkMode = null, string $message = ''): void |
||
101 | { |
||
102 | $constraint = new MatchesJsonSchema($jsonSchema, $checkMode); |
||
103 | static::assertThat(self::getHttpResponse()->toArray(false), $constraint, $message); |
||
104 | } |
||
105 | |||
106 | private static function getHttpClient(Client $newClient = null): ?Client |
||
107 | { |
||
108 | static $client; |
||
109 | |||
110 | if (0 < \func_num_args()) { |
||
111 | return $client = $newClient; |
||
112 | } |
||
113 | |||
114 | if (!$client instanceof Client) { |
||
115 | static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__)); |
||
116 | } |
||
117 | |||
118 | return $client; |
||
119 | } |
||
120 | |||
121 | private static function getHttpResponse(): ResponseInterface |
||
122 | { |
||
123 | if (!$response = self::getHttpClient()->getResponse()) { |
||
0 ignored issues
–
show
|
|||
124 | static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?'); |
||
125 | } |
||
126 | |||
127 | return $response; |
||
128 | } |
||
129 | } |
||
130 |
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.