ApiAssertionsTrait::assertMatchesItemJsonSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Testing;
10
11
use Gorynych\Testing\Constraint\ContainsSubset;
12
use Gorynych\Testing\Constraint\MatchesJsonSchema;
13
use Gorynych\Util\SchemaFactory;
0 ignored issues
show
Bug introduced by
The type Gorynych\Util\SchemaFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
trait ApiAssertionsTrait
16
{
17
    /**
18
     * Asserts that response status code matches given one
19
     */
20
    protected static function assertStatusCodeSame(int $statusCode, string $message = ''): void
21
    {
22
        static::assertSame($statusCode, static::$client->getResponse()->getStatusCode(), $message);
23
    }
24
25
    /**
26
     * Asserts that response contains given subset
27
     *
28
     * @param mixed $subset
29
     */
30
    protected static function assertContainsSubset($subset, bool $checkForObjectIdentity = false, string $message = ''): void
31
    {
32
        $constraint = new ContainsSubset($subset, $checkForObjectIdentity);
33
34
        static::assertThat(static::normalizeResponse(), $constraint, $message);
35
    }
36
37
    /**
38
     * Asserts that response matches given item resource JSON schema
39
     */
40
    protected static function assertMatchesItemJsonSchema(string $schemaClassName, ?int $checkMode = null, string $message = ''): void
41
    {
42
        self::matchesJsonSchema(static::normalizeResponse(), $schemaClassName, $checkMode, $message);
43
    }
44
45
    /**
46
     * Asserts that response matches given collection resource JSON schema
47
     */
48
    protected static function assertMatchesCollectionJsonSchema(string $schemaClassName, ?int $checkMode = null, string $message = ''): void
49
    {
50
        $data = static::normalizeResponse();
51
52
        self::matchesJsonSchema($data[0] ?? $data, $schemaClassName, $checkMode, $message);
53
    }
54
55
    /**
56
     * @param mixed[] $data
57
     */
58
    private static function matchesJsonSchema($data, string $schemaClassName, ?int $checkMode, string $message = ''): void
59
    {
60
        $schema = json_decode(static::$container->get(SchemaFactory::class)->create($schemaClassName));
61
        $constraint = new MatchesJsonSchema($schema, $checkMode);
62
63
        static::assertThat($data, $constraint, $message);
64
    }
65
}
66