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));
}