Skip to content

Commit

Permalink
Switch to using joke API
Browse files Browse the repository at this point in the history
  • Loading branch information
pascualstromsnes committed Nov 17, 2019
1 parent d2e7a6b commit 79073df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"email": "pascual.stromsnes@gmail.com"
}
],
"require": {},
"require": {
"guzzlehttp/guzzle": "^6.4"
},
"autoload": {
"psr-4": {
"Pascualstromsnes\\ChuckNorrisJokes\\": "src/"
Expand Down
29 changes: 14 additions & 15 deletions src/JokeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

35 changes: 13 additions & 22 deletions tests/JokeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 79073df

Please sign in to comment.