Ejemplo n.º 1
0
 /**
  * Call the given endpoint, transparently passing the call parameters to it.
  *
  * @param string $name
  * @param array $arguments
  * @throws UndefinedResourceException when the method called is not represented by a resource
  *
  * @return array|object
  */
 public function __call($name, $arguments)
 {
     if (!array_key_exists($name, $this->resources)) {
         throw UndefinedResourceException::create($name, array_keys($this->resources));
     }
     $resource = $this->resources[$name];
     $uri = $this->baseUrl . $this->router->generate($resource, ParameterHydrator::hydrateParameters($resource->getParameters(), $arguments));
     $request = new Request($resource->getMethod(), $uri);
     foreach ($this->listeners[self::LISTENER_BEFORE_REQUEST] as $listener) {
         /** @var callable $listener */
         if (is_callable($listener)) {
             $request = $listener($request);
         }
     }
     $response = $this->httpAdapter->send($request);
     foreach ($this->listeners[self::LISTENER_AFTER_RESPONSE] as $listener) {
         if (is_callable($listener)) {
             $result = $listener($request, $response);
         } elseif ($listener instanceof AfterResponseListenerInterface) {
             $result = $listener->process($request, $response);
         }
         if ($result) {
             $response = $result;
         }
     }
     foreach ($this->listeners[self::LISTENER_AFTER_DATA] as $listener) {
         if (is_callable($listener)) {
             return $listener($response, $resource);
         } elseif ($listener instanceof AfterDataListenerInterface) {
             return $listener->process($response, $resource);
         }
     }
     throw new \RuntimeException('No hydrators :(');
 }
 /**
  * @test
  */
 public function it_does_not_suggest_far_matches()
 {
     $exception = UndefinedResourceException::create('test', ['other', 'something']);
     static::assertNotContains('Did you mean', $exception->getMessage());
 }