Passed
Pull Request — master (#2887)
by Kévin
03:48
created

ApiTestCase::createClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 20
rs 9.9332
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\KernelBrowser;
17
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19
20
/**
21
 * Base class for functional API tests.
22
 *
23
 * @experimental
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
abstract class ApiTestCase extends KernelTestCase
28
{
29
    use ApiTestAssertionsTrait;
30
31
    protected function doTearDown(): void
32
    {
33
        parent::doTearDown();
34
        self::getClient(null);
35
    }
36
37
    /**
38
     * Creates a Client.
39
     *
40
     * @param array $options An array of options to pass to the createKernel method
41
     */
42
    protected static function createClient(array $options = []): Client
43
    {
44
        $kernel = static::bootKernel($options);
45
46
        try {
47
            /**
48
             * @var Client
49
             */
50
            $client = $kernel->getContainer()->get('test.api_platform.client');
51
        } catch (ServiceNotFoundException $e) {
52
            if (class_exists(KernelBrowser::class)) {
53
                throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
54
            }
55
            throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit".');
56
        }
57
58
        self::getHttpClient($client);
59
        self::getClient($client->getKernelBrowser());
60
61
        return $client;
62
    }
63
}
64