Completed
Push — master ( 6e9ccf...5245c1 )
by Han Hui
20s queued 12s
created

ApiTestCase::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function tearDown(): void
35
    {
36
        parent::tearDown();
37
38
        self::getClient(null);
39
    }
40
41
    /**
42
     * Creates a Client.
43
     *
44
     * @param array $kernelOptions  Options to pass to the createKernel method
45
     * @param array $defaultOptions Default options for the requests
46
     */
47
    protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
48
    {
49
        $kernel = static::bootKernel($kernelOptions);
50
51
        try {
52
            /**
53
             * @var Client
54
             */
55
            $client = $kernel->getContainer()->get('test.api_platform.client');
56
        } catch (ServiceNotFoundException $e) {
57
            if (class_exists(KernelBrowser::class) && trait_exists('Symfony\Component\HttpClient\HttpClientTrait')) {
58
                throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
59
            }
60
            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".');
61
        }
62
63
        $client->setDefaultOptions($defaultOptions);
64
65
        self::getHttpClient($client);
66
        self::getClient($client->getKernelBrowser());
67
68
        return $client;
0 ignored issues
show
Bug Best Practice introduced by Kévin Dunglas
The expression return $client could return the type null which is incompatible with the type-hinted return ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
}
71