Ejemplo n.º 1
0
 /**
  * @return JsonResponse
  */
 public function collect()
 {
     $toSave = array();
     $count = 0;
     $currentPageIndex = 0;
     while ($count < self::NB_POSTS) {
         $url = self::getUrl($currentPageIndex++);
         try {
             $html = $this->curlUtils->getResource($url);
             $posts = $this->postParserUtils->getPosts($html);
             foreach ($posts as $post) {
                 if ($count === self::NB_POSTS) {
                     break;
                 }
                 $postContent = $this->postParserUtils->parse($post);
                 $toSave[$count++] = $postContent;
             }
         } catch (\Exception $exception) {
             return new JsonResponse($exception);
         }
     }
     for ($i = 0; $i < count($toSave); $i++) {
         $this->dbal->insert(self::MODEL, $toSave[$i]);
     }
     $toReturn = count($toSave) . " posts have been added";
     return new JsonResponse($toReturn);
 }
Ejemplo n.º 2
0
 public function testParse()
 {
     $html = file_get_contents(__DIR__ . '/../../tests-resources/post.html');
     $posts = $this->postParserUtils->getPosts($html);
     $this->assertCount(1, $posts, "Only 1 post is available in the input file");
     $post = $this->postParserUtils->parse($posts[0]);
     $this->assertCount(3, $post, "parsed post should be a 3-length array");
     $this->assertEquals("This is the post content. VDM", $post['content']);
     $this->assertEquals("TestAuthor", $post['author']);
     $this->assertEquals("2016-01-13 13:33:00", $post['created']);
 }