/** * @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); }
public function testGetBadResource() { try { $html = $this->curlUtils->getResource(PostController::BASE_URL . '/bad'); } catch (\Exception $e) { $this->assertEquals(0, $e->getCode()); $this->assertContains("Curl : last received HTTP code is 404", $e->getMessage()); return; } $this->fail(); }