diff --git a/composer.json b/composer.json index 27a67f5..68c8a2b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ "email": "pascual.stromsnes@gmail.com" } ], - "require": {}, + "require": { + "guzzlehttp/guzzle": "^6.4" + }, "autoload": { "psr-4": { "Pascualstromsnes\\ChuckNorrisJokes\\": "src/" diff --git a/src/JokeFactory.php b/src/JokeFactory.php index 636bc94..20e4325 100644 --- a/src/JokeFactory.php +++ b/src/JokeFactory.php @@ -2,27 +2,26 @@ namespace Pascualstromsnes\ChuckNorrisJokes; +use GuzzleHttp\Client; + class JokeFactory { - protected $jokes = [ - 'The First rule of Chuck Norris is that you do not talk about Chuck Norris.', - 'Chuck Norris does not wear a condom. Because there is no such thing as protection from Chuck Norris.', - 'Chuck Norris\' tears cure cancer. Too bad he has never cried.', - 'Chuck Norris counted to infinity... Twice.', - 'If you can see Chuck Norris, he can see you. If you can\'t see Chuck Norris you may be only seconds away from death.', - 'When the Boogeyman goes to sleep at night he checks his closet for Chuck Norris.', - - ]; - - public function __construct(array $jokes = null) + const API_ENDPOINT = 'http://api.icndb.com/jokes/random'; + + protected $client; + + public function __construct(Client $client = null) { - if ($jokes) { - $this->jokes = $jokes; - } + $this->client = $client ?: new Client(); } public function getRandomJoke() { - return $this->jokes[array_rand($this->jokes)]; + $response = $this->client->get(self::API_ENDPOINT); + + $joke = json_decode($response->getBody()->getContents()); + + return $joke->value->joke; } } + \ No newline at end of file diff --git a/tests/JokeFactoryTest.php b/tests/JokeFactoryTest.php index cf50b39..348f562 100644 --- a/tests/JokeFactoryTest.php +++ b/tests/JokeFactoryTest.php @@ -2,40 +2,31 @@ namespace Pascualstromsnes\ChuckNorrisJokes\Tests; +use GuzzleHttp\Client; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\Exception\RequestException; +use PHPUnit\Framework\TestCase; use Pascualstromsnes\ChuckNorrisJokes\JokeFactory; -use PHPUnit\Framework\TestCase; class JokeFactoryTest extends TestCase { /** @test */ public function it_returns_a_random_joke() { - $jokes = new JokeFactory([ - 'This is a joke', + $mock = new MockHandler([ + new Response(200, [], '{ "type": "success", "value": { "id": 476, "joke": "Chuck Norris doesn\'t need a debugger, he just stares down the bug until the code confesses.", "categories": ["nerdy"] } }'), ]); - $joke = $jokes->getRandomJoke(); - - $this->assertSame('This is a joke', $joke); - } - - /** @test */ - public function it_returns_a_predefined_joke() - { - $chuckNorrisJokes = [ - 'The First rule of Chuck Norris is: you do not talk about Chuck Norris.', - 'Chuck Norris does not wear a condom. Because there is no such thing as protection from Chuck Norris.', - 'Chuck Norris\' tears cure cancer. Too bad he has never cried.', - 'Chuck Norris counted to infinity... Twice.', - 'If you can see Chuck Norris, he can see you. If you can\'t see Chuck Norris you may be only seconds away from death.', - 'When the Boogeyman goes to sleep at night he checks his closet for Chuck Norris.', - - ]; + $handler = HandlerStack::create($mock); - $jokes = new JokeFactory(); + $client = new Client(['handler' => $handler]); + + $jokes = new JokeFactory($client); $joke = $jokes->getRandomJoke(); - $this->assertContains($joke, $chuckNorrisJokes); + $this->assertSame('Chuck Norris doesn\'t need a debugger, he just stares down the bug until the code confesses.', $joke); } }