public function testMalXml()
 {
     $anime = new Anime();
     $items = array();
     $output = "<?xml version=\"1.0\"?>\n<entry><episode>7</episode><status>1</status><score>9</score><downloaded_episodes>8</downloaded_episodes><storage_type>2</storage_type><storage_value>3.7</storage_value><times_rewatched>1</times_rewatched><rewatch_value>4</rewatch_value><date_start>01012015</date_start><date_finish>01022015</date_finish><priority>0</priority><comments>This is a comment.</comments><fansub_group>GG</fansub_group><tags>one,two,three</tags></entry>\n";
     $anime->setId(1);
     $anime->setWatchedEpisodes(7);
     $items[] = 'episodes';
     $anime->setWatchedStatus(1);
     $items[] = 'status';
     $anime->setScore(9);
     $items[] = 'score';
     $anime->setEpsDownloaded(8);
     $items[] = 'downloaded';
     $anime->setStorage(2);
     $items[] = 'storage';
     $anime->setStorageValue(3.7);
     $items[] = 'storageAmt';
     $anime->setRewatchCount(1);
     $items[] = 'rewatchCount';
     $anime->setRewatchValue(4);
     $items[] = 'rewatchValue';
     $anime->setWatchingStart(new \DateTime('20150101'));
     $items[] = 'start';
     $anime->setWatchingEnd(new \DateTime('20150102'));
     $items[] = 'end';
     $anime->setPriority(6);
     $items[] = 'priority';
     $anime->setPersonalComments('This is a comment.');
     $items[] = 'comments';
     $anime->setFansubGroup('GG');
     $items[] = 'fansubber';
     $anime->setPersonalTags('one,two,three');
     $items[] = 'tags';
     $this->assertEquals($output, $anime->MALApiXml($items));
 }
 /**
  * Update an anime on a user's list.
  *
  * Uses the contents of the HTTP Request to get the needed data for updating the
  * requested title. The user must have passed the basic authentication needs and the
  * PHP_AUTH_USER and PHP_AUTH_PW variables must be set. If so, the get variables of
  * "status", "episodes", and "score" are checked and used in the creation of an Anime
  * object. The object is used to make an XML document that is then posted to MyAnimeList.
  *
  * @param Request $request    Contains all the needed information to update the title.
  * @param int     $id         ID of the anime.
  * @param float   $apiVersion The API version for the request
  *
  * @return View
  */
 public function updateAction(Request $request, $id, $apiVersion)
 {
     // http://myanimelist.net/api/animelist/update/#{id}.xml
     //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) {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     }
     $anime = new Anime();
     $anime->setId($id);
     //Only use values we were sent for the Update XML
     $update_items = array();
     try {
         if ($request->request->get('status') !== null) {
             $anime->setWatchedStatus($request->request->get('status'));
             $update_items[] = 'status';
         }
         if ($request->request->get('episodes') !== null) {
             $anime->setWatchedEpisodes($request->request->get('episodes'));
             $update_items[] = 'episodes';
         }
         if ($request->request->get('score') !== null) {
             $anime->setScore($request->request->get('score'));
             $update_items[] = 'score';
         }
         //API 2 Items
         if ($apiVersion >= 2.0) {
             if ($request->request->get('start') !== null) {
                 $anime->setWatchingStart(DateTime::createFromFormat('Y-m-d', $request->request->get('start')));
                 //Needs to be DT!
                 $update_items[] = 'start';
             }
             if ($request->request->get('end') !== null) {
                 $anime->setWatchingEnd(DateTime::createFromFormat('Y-m-d', $request->request->get('end')));
                 //Needs to be DT!
                 $update_items[] = 'end';
             }
             if ($request->request->get('downloaded_eps') !== null) {
                 $anime->setEpsDownloaded($request->request->get('downloaded_eps'));
                 //Int
                 $update_items[] = 'downloaded';
             }
             if ($request->request->get('storage_type') !== null) {
                 $anime->setStorage($request->request->get('storage_type'));
                 //Int (see getStorage mappings)
                 $update_items[] = 'storage';
             }
             if ($request->request->get('storage_amt') !== null) {
                 $anime->setStorageValue($request->request->get('storage_amt'));
                 //Float, either in number of discs or in GB
                 $update_items[] = 'storageAmt';
             }
             if ($request->request->get('priority') !== null) {
                 $anime->setPriority($request->request->get('priority'));
                 $update_items[] = 'priority';
             }
             if ($request->request->get('rewatch_value') !== null) {
                 $anime->setRewatchValue($request->request->get('rewatch_value'));
                 //Int
                 $update_items[] = 'rewatchValue';
             }
             if ($request->request->get('tags') !== null) {
                 $anime->setPersonalTags($request->request->get('tags'));
                 //Comma-separated string
                 $update_items[] = 'tags';
             }
             if ($request->request->get('comments') !== null) {
                 $anime->setPersonalComments($request->request->get('comments'));
                 //Plain text string. No HTML.
                 $update_items[] = 'comments';
             }
             if ($request->request->get('fansubber') !== null) {
                 $anime->setFansubGroup($request->request->get('fansubber'));
                 //Plain string
                 $update_items[] = 'fansubber';
             }
             if ($request->request->get('is_rewatching') !== null) {
                 $anime->setRewatching($request->request->get('is_rewatching'));
                 //Bool - 0 = no, 1 = yes
                 $update_items[] = 'isRewatching';
             }
             if ($request->request->get('rewatch_count') !== null) {
                 $anime->setRewatchCount($request->request->get('rewatch_count'));
                 //Int
                 $update_items[] = 'rewatchCount';
             }
         }
     } catch (\Exception $e) {
         return $this->view(array('error' => $e->getMessage()), 500);
     }
     $xmlcontent = $anime->MALApiXml($update_items);
     $connection = $this->get('atarashii_api.communicator');
     try {
         $connection->sendXML('/api/animelist/update/' . $anime->getId() . '.xml', $xmlcontent, $username, $password);
         return $this->view('ok', 200);
     } catch (Exception\ClientErrorResponseException $e) {
         $view = $this->view(array('error' => 'unauthorized'), 401);
         $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
         return $view;
     } catch (Exception\ServerErrorResponseException $e) {
         //MAL broke API responses, so we have to check the content on the response to make sure
         //it actually was an error.
         $response = $e->getResponse()->getBody(true);
         if (stripos($response, 'Updated') === 0) {
             return $this->view('ok', 200);
         }
         return $this->view(array('error' => 'not-found'), 404);
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
 }