예제 #1
0
 public function testGetResource()
 {
     $client = $this->getMock('Ekino\\HalClient\\HttpClient\\HttpClientInterface');
     $client->expects($this->any())->method('get')->will($this->returnCallback(function ($url) {
         if ($url === '/') {
             return new HttpResponse(200, array('Content-Type' => 'application/hal+json'), file_get_contents(__DIR__ . '/../fixtures/entry_point.json'));
         }
         if ($url === 'http://propilex.herokuapp.com/documents') {
             return new HttpResponse(200, array('Content-Type' => 'application/hal+json'), file_get_contents(__DIR__ . '/../fixtures/documents.json'));
         }
     }));
     $entryPoint = new EntryPoint('/', $client);
     $resource = $entryPoint->get();
     $this->assertInstanceOf('Ekino\\HalClient\\Resource', $resource);
     $this->assertCount(1, $resource->getProperties());
     $this->assertEmpty($resource->getEmbedded());
     $link = $resource->getLink('p:documents');
     $this->assertInstanceOf('Ekino\\HalClient\\Link', $link);
     $this->assertEquals($link->getHref(), 'http://propilex.herokuapp.com/documents');
     $this->assertNull($resource->get('fake'));
     $resource = $resource->get('p:documents');
     $this->assertInstanceOf('Ekino\\HalClient\\Resource', $resource);
     $expected = array("page" => 1, "limit" => 10, "pages" => 1);
     $this->assertEquals($expected, $resource->getProperties());
     $this->assertEquals(1, $resource->get('page'));
     $this->assertEquals(10, $resource->get('limit'));
     $this->assertEquals(1, $resource->get('pages'));
     $collection = $resource->get('documents');
     $this->assertInstanceOf('Ekino\\HalClient\\ResourceCollection', $collection);
     $this->assertEquals(4, $collection->count());
     foreach ($collection as $child) {
         $this->assertInstanceOf('Ekino\\HalClient\\Resource', $child);
         $this->assertNotNull($child->get('title'));
         $this->assertNotNull($child->get('body'));
         $this->assertNotNull($child->get('id'));
         $this->assertNull($child->get('fake'));
     }
     $this->assertEquals('teste', $collection[1]->get('title'));
 }
예제 #2
0
 /**
  * Create a resource from link href.
  *
  * @param Link  $link
  * @param array $variables Required if the link is templated
  *
  * @return Resource
  *
  * @throws \RuntimeException When call with property "href" of Link is empty and sets variables
  *                           Or response server is invalid
  */
 public function getResource(Link $link, array $variables = array())
 {
     $response = $this->client->get($link->getHref($variables));
     if (!$response instanceof HttpResponse) {
         throw new \RuntimeException(sprintf('HttpClient does not return a valid HttpResponse object, given: %s', $response));
     }
     if ($response->getStatus() !== 200) {
         throw new \RuntimeException(sprintf('HttpClient does not return a status code, given: %s', $response->getStatus()));
     }
     return EntryPoint::parse($response, $this->client);
 }