Beispiel #1
0
 public function execute()
 {
     try {
         $route = $this->routes->findMatched();
         $result = $route->getOperation();
         $result = $result->exec();
     } catch (ApiException $e) {
         $result = null;
         $error = new OutputError(__CLASS__, (string) $e);
         $this->output->addError($error);
     }
     return $this->output->execute($result);
 }
Beispiel #2
0
 public function testExecute()
 {
     $request = new SampleRequest(['method' => Route::GET_METHOD, 'params' => ['word2' => 'world!'], 'path' => '/aaa/Hello']);
     $output = new SampleOutput();
     $collection = new RoutesCollection();
     $config = new RouteConfig();
     $config->request = $request;
     $config->operation = new ApiOperationFactory(function () use($request) {
         return new SampleOperation($request);
     });
     $config->methods = Route::GET_METHOD;
     $config->path = '/aaa/:word1';
     $route = new Route($config);
     $this->assertTrue($route->match());
     $collection->add($route);
     $service = new ApiService($collection, $request, $output);
     $result = $service->execute();
     $this->assertFalse($output->hasErrors());
     $this->assertEquals('Hello world!', $result);
 }
 public function testFindByAlias()
 {
     $request = new SampleRequest(['path' => '/']);
     $firstConfig = new RouteConfig();
     $firstConfig->request = $request;
     $firstConfig->path = '/other-path';
     $firstConfig->methods = [Route::GET_METHOD];
     $firstConfig->alias = 'first';
     $firstRoute = new Route($firstConfig);
     $this->collection->add($firstRoute);
     $secondConfig = new RouteConfig();
     $secondConfig->request = $request;
     $secondConfig->path = '/other-path2';
     $secondConfig->methods = [Route::GET_METHOD];
     $secondConfig->alias = 'second';
     $secondRoute = new Route($secondConfig);
     $this->collection->add($secondRoute);
     $found = $this->collection->findByAlias('first');
     $this->assertEquals($firstRoute, $found);
     $found = $this->collection->findByAlias('second');
     $this->assertEquals($secondRoute, $found);
 }