コード例 #1
0
ファイル: 04-OptionsTest.php プロジェクト: sforsman/rest
 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());
 }
コード例 #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());
 }
コード例 #3
0
ファイル: 02-BasicEventTest.php プロジェクト: sforsman/rest
 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);
 }
コード例 #4
0
ファイル: 03-SubentityTest.php プロジェクト: sforsman/rest
 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());
 }
コード例 #5
0
ファイル: 00-BasicTest.php プロジェクト: sforsman/rest
 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());
 }