Improving Test refs #401

This commit is contained in:
Marc Ducobu 2015-02-27 14:01:30 +01:00
parent 26ed31e89e
commit 9b867ea0e0

View File

@ -52,10 +52,19 @@ class ReportControllerTest extends WebTestCase
private static $user; private static $user;
/**
*
* @var \Doctrine\ORM\EntityManagerInterface
*/
private static $em;
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
static::bootKernel(); static::bootKernel();
static::$em = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager');
//get a random person //get a random person
$persons = static::$kernel->getContainer() $persons = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
@ -111,7 +120,7 @@ class ReportControllerTest extends WebTestCase
*/ */
public function testChooseReportModelPage(Link $link) public function testChooseReportModelPage(Link $link)
{ {
// When I click on "adda report" link in menu // When I click on "add a report" link in menu
$crawlerAddAReportPage = static::$client->click($link); $crawlerAddAReportPage = static::$client->click($link);
$form = $crawlerAddAReportPage->selectButton("Créer un nouveau rapport")->form(); $form = $crawlerAddAReportPage->selectButton("Créer un nouveau rapport")->form();
@ -384,16 +393,15 @@ class ReportControllerTest extends WebTestCase
} }
/** /**
* Test the export ReportAction : * Test the export form for selecting the type of report to export :
* - follow the given link ( export/report/select/type ) * - follow the given link ( export/report/select/type )
* - choose randomly a type of report (CustomFieldsGroup) * - choose randomly a type of report (CustomFieldsGroup)
* - submit the form * - submit the form
* - check if a csv file is well receiv
* - check if the number of row of the csv file is as expected (number of report of this type + 1)
* *
* @return Integer The id of the type of report selected (CFGroup)
* @depends testLinkToTheExportReport * @depends testLinkToTheExportReport
*/ */
public function testExportAction(Link $link) public function testFormForExportAction(Link $link)
{ {
$crawlerExportReportPage = static::$client->click($link); $crawlerExportReportPage = static::$client->click($link);
@ -420,31 +428,52 @@ class ReportControllerTest extends WebTestCase
static::$client->followRedirect(); static::$client->followRedirect();
// TO DO CREATE A NEW FUNCTION return $cfGroupId;
}
/**
* Test the output of the export action :
* - check if a csv file is well received
* - check if the csv is well formated (if each row has the same number of
* cells)
* - check if the number of data rows (not the header) of the csv file is
* as expected (number of report of this type)
*
* @param Int The id of the type of report selected (CFGroup)
* @depends testFormForExportAction
*/
public function testCSVExportAction($cfGroupId)
{
$response = static::$client->getResponse(); $response = static::$client->getResponse();
$this->assertTrue( $this->assertTrue(
strpos($response->headers->get('Content-Type'),'text/csv') !== false, strpos($response->headers->get('Content-Type'),'text/csv') !== false,
'The csv file is well received'); 'The csv file is well received');
echo gettype(static::$kernel->getContainer()
->get('doctrine.orm.entity_manager'));
$content = $response->getContent(); $content = $response->getContent();
$rows = (explode("\n", $content)); $rows = str_getcsv($content, "\n");
$em = static::$kernel->getContainer()->get('doctrine.orm.entity_manager'); $headerRow = array_pop($rows);
$header = str_getcsv($headerRow);
$headerSize = sizeof($header);
$numberOfRows = 0;
$cfGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cfGroupId); foreach ($rows as $row) {
$reports = $em->getRepository('ChillReportBundle:Report')->findByCFGroup($cfGroup); $rowContent = str_getcsv($row);
if($rows[sizeof($rows) -1] == "") { $this->assertTrue(
array_pop($rows); sizeof($rowContent) == $headerSize,
'Each row of the csv contains the good number of elements ('
. 'regarding to the first row');
$numberOfRows ++;
} }
$cfGroup = static::$em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cfGroupId);
$reports = static::$em->getRepository('ChillReportBundle:Report')->findByCFGroup($cfGroup);
$this->assertTrue( $this->assertTrue(
sizeof($rows) == (sizeof($reports) + 1), $numberOfRows == sizeof($reports),
'The csv file has a number of row equivalent than the number of reports in the db' 'The csv file has a number of row equivalent than the number of reports in the db'
); );
} }