Failed Conditions
Push — master ( 4dad36...cd91a2 )
by Adrien
13:00
created

ExcelExportHandlerTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 18
c 1
b 0
f 0
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testExportTransactionLines() 0 29 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Handler;
6
7
use Application\Handler\ExportTransactionLinesHandler;
8
use Application\Model\TransactionLine;
9
use ApplicationTest\Traits\TestWithTransactionAndUser;
10
use Laminas\Diactoros\ServerRequest;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Http\Server\RequestHandlerInterface;
13
14
class ExcelExportHandlerTest extends TestCase
15
{
16
    use TestWithTransactionAndUser;
17
18
    public function testExportTransactionLines(): void
19
    {
20
        // Query to generate the Excel file on disk
21
        $hostname = 'my-ichtus.lan';
22
        $qb = _em()->getRepository(TransactionLine::class)->createQueryBuilder('tl');
23
        $handler = new ExportTransactionLinesHandler($hostname);
24
        $url = $handler->generate($qb->getQuery());
25
26
        $baseUrl = 'https://' . $hostname . '/export/transactionLines/';
27
28
        self::assertStringStartsWith($baseUrl, $url);
29
30
        preg_match('#' . $baseUrl . '([0-9a-f]+)/(.+)#', $url, $m);
31
32
        // Make sure the XLSX file was generated on disk
33
        $fpath = 'data/tmp/excel/' . $m[1];
34
        self::assertFileExists($fpath);
35
        $size = filesize($fpath);
36
37
        // Test handler to download the Excel file
38
        $handler = new ExportTransactionLinesHandler('my-ichtus.lan');
39
        // Mock route parsing: /export/transactionLines/{key:[0-9a-f]+}/{name:.+\.xlsx}
40
        $request = (new ServerRequest())->withAttribute('key', $m[1])->withAttribute('name', $m[2]);
41
42
        $response = $handler->handle($request);
43
44
        self::assertEquals(200, $response->getStatusCode());
45
        self::assertStringContainsString('attachment; filename=Ichtus_compta_ecritures', $response->getHeaderLine('content-disposition'));
46
        self::assertEquals($size, $response->getHeaderLine('content-length'));
47
    }
48
}
49