• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.07
/src/Symfony/Bundle/Test/ApiTestCase.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Symfony\Bundle\Test;
15

16
use ApiPlatform\Metadata\IriConverterInterface;
17
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18
use Symfony\Component\BrowserKit\AbstractBrowser;
19
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
20
use Symfony\Component\HttpClient\HttpClientTrait;
21

22
/**
23
 * Base class for functional API tests.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 */
27
abstract class ApiTestCase extends KernelTestCase
28
{
29
    use ApiTestAssertionsTrait;
30

31
    /**
32
     * If you're using RecreateDatabaseTrait, RefreshDatabaseTrait, ReloadDatabaseTrait from theofidry/AliceBundle, you
33
     * probably need to set this property to false in your test class to avoid recreating the database on each client creation.
34
     *
35
     * - `null` triggers a deprecation message and always boots the kernel
36
     * - `false` does not boot the kernel if it's already booted
37
     * - `true` always boots the kernel without any deprecation message
38
     */
39
    protected static ?bool $alwaysBootKernel = null;
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
    {
UNCOV
49
        if (null === static::$alwaysBootKernel) {
237✔
50
            trigger_deprecation(
×
51
                'api-platform/symfony',
×
52
                '4.1.0',
×
53
                'In API Platform 5.0, the kernel will not always be booted when a new client is created (see https://github.com/api-platform/core/issues/6971).',
×
54
            );
×
55
        }
56

UNCOV
57
        if (static::$alwaysBootKernel || null === static::$alwaysBootKernel) {
237✔
58
            static::bootKernel($kernelOptions);
×
59
        }
60

61
        try {
62
            /**
63
             * @var Client
64
             */
UNCOV
65
            $client = self::getContainer()->get('test.api_platform.client');
237✔
66
        } catch (ServiceNotFoundException) {
×
67
            if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
×
68
                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".');
×
69
            }
70

71
            throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
×
72
        }
73

UNCOV
74
        $client->setDefaultOptions($defaultOptions);
237✔
75

UNCOV
76
        self::getHttpClient($client);
237✔
UNCOV
77
        self::getClient($client->getKernelBrowser());
237✔
78

UNCOV
79
        return $client;
237✔
80
    }
81

82
    /**
83
     * Finds the IRI of a resource item matching the resource class and the specified criteria.
84
     */
85
    protected function findIriBy(string $resourceClass, array $criteria): ?string
86
    {
UNCOV
87
        $container = static::getContainer();
1✔
88

89
        if (
UNCOV
90
            (
UNCOV
91
                !$container->has('doctrine')
1✔
UNCOV
92
                || null === $objectManager = $container->get('doctrine')->getManagerForClass($resourceClass)
1✔
93
            )
94
            && (
UNCOV
95
                !$container->has('doctrine_mongodb')
1✔
UNCOV
96
                || null === $objectManager = $container->get('doctrine_mongodb')->getManagerForClass($resourceClass)
1✔
97
            )
98
        ) {
99
            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__));
×
100
        }
101

UNCOV
102
        $item = $objectManager->getRepository($resourceClass)->findOneBy($criteria);
1✔
UNCOV
103
        if (null === $item) {
1✔
UNCOV
104
            return null;
1✔
105
        }
106

UNCOV
107
        return $this->getIriFromResource($item);
1✔
108
    }
109

110
    /**
111
     * Generate the IRI of a resource item.
112
     */
113
    protected function getIriFromResource(object $resource): ?string
114
    {
115
        /** @var IriConverterInterface $iriConverter */
UNCOV
116
        $iriConverter = static::getContainer()->get('api_platform.iri_converter');
1✔
117

UNCOV
118
        return $iriConverter->getIriFromResource($resource);
1✔
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc