/** * @covers ::parse */ public function testParse() { $epsContents = file_get_contents(__DIR__ . '/../InputSamples/anime-21-eps.html'); $apiVersion = '2.1'; $epsArray = EpsParser::parse($epsContents, $apiVersion); $this->assertInternalType('array', $epsArray); $eps = $epsArray[0]; /* As this test is against a static downloaded copy, we know what the exact values should be. As such, rather * than testing based on type for many of the items, we're checking for exact values. Obviously, this test * will need to be updated when the source file is re-downloaded to update any values. */ $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Episode', $eps); $this->assertEquals('I\'m Luffy! The Man Who\'s Gonna Be King of the Pirates!', $eps->getTitle()); $this->assertInstanceOf('\\DateTime', new \DateTime($eps->getAirDate())); $this->assertGreaterThan(0, $eps->getNumber()); $otherTitles = $eps->getOtherTitles(); $this->assertArrayHasKey('english', $otherTitles); $this->assertEquals('Ore wa Luffy! Kaizoku Ou ni Naru Otoko Da!', $otherTitles['english'][0]); $this->assertArrayHasKey('japanese', $otherTitles); $this->assertEquals('俺はルフィ!海賊王になる男だ!', $otherTitles['japanese'][0]); }
/** * Get the episodes of an anime. * * @param int $id The ID of the anime as assigned by MyAnimeList * @param Request $request HTTP Request object * * @return View */ public function getEpsAction($id, Request $request) { // http://myanimelist.net/anime/#{id}/_/episode $downloader = $this->get('atarashii_api.communicator'); $page = (int) $request->query->get('page'); if ($page < 0) { $page = 1; } try { $details = $downloader->fetch('/anime/' . $id . '/_/episode?offset=' . ($page * 100 - 100)); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } if (strpos($details, 'No episode information has been added to this title.') !== false) { return $this->view(array('error' => 'not-found'), 200); } else { $result = EpsParser::parse($details); $response = new Response(); $response->setPublic(); $response->setMaxAge(86400); //Two day $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag('anime/episodes/' . $id); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+86400 seconds'); //Two days $response->setExpires($date); $view = $this->view($result); $view->setResponse($response); $view->setStatusCode(200); return $view; } }