faster spreadsheet exporter based on opensout and other export improvements (#5238)

This commit is contained in:
Kevin Papst
2024-12-22 18:36:47 +01:00
committed by GitHub
parent c7f0508707
commit 136104d4b0
80 changed files with 2023 additions and 1699 deletions

View File

@@ -9,7 +9,6 @@
namespace App\Tests\Export\Renderer;
use App\Configuration\LocaleService;
use App\Entity\Activity;
use App\Entity\ActivityMeta;
use App\Entity\Customer;
@@ -26,41 +25,14 @@ use App\Event\CustomerMetaDisplayEvent;
use App\Event\ProjectMetaDisplayEvent;
use App\Event\TimesheetMetaDisplayEvent;
use App\Export\ExportRendererInterface;
use App\Export\TimesheetExportInterface;
use App\Repository\Query\TimesheetQuery;
use App\Twig\LocaleFormatExtensions;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
abstract class AbstractRendererTestCase extends KernelTestCase
{
/**
* @param class-string $classname
*/
protected function getAbstractRenderer(string $classname): ExportRendererInterface|TimesheetExportInterface
{
$languages = [
'en' => LocaleService::DEFAULT_SETTINGS
];
$security = $this->createMock(Security::class);
$security->expects($this->any())->method('getUser')->willReturn(new User());
$security->expects($this->any())->method('isGranted')->willReturn(true);
$translator = $this->createMock(TranslatorInterface::class);
$dateExtension = new LocaleFormatExtensions(new LocaleService($languages));
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new MetaFieldColumnSubscriber());
return new $classname($translator, $dateExtension, $dispatcher, $security);
}
protected function render(ExportRendererInterface $renderer): Response
{
$customer = new Customer('Customer Name');

View File

@@ -1,147 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Export\Renderer;
use App\Export\Renderer\CsvRenderer;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @covers \App\Export\Base\CsvRenderer
* @covers \App\Export\Base\AbstractSpreadsheetRenderer
* @covers \App\Export\Base\RendererTrait
* @covers \App\Export\Renderer\CsvRenderer
* @group integration
*/
class CsvRendererTest extends AbstractRendererTestCase
{
public function testConfiguration(): void
{
$sut = $this->getAbstractRenderer(CsvRenderer::class);
self::assertEquals('csv', $sut->getId());
self::assertEquals('csv', $sut->getTitle());
}
public static function getTestModel()
{
return [
['400', '2437.12', ' EUR 1,947.99 ', 7, 6, 1, 2, 2]
];
}
/**
* @dataProvider getTestModel
*/
public function testRender($totalDuration, $totalRate, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3): void
{
$sut = $this->getAbstractRenderer(CsvRenderer::class);
/** @var BinaryFileResponse $response */
$response = $this->render($sut);
$file = $response->getFile();
$prefix = date('Ymd');
self::assertEquals('text/csv', $response->headers->get('Content-Type'));
self::assertEquals('attachment; filename=' . $prefix . '-Customer_Name-project_name.csv', $response->headers->get('Content-Disposition'));
self::assertTrue(file_exists($file->getRealPath()));
$content = file_get_contents($file->getRealPath());
self::assertStringContainsString('"' . $expectedRate . '"', $content);
self::assertEquals($expectedRows, substr_count($content, PHP_EOL));
self::assertEquals($expectedDescriptions, substr_count($content, 'activity description'));
self::assertEquals($expectedUser1, substr_count($content, ',"kevin",'));
self::assertEquals($expectedUser3, substr_count($content, ',"hello-world",'));
self::assertEquals($expectedUser2, substr_count($content, ',"foo-bar",'));
ob_start();
$response->sendContent();
$content2 = ob_get_clean();
self::assertEquals($content, $content2);
self::assertFalse(file_exists($file->getRealPath()));
$all = [];
$rows = str_getcsv($content2, PHP_EOL);
foreach ($rows as $row) {
$all[] = str_getcsv($row);
}
$expected = [
'2019-06-16',
'12:00',
'12:06',
'400',
'0',
'',
'kevin',
'kevin',
'',
'Customer Name',
'project name',
'activity description',
'',
'',
'',
'foo,bar',
'',
' EUR 84.00 ',
'meta-bar',
'meta-bar2',
'customer-bar',
'',
'project-foo2',
'activity-bar',
'timesheet',
'work',
'A-0123456789',
'DE-9876543210',
'ORDER-123',
];
$expected2 = [
'2019-06-16',
'12:00',
'12:06',
'400',
'0',
'',
'nivek',
'nivek',
'',
'Customer Name',
'project name',
'activity description',
'',
'',
'',
'',
'',
' EUR -100.92',
'',
'',
'customer-bar',
'',
'project-foo2',
'activity-bar',
'timesheet',
'work',
'A-0123456789',
'DE-9876543210',
'ORDER-123',
];
self::assertEquals(7, \count($all));
self::assertEquals($expected, $all[5]);
self::assertEquals($expected2, $all[6]);
self::assertEquals(\count($expected), \count($all[0]));
self::assertEquals('foo', $all[4][15]);
}
}

View File

@@ -10,7 +10,7 @@
namespace App\Tests\Export\Renderer;
use App\Activity\ActivityStatisticService;
use App\Export\Renderer\HtmlRenderer;
use App\Export\Base\HtmlRenderer;
use App\Export\Renderer\HtmlRendererFactory;
use App\Project\ProjectStatisticService;
use PHPUnit\Framework\TestCase;
@@ -35,5 +35,6 @@ class HtmlRendererFactoryTest extends TestCase
self::assertInstanceOf(HtmlRenderer::class, $renderer);
self::assertEquals('foo', $renderer->getId());
self::assertEquals('print', $renderer->getTitle());
}
}

View File

@@ -11,7 +11,7 @@ namespace App\Tests\Export\Renderer;
use App\Activity\ActivityStatisticService;
use App\Entity\User;
use App\Export\Renderer\HtmlRenderer;
use App\Export\Base\HtmlRenderer;
use App\Project\ProjectStatisticService;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -24,7 +24,6 @@ use Twig\Environment;
/**
* @covers \App\Export\Base\HtmlRenderer
* @covers \App\Export\Base\RendererTrait
* @covers \App\Export\Renderer\HtmlRenderer
* @group integration
*/
class HtmlRendererTest extends AbstractRendererTestCase

View File

@@ -9,7 +9,7 @@
namespace App\Tests\Export\Renderer;
use App\Export\Renderer\PDFRenderer;
use App\Export\Base\PDFRenderer;
use App\Export\Renderer\PdfRendererFactory;
use App\Pdf\HtmlToPdfConverter;
use App\Project\ProjectStatisticService;

View File

@@ -9,7 +9,7 @@
namespace App\Tests\Export\Renderer;
use App\Export\Renderer\PDFRenderer;
use App\Export\Base\PDFRenderer;
use App\Pdf\HtmlToPdfConverter;
use App\Pdf\MPdfConverter;
use App\Project\ProjectStatisticService;
@@ -21,7 +21,6 @@ use Twig\Environment;
/**
* @covers \App\Export\Base\PDFRenderer
* @covers \App\Export\Base\RendererTrait
* @covers \App\Export\Renderer\PDFRenderer
* @group integration
*/
class PdfRendererTest extends AbstractRendererTestCase

View File

@@ -1,53 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Export\Renderer;
use App\Export\Renderer\XlsxRenderer;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* @covers \App\Export\Base\XlsxRenderer
* @covers \App\Export\Base\AbstractSpreadsheetRenderer
* @covers \App\Export\Base\RendererTrait
* @covers \App\Export\Renderer\XlsxRenderer
* @group integration
*/
class XlsxRendererTest extends AbstractRendererTestCase
{
public function testConfiguration(): void
{
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
self::assertEquals('xlsx', $sut->getId());
self::assertEquals('xlsx', $sut->getTitle());
}
public function testRender(): void
{
$sut = $this->getAbstractRenderer(XlsxRenderer::class);
/** @var BinaryFileResponse $response */
$response = $this->render($sut);
$file = $response->getFile();
$prefix = date('Ymd');
self::assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
self::assertEquals('attachment; filename=' . $prefix . '-Customer_Name-project_name.xlsx', $response->headers->get('Content-Disposition'));
self::assertTrue(file_exists($file->getRealPath()));
ob_start();
$response->sendContent();
$content2 = ob_get_clean();
self::assertNotEmpty($content2);
self::assertFalse(file_exists($file->getRealPath()));
}
}