Exemplo n.º 1
0
 public function testNotImplemented()
 {
     $api = new Server();
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/error', 'OPTIONS');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
 }
Exemplo n.º 2
0
 public function testRestException()
 {
     $api = new Server();
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/error/abcd', 'GET');
     $response = $api->run($request);
     $this->assertEquals($response->getStatusCode(), 400);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":400,"message":"Bad id"}', $response->getContent());
 }
Exemplo n.º 3
0
 public function testResponseReady()
 {
     $emitter = new Emitter();
     $callback = function ($event, $parms) {
         $this->assertInstanceOf(Response::class, $parms['response']);
     };
     $emitter->addListener('response_ready', CallbackListener::fromCallable($callback));
     $api = new Server($emitter);
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/test/123', 'GET');
     $response = $api->run($request);
 }
Exemplo n.º 4
0
 public function testUnallowedSubentityRequests()
 {
     $api = new Server();
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/test/subentity/123', 'GET');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test/subentity', 'POST', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test/subentity/123', 'PUT', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
     $request = Request::create('/v1/test/subentity/123', 'DELETE');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test/subentity/123', 'PATCH', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => '123', 'data' => $data, 'subentity' => 'test']);
     $this->assertJsonStringEqualsJsonString('{"status_code":405,"message":"Not implemented"}', $response->getContent());
 }
Exemplo n.º 5
0
 public function testDirectoryServiceLoader()
 {
     $api = new Server();
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/test', 'GET');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['ids' => [1, 2, 3]]);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
     $request = Request::create('/v1/test/123', 'GET');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => '123']);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test', 'POST', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => 1000, 'data' => $data]);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test/123', 'PUT', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => '123', 'data' => $data]);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
     $request = Request::create('/v1/test/123', 'DELETE');
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => '123']);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
     $data = ['test' => '123'];
     $request = Request::create('/v1/test/123', 'PATCH', [], [], [], [], json_encode($data));
     $response = $api->run($request);
     $this->assertInstanceOf(JsonResponse::class, $response);
     $expected = json_encode(['id' => '123', 'data' => $data]);
     $this->assertJsonStringEqualsJsonString($expected, $response->getContent());
 }
Exemplo n.º 6
0
$log = new Logger('API');
$log->pushHandler(new StreamHandler('/tmp/api_log.txt', Logger::WARNING));
// We are interested in some events generated by the server
$emitter = new Emitter();
// This will be emitted right before control is dispatched to the actual service
$callback = function (AbstractEvent $event, $param = null) use($log) {
    // In the real world, you would (for an example) validate OAuth2 headers here
    $log->addNotice(serialize($param));
};
$emitter->addListener('dispatch', CallbackListener::fromCallable($callback));
// This will be emitted when an exception is going to be processed and "converted" into JSON
$callback = function ($event, $param) use($log) {
    $log->addError($param['exception']->getMessage());
};
$emitter->addListener('exception', CallbackListener::fromCallable($callback));
// This will be emitted when an PHP error (warning, notice, fatal) has happened and the processing
$callback = function ($event, $errorStr) use($log) {
    $log->addWarning($errorStr);
};
$emitter->addListener('error', CallbackListener::fromCallable($callback));
// Create the actual REST server
$api = new Server($emitter);
// Use the built-in error handlers that prevent any default PHP behavior and ensure all errors are
// cleanly returned as JSON. For logging purposes you should use the event listeners
$api->registerErrorHandlers();
// Register a REST endpoint/service called page (by default it will be placed in the v1 namespace)
// e.g. https://api.myapp.com/v1/page
$api->register('page', PageService::class);
// Process the request
$response = $api->run();
$response->send();