Last active
August 29, 2015 14:18
-
-
Save tomphp/aa988344c4a77d52b3d5 to your computer and use it in GitHub Desktop.
Example of navigating HAL APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// An example of navigating HAL enabled JSON APIs in Behat tests | |
// using https://github.com/tomphp/hal-client | |
use TomPHP\HalClient\Client; | |
class APIContext | |
{ | |
// ... | |
/** | |
* @When I fetch and view the recipe :name by user :username | |
*/ | |
public function iFetchAndViewTheRecipeByUser($name, Username $username) | |
{ | |
$recipes = Client::create()->get($this->url . 'recipes')->getResource('recipes'); | |
$this->response = $recipes->findMatching([ | |
'name' => $name, | |
['resource', 'user', ['name' => $username->getValue()] | |
])[0]->getLink('self')->get(); | |
} | |
/** | |
* @Then I should be viewing the name, user, rating, measured ingredients and method of the recipe | |
*/ | |
public function iShouldBeViewingTheNameUserRatingMeasuredIngredientsAndMethodOfTheRecipe() | |
{ | |
Assert::assertEquals($this->getName(), $this->response->name->getValue()); | |
Assert::assertEquals($this->getUser()->view()['name'], $this->response->getResource('user')->name->getValue()); | |
Assert::assertEquals($this->getRating()->getValue(), $this->response->stars->getValue()); | |
Assert::assertEquals($this->getMethod()->getValue(), $this->response->method->getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment