From a327f581c5bc113e0f8ea01083e4e8913761d68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 24 Jan 2015 23:07:39 +0100 Subject: [PATCH] complete test for reports lifecycle close #289 typo in messages.fr.yml add validation file for reports --- Resources/config/validation.yml | 5 + Resources/translations/messages.fr.yml | 2 +- Tests/Controller/ReportControllerTest.php | 173 ++++++++++++++++++++-- 3 files changed, 166 insertions(+), 14 deletions(-) create mode 100644 Resources/config/validation.yml diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml new file mode 100644 index 000000000..e6920f438 --- /dev/null +++ b/Resources/config/validation.yml @@ -0,0 +1,5 @@ +Chill\ReportBundle\Entity\Report: + properties: + date: + - NotNull: ~ + - Date: ~ diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 270ac00e1..3b1940502 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -1,5 +1,5 @@ 'Report edit': "Edition d'un rapport" -'Save report': "Enregister le rapport" +'Save report': "Enregistrer le rapport" 'Reset report': "Remise à zéro" 'Add a report': "Ajout d'un rapport" 'Add report': 'Ajouter le rapport' diff --git a/Tests/Controller/ReportControllerTest.php b/Tests/Controller/ReportControllerTest.php index 95eb76436..dacba62e8 100644 --- a/Tests/Controller/ReportControllerTest.php +++ b/Tests/Controller/ReportControllerTest.php @@ -152,19 +152,7 @@ class ReportControllerTest extends WebTestCase $this->isFormAsExpected($addForm); - $c = static::$client->submit($addForm); - - $this->assertTrue(static::$client->getResponse()->isRedirect(), - "The next page is a redirection to the new report's view page"); - static::$client->followRedirect(); - - $this->assertRegExp("|/fr/person/".static::$person->getId()."/report/[0-9]*/view$|", - static::$client->getHistory()->current()->getUri(), - "The next page is a redirection to the new report's view page"); - return $addForm; - - } @@ -202,12 +190,171 @@ class ReportControllerTest extends WebTestCase } /** + * fill the form with correct data + * + * @param Form $form + */ + private function fillCorrectForm(Form $form) + { + $form->get('chill_reportbundle_report[date]')->setValue( + (new \DateTime())->format('d-m-Y')); + //get the first option values + $form->get('chill_reportbundle_report[user]')->setValue( + $form->get('chill_reportbundle_report[user]') + ->availableOptionValues()[0]); + + return $form; + } + + /** + * Test that setting a Null date redirect to an error page * * @param Form $form * @depends testNewReportPage */ public function testNullDate(Form $form) { - $this->assertTrue(true); + $filledForm = $this->fillCorrectForm($form); + $filledForm->get('chill_reportbundle_report[date]')->setValue(''); + + $crawler = static::$client->submit($filledForm); + + $this->assertFalse(static::$client->getResponse()->isRedirect()); + $this->assertGreaterThan(0, $crawler->filter('.error')->count()); + } + + /** + * Test that setting a Null date redirect to an error page + * + * @param Form $form + * @depends testNewReportPage + */ + public function testInvalidDate(Form $form) + { + $filledForm = $this->fillCorrectForm($form); + $filledForm->get('chill_reportbundle_report[date]')->setValue('invalid date value'); + + $crawler = static::$client->submit($filledForm); + + $this->assertFalse(static::$client->getResponse()->isRedirect()); + $this->assertGreaterThan(0, $crawler->filter('.error')->count()); + } + + /** + * Test that a incorrect value in user will show an error page + * + * @depends testNewReportPage + * @param Form $form + */ + public function testInvalidUser(Form $form) + { + $filledForm = $this->fillCorrectForm($form); + $select = $filledForm->get('chill_reportbundle_report[user]') + ->disableValidation() + ->setValue(-1); + + $crawler = static::$client->submit($filledForm); + + $this->assertFalse(static::$client->getResponse()->isRedirect()); + $this->assertGreaterThan(0, $crawler->filter('.error')->count()); + } + + /** + * Test the creation of a report + * + * @depends testNewReportPage + * @param Form $form + */ + public function testValidCreate(Form $addForm) + { + $filledForm = $this->fillCorrectForm($addForm); + $c = static::$client->submit($filledForm); + + $this->assertTrue(static::$client->getResponse()->isRedirect(), + "The next page is a redirection to the new report's view page"); + static::$client->followRedirect(); + + $this->assertRegExp("|/fr/person/".static::$person->getId()."/report/[0-9]*/view$|", + static::$client->getHistory()->current()->getUri(), + "The next page is a redirection to the new report's view page"); + + $matches = array(); + preg_match('|/report/([0-9]*)/view$|', + static::$client->getHistory()->current()->getUri(), $matches); + + return $matches[1]; + } + + + /** + * @depends testValidCreate + * @param int $reportId + */ + public function testList($reportId) + { + $crawler = static::$client->request('GET', sprintf('/fr/person/%s/report/list', + static::$person->getId())); + + $this->assertTrue(static::$client->getResponse()->isSuccessful()); + + $linkSee = $crawler->selectLink('Voir le rapport')->links(); + $this->assertGreaterThan(0, count($linkSee)); + $this->assertRegExp(sprintf('|/fr/person/%s/report/[0-9]*/view$|', + static::$person->getId(), $reportId), $linkSee[0]->getUri()); + + $linkUpdate = $crawler->selectLink('Mettre à jour le rapport')->links(); + $this->assertGreaterThan(0, count($linkUpdate)); + $this->assertRegExp(sprintf('|/fr/person/%s/report/[0-9]*/edit$|', + static::$person->getId(), $reportId), $linkUpdate[0]->getUri()); + + } + + /** + * Test the view of a report + * + * @depends testValidCreate + * @param int $reportId + */ + public function testView($reportId) + { + static::$client->request('GET', + sprintf('/fr/person/%s/report/%s/view', static::$person->getId(), $reportId)); + + $this->assertTrue(static::$client->getResponse()->isSuccessful(), + 'the page is shown'); + } + + /** + * test the update form + * + * @depends testValidCreate + * @param int $reportId + */ + public function testUpdate($reportId) + { + $crawler = static::$client->request('GET', + sprintf('/fr/person/%s/report/%s/edit', static::$person->getId(), $reportId)); + + $this->assertTrue(static::$client->getResponse()->isSuccessful()); + + $form = $crawler + ->selectButton('Enregistrer le rapport') + ->form(); + + $form->get('chill_reportbundle_report[date]')->setValue( + (new \DateTime('yesterday'))->format('d-m-Y')); + + static::$client->submit($form); + + $this->assertTrue(static::$client->getResponse()->isRedirect( + sprintf('/fr/person/%s/report/%s/view', + static::$person->getId(), $reportId))); + + $this->assertEquals(new \DateTime('yesterday'), static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('ChillReportBundle:Report') + ->find($reportId) + ->getDate()); + } }