Skip to content

Commit fbf5dd9

Browse files
committed
Added tests for the cache
1 parent e8f0e35 commit fbf5dd9

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

src/imdb.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ public function film(string $filmId, array $options = []): array
8181
}
8282
}
8383

84-
// Check cache for film
85-
if ($cache->has($filmId)) {
86-
return $cache->get($filmId)->film;
84+
// If caching is enabled
85+
if ($options["cache"]) {
86+
// Check cache for film
87+
if ($cache->has($filmId)) {
88+
return $cache->get($filmId)->film;
89+
}
8790
}
8891

8992
// Load imdb film page and parse the dom
@@ -111,8 +114,11 @@ public function film(string $filmId, array $options = []): array
111114
$response->add("technical_specs", []);
112115
}
113116

114-
// Add result to the cache
115-
$cache->add($filmId, $response->return());
117+
// If caching is enabled
118+
if ($options["cache"]) {
119+
// Add result to the cache
120+
$cache->add($filmId, $response->return());
121+
}
116122

117123
// Return the response $store
118124
return $response->return();

tests/CacheTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use hmerritt\Imdb;
5+
use hmerritt\Response;
6+
use hmerritt\Cache;
7+
8+
class CacheTest extends TestCase {
9+
10+
public function testCache()
11+
{
12+
$cache = new Cache;
13+
$cache_testStore = [
14+
"title" => "Interstellar"
15+
];
16+
17+
$cache->add("test", $cache_testStore);
18+
$cache_testFile = $cache->get("test");
19+
20+
$this->assertEquals($cache_testStore, $cache_testFile->film);
21+
}
22+
23+
}

tests/ImdbTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ImdbTest extends TestCase {
99
public function testFilm()
1010
{
1111
$imdb = new Imdb;
12-
$film = $imdb->film('tt0816692');
12+
$film = $imdb->film('tt0816692', [ 'cache' => false ]);
1313

1414
$this->assertEquals('tt0816692', $film['id']);
1515
$this->assertEquals('Interstellar', $film['title']);
@@ -35,8 +35,9 @@ public function testFilmOptions()
3535
{
3636
$imdb = new Imdb;
3737
$film = $imdb->film('tt0065531', [
38-
"curlHeaders" => ['Accept-Language: de-DE, de;q=0.5'],
39-
"techSpecs" => false
38+
'cache' => false,
39+
'curlHeaders' => ['Accept-Language: de-DE, de;q=0.5'],
40+
'techSpecs' => false,
4041
]);
4142

4243
$this->assertEquals('Vier im roten Kreis', $film['title']);
@@ -48,7 +49,7 @@ public function test404Page()
4849
$imdb = new Imdb;
4950
$response = new Response;
5051

51-
$film = $imdb->film('ttest404');
52+
$film = $imdb->film('ttest404', [ 'cache' => false ]);
5253
$search = $imdb->search('ttest404040404004', [ 'category' => 'test404' ]);
5354

5455
$emptyResponse = [

tests/index.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@
88
use hmerritt\Imdb;
99

1010

11-
// Get url search param
12-
$q = $_GET["film"];
11+
// Get url params
12+
$type = $_GET["type"];
13+
$query = $_GET["q"];
1314

1415
// Initialise Imdb
15-
// Load film data
1616
$imdb = new Imdb();
17-
$film = $imdb->film($q); // tt0816692 tt8633464
1817

19-
// Return loaded film data
20-
echo json_encode($film, JSON_PRETTY_PRINT);
18+
switch($type)
19+
{
20+
case "film":
21+
$response = $imdb->film($query);
22+
break;
23+
24+
case "search":
25+
$response = $imdb->search($query);
26+
break;
27+
}
28+
29+
// -> Return loaded film data
30+
echo json_encode($response, JSON_PRETTY_PRINT);

0 commit comments

Comments
 (0)