Issues (3627)

CoreBundle/Tests/Unit/Helper/ExportHelperTest.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright   2020 Mautic Contributors. All rights reserved
7
 * @author      Mautic
8
 *
9
 * @link        http://mautic.org
10
 *
11
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Mautic\CoreBundle\Tests\Unit\Helper;
15
16
use Mautic\CoreBundle\Helper\ExportHelper;
17
use Symfony\Component\HttpFoundation\StreamedResponse;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
class ExportHelperTest extends \PHPUnit\Framework\TestCase
21
{
22
    /** @var TranslatorInterface */
23
    private $translatorInterfaceMock;
24
25
    private $dummyData = [
26
        [
27
            'id'        => 1,
28
            'firstname' => 'Mautibot',
29
            'lastname'  => 'Mautic',
30
            'email'     => '[email protected]',
31
        ],
32
        [
33
            'id'        => 2,
34
            'firstname' => 'Demo',
35
            'lastname'  => 'Mautic',
36
            'email'     => '[email protected]',
37
        ],
38
    ];
39
40
    protected function setUp(): void
41
    {
42
        $this->translatorInterfaceMock = $this->createMock(TranslatorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...slatorInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Translation\TranslatorInterface of property $translatorInterfaceMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * Test if exportDataAs() correctly generates a CSV file when we input some array data.
47
     */
48
    public function testCsvExport()
49
    {
50
        $helper = $this->getExportHelper();
51
        $stream = $helper->exportDataAs($this->dummyData, ExportHelper::EXPORT_TYPE_CSV, 'demo-file.csv');
52
53
        $this->assertInstanceOf(StreamedResponse::class, $stream);
54
        $this->assertSame(200, $stream->getStatusCode());
55
        $this->assertSame(false, $stream->isEmpty());
56
57
        ob_start();
58
        $stream->sendContent();
59
        $content = ob_get_contents();
60
        ob_end_clean();
61
62
        $lines = explode(PHP_EOL, $this->removeBomUtf8($content));
63
64
        $this->assertSame('"id","firstname","lastname","email"', $lines[0]);
65
        $this->assertSame('"1","Mautibot","Mautic","[email protected]"', $lines[1]);
66
        $this->assertSame('"2","Demo","Mautic","[email protected]"', $lines[2]);
67
    }
68
69
    /**
70
     * Test if exportDataAs() correctly generates an Excel file when we input some array data.
71
     */
72
    public function testExcelExport()
73
    {
74
        $helper = $this->getExportHelper();
75
        $stream = $helper->exportDataAs($this->dummyData, ExportHelper::EXPORT_TYPE_EXCEL, 'demo-file.xlsx');
76
77
        $this->assertInstanceOf(StreamedResponse::class, $stream);
78
        $this->assertSame(200, $stream->getStatusCode());
79
        $this->assertSame(false, $stream->isEmpty());
80
81
        ob_start();
82
        $stream->sendContent();
83
        $content = ob_get_contents();
84
        ob_end_clean();
85
86
        // We need to write to a temp file as PhpSpreadsheet can only read from files
87
        file_put_contents('./demo-file.xlsx', $content);
88
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('./demo-file.xlsx');
89
        unlink('./demo-file.xlsx');
90
91
        $this->assertSame(1, $spreadsheet->getActiveSheet()->getCell('A2')->getValue());
92
        $this->assertSame('Mautibot', $spreadsheet->getActiveSheet()->getCell('B2')->getValue());
93
        $this->assertSame(2, $spreadsheet->getActiveSheet()->getCell('A3')->getValue());
94
        $this->assertSame('Demo', $spreadsheet->getActiveSheet()->getCell('B3')->getValue());
95
    }
96
97
    private function getExportHelper(): ExportHelper
98
    {
99
        return new ExportHelper(
100
            $this->translatorInterfaceMock
101
        );
102
    }
103
104
    /**
105
     * Needed to remove the BOM that we add in our CSV exports (for UTF-8 parsing in Excel).
106
     */
107
    private function removeBomUtf8(string $s): string
108
    {
109
        if (substr($s, 0, 3) == chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))) {
0 ignored issues
show
It seems like hexdec('EF') can also be of type double; however, parameter $codepoint of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        if (substr($s, 0, 3) == chr(/** @scrutinizer ignore-type */ hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))) {
Loading history...
110
            return substr($s, 3);
111
        } else {
112
            return $s;
113
        }
114
    }
115
}
116