File size: 3,458 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

declare(strict_types=1);

namespace Mautic\ReportBundle\Tests\Model;

use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Twig\Helper\DateHelper;
use Mautic\CoreBundle\Twig\Helper\FormatterHelper;
use Mautic\ReportBundle\Crate\ReportDataResult;
use Mautic\ReportBundle\Model\ExcelExporter;
use Mautic\ReportBundle\Tests\Fixtures;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

class ExcelExporterTest extends TestCase
{
    private ExcelExporter $excelExporter;

    private string|false $tmpFile;

    public function setUp(): void
    {
        $translator       = $this->createMock(TranslatorInterface::class);
        $translator->expects($this->any())
            ->method('trans')
            ->with('mautic.report.report.groupby.totals')
            ->willReturn('Totals');

        $dateHelperMock   =new DateHelper(
            'F j, Y g:i a T',
            'D, M d',
            'F j, Y',
            'g:i a',
            $translator,
            $this->createMock(CoreParametersHelper::class)
        );

        $formatterHelper  = new FormatterHelper($dateHelperMock, $translator);

        $this->excelExporter = new ExcelExporter($formatterHelper, $translator);
        $this->tmpFile       = tempnam(sys_get_temp_dir(), 'mautic_xlsx_export_test_');

        parent::setUp();
    }

    public function tearDown(): void
    {
        if (file_exists($this->tmpFile)) {
            unlink($this->tmpFile);
        }
    }

    /**
     * @return array<mixed>
     *
     * @throws Exception
     */
    private function getExcelResult(): array
    {
        /** @var Xlsx $objReader */
        $objReader   = IOFactory::createReader('Xlsx');
        $spreadsheet = $objReader->load($this->tmpFile);

        return $spreadsheet->getActiveSheet()->toArray();
    }

    public function testExport(): void
    {
        $reportData       = Fixtures::getValidReportResultWithAggregatedColumns();
        $reportDataResult = new ReportDataResult($reportData);

        $this->excelExporter->export($reportDataResult, 'mautic_xlsx_export_test', $this->tmpFile);

        $result = $this->getExcelResult();

        $expected = [
            [
                'ID',
                'Name',
                'SUM Read',
                'AVG Read',
                'COUNT Contact ID',
            ],
            [
                1,
                'Email 1',
                50,
                0.5,
                100,
            ],
            [
                2,
                'Email 2',
                10,
                0.1666,
                60,
            ],
            [
                'Totals',
                null,
                60,
                0.3333,
                160,
            ],
        ];

        $this->assertSame($expected, $result);
    }

    public function testExportEmptyData(): void
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('No report data to be exported');

        $reportData         = Fixtures::getValidReportResultWithAggregatedColumns();
        $reportData['data'] = [];
        $reportDataResult   = new ReportDataResult($reportData);

        $this->excelExporter->export($reportDataResult, 'mautic_xlsx_export_test', $this->tmpFile);
    }
}