From 8e6f2d4355543f93710f1d1bf6c2db29ab3f5f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 21 Nov 2016 22:36:02 +0100 Subject: [PATCH] fix tests --- Tests/Search/EventSearchTest.php | 176 ++++++++++++++++++++++++++----- 1 file changed, 149 insertions(+), 27 deletions(-) diff --git a/Tests/Search/EventSearchTest.php b/Tests/Search/EventSearchTest.php index ab5e732c9..bb31a7bfa 100644 --- a/Tests/Search/EventSearchTest.php +++ b/Tests/Search/EventSearchTest.php @@ -167,7 +167,7 @@ class EventSearchTest extends WebTestCase 'q' => '@events' )); - $this->assertGreaterThan(2, $crawler->filter('table.events tr')->count(), + $this->assertGreaterThanOrEqual(2, $crawler->filter('table.events tr')->count(), 'assert than more than 2 tr are present'); } @@ -205,49 +205,171 @@ class EventSearchTest extends WebTestCase 'assert that the word "printemps" is present'); } - public function testSearchByDate() + public function testSearchByDateDateFromOnly() { // search with date from $crawler = $this->client->request('GET', '/fr/search', array( 'q' => '@events date-from:2016-05-30' )); + /* @var $dateFrom \DateTime the date from in DateTime */ + $dateFrom = \DateTime::createFromFormat("Y-m-d", "2016-05-30"); - $this->assertEquals( - 1, - $crawler->filter('tr:contains("Printemps")')->count(), - 'assert that the word "printemps" is present'); - $this->assertEquals( - 1, - $crawler->filter('tr:contains("Hiver")')->count(), - 'assert that the word "Hiver" is present'); + $dates = $this->iterateOnRowsToFindDate($crawler->filter("tr")); + foreach($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + } + + // click on link "Voir tous les résultats" + $crawlerAllResults = $this->client->click($crawler + ->selectLink("Voir tous les résultats")->link()); + $dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr")); + + foreach ($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + } + + //iterate on pagination + $crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) { + $page = $this->client->click($a->link()); + $dates = $this->iterateOnRowsToFindDate($page->filter("tr")); + + foreach($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + } + }); + } + + public function testSearchByDateDateBetween() + { // serach with date from **and** date-to $crawler = $this->client->request('GET', '/fr/search', array( 'q' => '@events date-from:2016-05-30 date-to:2016-06-20' )); - - $this->assertEquals( - 1, - $crawler->filter('tr:contains("Printemps")')->count(), - 'assert that the word "printemps" is present'); - $this->assertEquals( - 0, - $crawler->filter('tr:contains("Hiver")')->count(), - 'assert that the word "Hiver" is not present'); + + /* @var $dateFrom \DateTime the date from in DateTime */ + $dateFrom = \DateTime::createFromFormat("Y-m-d", "2016-05-30"); + $dateTo = \DateTime::createFromFormat("Y-m-d", "2016-06-20"); + + $dates = $this->iterateOnRowsToFindDate($crawler->filter("tr")); + + foreach($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + $this->assertLessThanOrEqual($dateTo, $date); + } + + // there should not have any other results, but if any other bundle + // add some other event, we go on next pages + + if ($crawler->selectLink("Voir tous les résultats")->count() == 0) { + return ; + } + + // click on link "Voir tous les résultats" + $crawlerAllResults = $this->client->click($crawler + ->selectLink("Voir tous les résultats")->link()); + $dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr")); + + foreach ($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + $this->assertLessThanOrEqual($dateTo, $date); + } + + //iterate on pagination + $crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) { + $page = $this->client->click($a->link()); + $dates = $this->iterateOnRowsToFindDate($page->filter("tr")); + + foreach($dates as $date) { + $this->assertGreaterThanOrEqual($dateFrom, $date); + $this->assertLessThanOrEqual($dateTo, $date); + } + }); + } + + public function testSearchByDateDateTo() + { // serach with date from **and** date-to $crawler = $this->client->request('GET', '/fr/search', array( 'q' => '@events date:2016-05-30' )); - $this->assertEquals( - 1, - $crawler->filter('tr:contains("Printemps")')->count(), - 'assert that the word "printemps" is present'); - $this->assertEquals( - 0, - $crawler->filter('tr:contains("Hiver")')->count(), - 'assert that the word "Hiver" is not present'); + /* @var $dateFrom \DateTime the date from in DateTime */ + $dateTo = \DateTime::createFromFormat("Y-m-d", "2016-05-30"); + + $dates = $this->iterateOnRowsToFindDate($crawler->filter("tr")); + + foreach($dates as $date) { + $this->assertLessThanOrEqual($dateTo, $date); + } + + if ($crawler->selectLink("Voir tous les résultats")->count() == 0) { + return ; + } + + // click on link "Voir tous les résultats" + $crawlerAllResults = $this->client->click($crawler + ->selectLink("Voir tous les résultats")->link()); + $dates = $this->iterateOnRowsToFindDate($crawlerAllResults->filter("tr")); + + foreach ($dates as $date) { + $this->assertLessThanOrEqual($dateTo, $date); + } + + //iterate on pagination + $crawlerAllResults->filter(".pagination a")->each(function($a, $i) use ($dateFrom) { + $page = $this->client->click($a->link()); + $dates = $this->iterateOnRowsToFindDate($page->filter("tr")); + + foreach($dates as $date) { + $this->assertLessThanOrEqual($dateTo, $date); + } + }); + + } + + /** + * this function iterate on row from results of events and return the content + * of the second column (which should contains the date) in DateTime objects + * + * @param \Symfony\Component\DomCrawler\Crawler $trs + * @return \DateTime[] + */ + private function iterateOnRowsToFindDate(\Symfony\Component\DomCrawler\Crawler $trs) + { + $months = array( + "janvier" => 1, + "février" => 2, + "mars" => 3, + "avril" => 4, + "mai" => 5, + "juin" => 6, + "juillet" => 7, + "août" => 8, + "septembre" => 9, + "octobre" => 10, + "novembre" => 11, + "décembre" => 12 + ); + + + $results = $trs->each(function($tr, $i) use ($months) { + // we skip the first row + if ($i > 0) { + // get the second node, which should contains a date + $tdDate = $tr->filter("td")->eq(1); + // transform the date, which should be in french, into a DateTime object + $parts = explode(" ", $tdDate->text()); + return \DateTime::createFromFormat("Y-m-d", $parts[2]. + "-".$months[$parts[1]]."-".$parts[0]); + } + }); + + // remove the first row + unset($results[0]); + + return $results; } /**