/** * @test */ public function testGetHandler() { //Application instance is needed to setup container $app = new ApiApplication([ApiApplication::SETTING_API_VERSION => '1beta0']); $route = $this->getRouteFixture(); $mdHandleApiVersion = [$app, 'handleApiVersion']; $route->addMiddleware($mdHandleApiVersion); $handler1 = function ($id) { }; $handler2 = function ($id) { return 1; }; $this->assertInternalType('callable', $route->getHandler()); $route->addDefaults(['controller' => $handler1]); $this->assertSame($handler1, $route->getHandler()); $route->addDefaults($handler2); $this->assertSame($handler2, $route->getHandler()); //AbstractController based handler $route->setDefaults(['controller' => $app->getRouteHandler('Admin_Users:get')]); $this->assertInternalType('callable', $route->getHandler()); $this->assertInstanceOf('Scalr\\Api\\Service\\Admin\\V1beta0\\Controller\\Users', $route->getHandler()[0]); $this->assertEquals('get', $route->getHandler()[1]); }
<?php include __DIR__ . '/../src/prepend.inc.php'; use Scalr\Api\Rest\ApiApplication; ini_set('display_errors', '0'); ini_set('html_errors', '0'); register_shutdown_function(function () { $error = error_get_last(); if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_COMPILE_ERROR])) { if (!headers_sent()) { header("HTTP/1.0 500"); } } }); $app = new ApiApplication(); $app->setupRoutes(); $app->run();
/** * Gets Environment from the current request * * @return \Scalr\Model\Entity\Account\Environment */ public function getEnvironment() { return $this->app->getEnvironment(); }
/** * Calls api controllers' actions * * @param string $uri Request uri * @param string $method Http action * @param array $params optional Array of GET values * @param array $body optional POST fields => values * @param array $headers optional Custom headers * * @return ApiTestResponse Returns API test response */ public function request($uri, $method = Request::METHOD_GET, array $params = [], array $body = [], array $headers = []) { //Releases API container \Scalr::getContainer()->release('api'); $jsonEncodedBody = !empty($body) ? json_encode($body) : ''; $date = gmdate('Y-m-d\\TH:i:s\\Z'); $c11dQueryString = ''; if (!empty($params)) { ksort($params); $c11dQueryString = http_build_query($params, null, '&', PHP_QUERY_RFC3986); } $stringToSign = $method . "\n" . $date . "\n" . self::TEST_SCRIPT_NAME . $uri . "\n" . $c11dQueryString . "\n" . (!empty($jsonEncodedBody) ? $jsonEncodedBody : ''); $sig = base64_encode(hash_hmac('sha256', $stringToSign, static::$apiKeyEntity->secretKey, 1)); $defaultHeaders = ['Content-Type' => 'application/json; charset=utf-8', 'x-scalr-key-id' => static::$apiKeyEntity->keyId, 'x-scalr-date' => $date, 'x-scalr-signature' => 'V1-HMAC-SHA256 ' . $sig]; $headers = array_merge($defaultHeaders, $headers); $properties = ['REQUEST_METHOD' => $method, 'QUERY_STRING' => $c11dQueryString, 'REQUEST_URI' => self::TEST_HTTP_REFERER . $uri, 'raw.body' => $jsonEncodedBody, 'request.headers' => $headers, 'PATH_INFO' => $uri]; $properties = array_merge($this->getTestEnvProperties(), $properties); $app = new ApiApplication([ApiApplication::SETTING_ENV_MOCK => $properties]); $app->setupRoutes(); $app->call(); return new ApiTestResponse($app->response); }
/** * Gets current API request scope * * @return string Returns scope */ public function getScope() { return $this->app->getScope(); }