• 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

81.48
/src/Symfony/Bundle/Test/Constraint/MatchesJsonSchema.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\Constraint;
15

16
use JsonSchema\Validator;
17
use PHPUnit\Framework\Constraint\Constraint;
18

19
/**
20
 * Asserts that a JSON document matches a given JSON Schema.
21
 *
22
 * @author Kévin Dunglas <dunglas@gmail.com>
23
 */
24
final class MatchesJsonSchema extends Constraint
25
{
26
    private object|array $schema;
27

28
    public function __construct(object|array|string $schema, private readonly ?int $checkMode = null)
29
    {
UNCOV
30
        $this->schema = \is_string($schema) ? json_decode($schema, null, 512, \JSON_THROW_ON_ERROR) : $schema;
22✔
31
    }
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function toString(): string
37
    {
UNCOV
38
        return 'matches the provided JSON Schema';
1✔
39
    }
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function matches(mixed $other): bool
45
    {
UNCOV
46
        if (!class_exists(Validator::class)) {
22✔
47
            throw new \LogicException('The "justinrainbow/json-schema" library must be installed to use "assertMatchesJsonSchema()". Try running "composer require --dev justinrainbow/json-schema".');
×
48
        }
49

UNCOV
50
        $other = $this->normalizeJson($other);
22✔
51

UNCOV
52
        $validator = new Validator();
22✔
UNCOV
53
        $validator->validate($other, $this->schema, $this->checkMode);
22✔
54

UNCOV
55
        return $validator->isValid();
22✔
56
    }
57

58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function additionalFailureDescription(mixed $other): string
62
    {
UNCOV
63
        $other = $this->normalizeJson($other);
1✔
64

UNCOV
65
        $validator = new Validator();
1✔
UNCOV
66
        $validator->validate($other, $this->schema, $this->checkMode);
1✔
67

UNCOV
68
        $errors = [];
1✔
UNCOV
69
        foreach ($validator->getErrors() as $error) {
1✔
UNCOV
70
            $property = $error['property'] ? $error['property'].': ' : '';
1✔
UNCOV
71
            $errors[] = $property.$error['message'];
1✔
72
        }
73

UNCOV
74
        return implode("\n", $errors);
1✔
75
    }
76

77
    /**
78
     * Normalizes a JSON document.
79
     *
80
     * Specifically, we should ensure that:
81
     * 1. a JSON object is represented as a PHP object, not as an associative array.
82
     */
83
    private function normalizeJson(mixed $document): object|array
84
    {
UNCOV
85
        if (\is_scalar($document) || \is_object($document)) {
22✔
86
            return $document;
×
87
        }
88

UNCOV
89
        if (!\is_array($document)) {
22✔
90
            throw new \InvalidArgumentException('Document must be scalar, array or object.');
×
91
        }
92

UNCOV
93
        $document = json_encode($document, \JSON_THROW_ON_ERROR);
22✔
UNCOV
94
        if (!\is_string($document)) {
22✔
95
            throw new \UnexpectedValueException('JSON encode failed.');
×
96
        }
UNCOV
97
        $document = json_decode($document, null, 512, \JSON_THROW_ON_ERROR);
22✔
UNCOV
98
        if (!\is_array($document) && !\is_object($document)) {
22✔
99
            throw new \UnexpectedValueException('JSON decode failed.');
×
100
        }
101

UNCOV
102
        return $document;
22✔
103
    }
104
}
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