Passed
Push — master ( 336684...81d276 )
by Kévin
19:58 queued 14:43
created

ApiTestCase::findIriBy()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 4
nop 2
dl 0
loc 25
rs 8.8333
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\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;
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...
71
    }
72
73
    /**
74
     * Finds the IRI of a resource item macthing the resource class and the specified criteria.
75
     */
76
    protected function findIriBy(string $resourceClass, array $criteria): ?string
77
    {
78
        if (null === static::$container) {
79
            throw new \RuntimeException(sprintf('The container is not available. You must call "bootKernel()" or "createClient()" before calling "%s".', __METHOD__));
80
        }
81
82
        if (
83
            (
84
                !static::$container->has('doctrine') ||
85
                null === $objectManager = static::$container->get('doctrine')->getManagerForClass($resourceClass)
86
            ) &&
87
            (
88
                !static::$container->has('doctrine_mongodb') ||
89
                null === $objectManager = static::$container->get('doctrine_mongodb')->getManagerForClass($resourceClass)
90
            )
91
        ) {
92
            throw new \RuntimeException(sprintf('"%s" only supports classes managed by Doctrine ORM or Doctrine MongoDB ODM. Override this method to implement your own retrieval logic if you don\'t use those libraries.', __METHOD__));
93
        }
94
95
        $item = $objectManager->getRepository($resourceClass)->findOneBy($criteria);
96
        if (null === $item) {
97
            return null;
98
        }
99
100
        return static::$container->get('api_platform.iri_converter')->getIriFromItem($item);
101
    }
102
}
103