Passed
Pull Request — master (#84)
by Joao
01:45
created

ApiTestCase::checkSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace ByJG\ApiTools;
4
5
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
6
use ByJG\ApiTools\Exception\GenericApiException;
7
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
8
use ByJG\ApiTools\Exception\InvalidDefinitionException;
9
use ByJG\ApiTools\Exception\InvalidRequestException;
10
use ByJG\ApiTools\Exception\NotMatchedException;
11
use ByJG\ApiTools\Exception\PathNotFoundException;
12
use ByJG\ApiTools\Exception\RequiredArgumentNotFound;
13
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Base test case for OpenAPI/Swagger validation.
19
 *
20
 * This class extends PHPUnit's TestCase and uses the OpenApiValidation trait
21
 * to provide OpenAPI/Swagger validation capabilities.
22
 *
23
 * If you need to extend a different base class, you can use the OpenApiValidation
24
 * trait directly instead of extending this class.
25
 *
26
 * @see OpenApiValidation
27
 */
28
abstract class ApiTestCase extends TestCase
29
{
30
    use OpenApiValidation;
31
32
    /**
33
     * Legacy method for backward compatibility.
34
     *
35
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
36
     * @param string $path The REST path call
37
     * @param int $statusExpected
38
     * @param string|array|null $query
39
     * @param array|string|null $requestBody
40
     * @param array $requestHeader
41
     * @return mixed
42
     * @throws DefinitionNotFoundException
43
     * @throws GenericApiException
44
     * @throws HttpMethodNotFoundException
45
     * @throws InvalidDefinitionException
46
     * @throws InvalidRequestException
47
     * @throws NotMatchedException
48
     * @throws PathNotFoundException
49
     * @throws RequiredArgumentNotFound
50
     * @throws StatusCodeNotMatchedException
51
     * @deprecated Since version 6.0, use sendRequest() with ApiRequester fluent interface instead. Will be removed in version 7.0
52
     */
53
    protected function makeRequest(
54
        string $method,
55
        string $path,
56
        int $statusExpected = 200,
57
        string|array|null $query = null,
58
        array|string|null $requestBody = null,
59
        array $requestHeader = []
60
    ): ResponseInterface {
61
        $this->checkSchema();
62
        return $this->getRequester()
63
            ->withSchema($this->schema)
0 ignored issues
show
Bug introduced by
It seems like $this->schema can also be of type null; however, parameter $schema of ByJG\ApiTools\AbstractRequester::withSchema() does only seem to accept ByJG\ApiTools\Base\Schema, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            ->withSchema(/** @scrutinizer ignore-type */ $this->schema)
Loading history...
64
            ->withMethod($method)
65
            ->withPath($path)
66
            ->withQuery($query)
67
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody can also be of type null; however, parameter $requestBody of ByJG\ApiTools\AbstractRequester::withRequestBody() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            ->withRequestBody(/** @scrutinizer ignore-type */ $requestBody)
Loading history...
68
            ->withRequestHeader($requestHeader)
69
            ->expectStatus($statusExpected)
70
            ->send();
71
    }
72
}
73