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 Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
17
|
|
|
use Symfony\Component\BrowserKit\AbstractBrowser; |
18
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
19
|
|
|
use Symfony\Component\HttpClient\HttpClientTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base class for functional API tests. |
23
|
|
|
* |
24
|
|
|
* @experimental |
25
|
|
|
* |
26
|
|
|
* @author Kévin Dunglas <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class ApiTestCase extends KernelTestCase |
29
|
|
|
{ |
30
|
|
|
use ApiTestAssertionsTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function tearDown(): void |
36
|
|
|
{ |
37
|
|
|
parent::tearDown(); |
38
|
|
|
|
39
|
|
|
self::getClient(null); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a Client. |
44
|
|
|
* |
45
|
|
|
* @param array $kernelOptions Options to pass to the createKernel method |
46
|
|
|
* @param array $defaultOptions Default options for the requests |
47
|
|
|
*/ |
48
|
|
|
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client |
49
|
|
|
{ |
50
|
|
|
$kernel = static::bootKernel($kernelOptions); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
/** |
54
|
|
|
* @var Client |
55
|
|
|
*/ |
56
|
|
|
$client = $kernel->getContainer()->get('test.api_platform.client'); |
57
|
|
|
} catch (ServiceNotFoundException $e) { |
58
|
|
|
if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) { |
59
|
|
|
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit and HttpClient components are not available. Try running "composer require --dev symfony/browser-kit symfony/http-client".'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$client->setDefaultOptions($defaultOptions); |
66
|
|
|
|
67
|
|
|
self::getHttpClient($client); |
68
|
|
|
self::getClient($client->getKernelBrowser()); |
69
|
|
|
|
70
|
|
|
return $client; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|