/** * @test * @expectedException \Jsor\HalClient\Exception\BadResponseException */ public function it_will_transform_bad_response_exception_without_response() { $guzzleClient = $this->getMock('GuzzleHttp\\ClientInterface'); $guzzleClient->expects($this->once())->method('send')->will($this->returnCallback(function ($request) { throw new GuzzleBadResponseException('Error', $request); })); $client = new HalClient('http://propilex.herokuapp.com', new Guzzle6HttpClient($guzzleClient)); $client->request('GET', '/'); }
/** * @test */ public function it_can_browse() { $client = new HalClient('http://propilex.herokuapp.com', new FixtureHttpClient()); $resource = $client->root(); $this->assertInstanceOf('Jsor\\HalClient\\HalResource', $resource); $this->assertEmpty($resource->getProperties()); $this->assertEmpty($resource->getResources()); $link = $resource->getFirstLink('documents'); $this->assertInstanceOf('Jsor\\HalClient\\HalLink', $link); $this->assertEquals($link->getHref(), 'http://propilex.herokuapp.com/documents'); $this->assertNull($resource->getProperty('fake')); $resource = $resource->getFirstLink('documents')->get([], ['query' => ['page' => 1]]); $this->assertInstanceOf('Jsor\\HalClient\\HalResource', $resource); $expected = ['page' => 1, 'limit' => 10, 'pages' => 1, 'total' => 3]; $this->assertEquals($expected, $resource->getProperties()); $this->assertEquals(1, $resource->getProperty('page')); $this->assertEquals(10, $resource->getProperty('limit')); $this->assertEquals(1, $resource->getProperty('pages')); $collection = $resource->getResource('documents'); $this->assertInternalType('array', $collection); $this->assertCount(3, $collection); foreach ($collection as $child) { $this->assertInstanceOf('Jsor\\HalClient\\HalResource', $child); $this->assertNotNull($child->getProperty('title')); $this->assertNotNull($child->getProperty('body')); $this->assertNotNull($child->getProperty('id')); $this->assertNull($child->getProperty('fake')); } $this->assertEquals('Test 1', $collection[0]->getProperty('title')); $this->assertEquals('Test 2', $collection[1]->getProperty('title')); $this->assertEquals('Test 3', $collection[2]->getProperty('title')); $document1 = $collection[0]->get(); $this->assertEquals('Test 1', $document1->getProperty('title')); $resource = $document1->getFirstLink('documents')->get(); $newResource = $resource->post(['body' => ['title' => 'Test 4', 'body' => 'Lorem ipsum']]); $this->assertEquals('Test 4', $newResource->getProperty('title')); $changedResource = $newResource->put(['body' => ['title' => 'Test 4 changed', 'body' => 'Lorem ipsum']]); $this->assertEquals('Test 4 changed', $changedResource->getProperty('title')); $deletedResource = $changedResource->delete(); $this->assertEmpty($deletedResource->getProperties()); }