Scalr\Tests\Functional\Api\V2\ApiTest::testApi PHP Method

testApi() public method

Test for Api endpoints Data provider prepares data for Api request SpecFile get responses definition from api specifications for each endpoint ResponseBodyConstraint compares Api definition with request
public testApi ( string $path, string | Exceptio\Exception $type, string $adapter, string $aclType, string $userType, string $method, integer $responseCode, array $params, array $filterable, array $body )
$path string Api endpoint
$type string | Exceptio\Exception Api specifications type
$adapter string Adapter name
$aclType string Acl type for test
$userType string UserType for test
$method string HTTP method
$responseCode integer HTTP code
$params array Array of path parameters
$filterable array Array of GET parameters
$body array Array of POST parameters
    public function testApi($path, $type, $adapter, $aclType, $userType, $method, $responseCode, $params, $filterable, $body)
    {
        if ($type instanceof Exception) {
            $this->markTestIncomplete(sprintf('Class or file name %s. Exception message %s', $path, $type->getMessage()));
        }
        if ($type === 'account') {
            $specFile = static::$accountSpec;
            $baseUrl = '/api/' . static::$apiVersion . '/account';
        } else {
            $specFile = static::$userSpec;
            $baseUrl = '/api/' . static::$apiVersion . '/user';
            $params['envId'] = static::$testEnvId;
        }
        $this->setTestAcl($aclType);
        $this->setUserType($userType);
        $method = strtoupper($method);
        $adapter = $this->getAdapter($adapter);
        $entityClass = $adapter->getEntityClass();
        $requestUrl = $baseUrl . $this->matchApiUrl($path, $params);
        $apiResp = $specFile->getResponse($path, $method, $responseCode);
        if ($method == Request::METHOD_GET && !isset($filterable[ApiController::QUERY_PARAM_MAX_RESULTS])) {
            // Tests should be considerably fast.
            $filterable[ApiController::QUERY_PARAM_MAX_RESULTS] = static::$maxResults ? static::$maxResults : 10;
        }
        $response = $this->request($requestUrl, $method, $filterable, $body);
        $envelope = $response->getBody();
        $this->assertEquals($responseCode, $response->status, $this->printResponseError($response));
        $checkRules = false;
        switch ($method) {
            case Request::METHOD_GET:
                if (200 == $responseCode) {
                    $this->assertFilterableProperties($requestUrl, $apiResp);
                    if (count($filterable) > 1) {
                        $this->assertNotEmpty($envelope->data);
                    }
                    $checkRules = true;
                }
                break;
            case Request::METHOD_POST:
                if (201 == $responseCode || 200 === $responseCode) {
                    $objectName = $apiResp->getObjectEntity()->getObjectName();
                    $pk = $this->mapApiResponsePk($path, $objectName);
                    $id = ApiController::getBareId($envelope->data, $pk);
                    if (!empty($id)) {
                        $dbObject = $this->getDbObject($objectName, $id, $entityClass, $params);
                        $this->assertNotEmpty($dbObject, sprintf('Object %s should exist in db', $objectName));
                        $this->assertObjectEqualsEntity($envelope->data, $dbObject, $adapter);
                    }
                    $checkRules = true;
                }
                break;
            case Request::METHOD_PATCH:
                if (200 == $responseCode) {
                    $objectName = $apiResp->getObjectEntity()->getObjectName();
                    $pk = $this->mapApiResponsePk($path, $objectName);
                    $id = ApiController::getBareId($envelope->data, $pk);
                    if (!empty($id)) {
                        $dbObject = $this->getDbObject($objectName, $id, $entityClass, $params);
                        $this->assertNotEmpty($dbObject, sprintf('Object %s should exist in db', $objectName));
                        $this->assertObjectEqualsEntity($envelope->data, $dbObject, $adapter);
                    }
                    $checkRules = true;
                }
                break;
        }
        if ($checkRules) {
            $this->checkAdapterRules($apiResp->getObjectEntity(), $adapter->getRules());
        }
        $this->assertThat($envelope, new ResponseBodyConstraint($apiResp));
    }