/**
  * @covers ::parse
  */
 public function testParse()
 {
     $anime = file_get_contents(__DIR__ . '/../InputSamples/anime-1689-history.html');
     $manga = file_get_contents(__DIR__ . '/../InputSamples/manga-11977-history.html');
     $animeRes = HistoryParser::parse($anime, 21, 'anime');
     $this->assertNotNull($animeRes);
     $this->assertInternalType('array', $animeRes);
     $historyItem = $animeRes[0];
     $this->assertInstanceOf('Atarashii\\ApiBundle\\Model\\Anime', $historyItem['item']);
     $this->assertEquals('anime', $historyItem['type']);
     $mangaRes = HistoryParser::parse($manga, 21, 'manga');
     $this->assertNotNull($mangaRes);
     $this->assertInternalType('array', $mangaRes);
     $historyItem = $mangaRes[0];
     $this->assertInstanceOf('Atarashii\\ApiBundle\\Model\\Manga', $historyItem['item']);
     $this->assertEquals('manga', $historyItem['type']);
 }
 /**
  * Get the watching history of an anime or manga.
  *
  * @param int    $id          The ID of the anime or manga as assigned by MyAnimeList
  * @param string $requestType The anime or manga request string
  *
  * @return View
  */
 public function getHistoryAction($id, $requestType)
 {
     // http://myanimelist.net/ajaxtb.php?detailedaid=#{id}
     // http://myanimelist.net/ajaxtb.php?detailedmid=#{id}
     $downloader = $this->get('atarashii_api.communicator');
     //get the credentials we received
     $username = $this->getRequest()->server->get('PHP_AUTH_USER');
     $password = $this->getRequest()->server->get('PHP_AUTH_PW');
     //Don't bother making a request if the user didn't send any authentication
     if ($username === null || $password === null || $username === '' || $password === '') {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     }
     try {
         if (!$downloader->cookieLogin($username, $password)) {
             $view = $this->view(array('error' => 'unauthorized'), 401);
             $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
             return $view;
         }
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
     try {
         if ($requestType === 'anime') {
             $content = $downloader->fetch('/ajaxtb.php?detailedaid=' . $id);
         } else {
             $content = $downloader->fetch('/ajaxtb.php?detailedmid=' . $id);
         }
         Date::setTimeZone($downloader->fetch('/editprofile.php'));
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     } catch (Exception\ClientErrorResponseException $e) {
         $content = $e->getResponse();
     }
     if (strpos($content, 'Not logged in') !== false) {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     } else {
         $result = HistoryParser::parse($content, $id, $requestType);
         $view = $this->view($result);
         $view->setResponse(new Response());
         $view->setStatusCode(200);
     }
     return $view;
 }