コード例 #1
0
ファイル: HTMLTest.php プロジェクト: igraal/stats-table
 public function testDump()
 {
     $headers = array('date' => 'Date', 'hits' => 'Nb de visites', 'subscribers' => 'Nb inscrits', 'ratio' => 'Taux de transfo', 'revenues' => 'Revenus générés');
     $data = array(array('date' => '2014-01-01', 'hits' => '10', 'subscribers' => 2, 'ratio' => 0.2, 'revenues' => 45.321), array('date' => '2014-01-01', 'hits' => '20', 'subscribers' => 7, 'ratio' => 0.35, 'revenues' => 80.754));
     $dataTypes = array('date' => Format::DATE, 'hits' => Format::INTEGER, 'subscribers' => Format::INTEGER, 'ratio' => Format::PCT2, 'revenues' => Format::MONEY2);
     $aggregations = array('date' => 'Total', 'hits' => '30', 'subscribers' => '9', 'ratio' => '.3', 'revenues' => 126.075);
     $metaData = array('date' => array('description' => 'Date of the stats'), 'hits' => array('description' => 'Number of hits'));
     $aggregationsTypes = $dataTypes;
     $aggregationsTypes['date'] = Format::STRING;
     $aggregationsTypes['ratio'] = Format::PCT;
     $statsTable = new StatsTable($data, $headers, $aggregations, $dataTypes, $aggregationsTypes, $metaData);
     $dumper = new HTMLDumper();
     $html = $dumper->dump($statsTable);
     $doc = new \DOMDocument();
     $doc->loadXML($html);
     $expectedDoc = new \DOMDocument();
     $expectedDoc->load(__DIR__ . '/Fixtures/test.html');
     $this->assertEquals($expectedDoc, $doc);
     // Test with a custom template
     $dumper = new HTMLDumper(array('template' => 'custom.html.twig', 'templateFolder' => __DIR__ . '/Fixtures/template', 'templateOptions' => array('title' => 'My title test')));
     $html = $dumper->dump($statsTable);
     $doc = new \DOMDocument();
     $doc->loadXML($html);
     $expectedDoc = new \DOMDocument();
     $expectedDoc->load(__DIR__ . '/Fixtures/test-custom.html');
     $this->assertEquals($expectedDoc, $doc);
 }
コード例 #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $statsTable = $this->doExecute($input, $output);
     if (!$statsTable instanceof StatsTable) {
         throw new RuntimeException('StatsTable expected');
     }
     $dumper = null;
     if ($filename = $input->getOption('xls')) {
         $excelDumperOptions = array();
         if (method_exists($this, 'getExcelDumperOptions')) {
             $excelDumperOptions = $this->getExcelDumperOptions();
         }
         $dumper = new ExcelDumper($excelDumperOptions);
     } elseif ($filename = $input->getOption('csv')) {
         $csvDumperOptions = array();
         if (method_exists($this, 'getCSVDumperOptions')) {
             $csvDumperOptions = $this->getCSVDumperOptions();
         }
         $dumper = new CSVDumper($csvDumperOptions);
     } elseif ($filename = $input->getOption('html')) {
         $htmlDumperOptions = array();
         if (method_exists($this, 'getHTMLDumperOptions')) {
             $htmlDumperOptions = $this->getHTMLDumperOptions();
         }
         $dumper = new HTMLDumper($htmlDumperOptions);
     }
     if (null !== $dumper) {
         $contents = $dumper->dump($statsTable);
         if ($filename == '-') {
             $output->write($contents);
         } else {
             file_put_contents($filename, $contents);
         }
     } else {
         $tableHelper = new TableHelper();
         $tableHelper->setHeaders($statsTable->getHeaders());
         // Dump from CSV
         $dumper = new CSVDumper();
         $dumper->enableHeaders(false);
         $dumper->enableAggregation(false);
         $data = $dumper->dump($statsTable);
         $fp = fopen('php://temp', 'rw');
         fwrite($fp, $data);
         fseek($fp, 0, SEEK_SET);
         while ($line = fgetcsv($fp)) {
             $tableHelper->addRow($line);
         }
         if ($statsTable->getAggregations()) {
             $tableHelper->addRow($statsTable->getAggregations());
         }
         $tableHelper->render($output);
     }
 }