1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Handler; |
6
|
|
|
|
7
|
|
|
use Application\Handler\ExportAccountingReportHandler; |
8
|
|
|
use Application\Handler\ExportTransactionLinesHandler; |
9
|
|
|
use Application\Model\Account; |
10
|
|
|
use Application\Model\TransactionLine; |
11
|
|
|
use Application\Model\User; |
12
|
|
|
use Application\Repository\AccountRepository; |
13
|
|
|
use Cake\Chronos\Date; |
14
|
|
|
use Laminas\Diactoros\ServerRequest; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class ExcelExportHandlerTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
protected function setUp(): void |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
$responsible = new User(User::ROLE_RESPONSIBLE); |
23
|
|
|
$responsible->setLogin('accountant'); |
24
|
|
|
User::setCurrent($responsible); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testExportTransactionLines(): void |
28
|
|
|
{ |
29
|
|
|
// Query to generate the Excel file on disk |
30
|
|
|
$hostname = 'my-ichtus.lan'; |
31
|
|
|
$qb = _em()->getRepository(TransactionLine::class)->createQueryBuilder('tl'); |
32
|
|
|
$handler = new ExportTransactionLinesHandler($hostname); |
33
|
|
|
$url = $handler->generate($qb->getQuery()); |
34
|
|
|
|
35
|
|
|
$baseUrl = 'https://' . $hostname . '/export/transactionLines/'; |
36
|
|
|
|
37
|
|
|
self::assertStringStartsWith($baseUrl, $url); |
38
|
|
|
|
39
|
|
|
preg_match('#' . $baseUrl . '([0-9a-f]+)/(.+)#', $url, $m); |
40
|
|
|
|
41
|
|
|
// Make sure the XLSX file was generated on disk |
42
|
|
|
$fpath = 'data/tmp/excel/' . $m[1]; |
43
|
|
|
self::assertFileExists($fpath); |
44
|
|
|
$size = filesize($fpath); |
45
|
|
|
|
46
|
|
|
// Test handler to download the Excel file |
47
|
|
|
$handler = new ExportTransactionLinesHandler('my-ichtus.lan'); |
48
|
|
|
// Mock route parsing: /export/transactionLines/{key:[0-9a-f]+}/{name:.+\.xlsx} |
49
|
|
|
$request = (new ServerRequest())->withAttribute('key', $m[1])->withAttribute('name', $m[2]); |
50
|
|
|
|
51
|
|
|
$response = $handler->handle($request); |
52
|
|
|
|
53
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
54
|
|
|
self::assertStringContainsString('attachment; filename=Ichtus_compta_ecritures', $response->getHeaderLine('content-disposition')); |
55
|
|
|
self::assertEquals($size, $response->getHeaderLine('content-length')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testExportAccountingReport(): void |
59
|
|
|
{ |
60
|
|
|
$hostname = 'my-ichtus.lan'; |
61
|
|
|
|
62
|
|
|
$accountingConfig = [ |
63
|
|
|
'stockOfGoodsAccountCode' => 120, |
64
|
|
|
'salesAccountCode' => 3200, |
65
|
|
|
'bankAccountCode' => 1020, |
66
|
|
|
'customerDepositsAccountCode' => 2030, |
67
|
|
|
'stockVariationAccountCode' => 3900, |
68
|
|
|
'report' => [ |
69
|
|
|
'showAccountsWithZeroBalance' => true, |
70
|
|
|
'maxAccountDepth' => 2, |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
// Query to generate the Excel file on disk |
75
|
|
|
/** @var AccountRepository $repository */ |
76
|
|
|
$repository = _em()->getRepository(Account::class); |
77
|
|
|
$query = $repository->getRootAccountsQuery(); |
78
|
|
|
|
79
|
|
|
$handler = new ExportAccountingReportHandler($hostname, $accountingConfig); |
80
|
|
|
$handler->setDate(new Date('2019-12-31')); |
81
|
|
|
$url = $handler->generate($query); |
82
|
|
|
|
83
|
|
|
$baseUrl = 'https://' . $hostname . '/export/accountingReport/'; |
84
|
|
|
|
85
|
|
|
self::assertStringStartsWith($baseUrl, $url); |
86
|
|
|
|
87
|
|
|
preg_match('#' . $baseUrl . '([0-9a-f]+)/(.+)#', $url, $m); |
88
|
|
|
|
89
|
|
|
// Make sure the XLSX file was generated on disk |
90
|
|
|
$fpath = 'data/tmp/excel/' . $m[1]; |
91
|
|
|
self::assertFileExists($fpath); |
92
|
|
|
$size = filesize($fpath); |
93
|
|
|
|
94
|
|
|
// Test handler to download the Excel file |
95
|
|
|
$handler = new ExportAccountingReportHandler($hostname, $accountingConfig); |
96
|
|
|
$handler->setDate(new Date('2019-12-31')); |
97
|
|
|
// Mock route parsing: /export/accountingReport/{key:[0-9a-f]+}/{name:.+\.xlsx} |
98
|
|
|
$request = (new ServerRequest())->withAttribute('key', $m[1])->withAttribute('name', $m[2]); |
99
|
|
|
|
100
|
|
|
$response = $handler->handle($request); |
101
|
|
|
|
102
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
103
|
|
|
self::assertStringContainsString('attachment; filename=my-ichtus.lan_compta_rapport_2019-12-31.xlsx', $response->getHeaderLine('content-disposition')); |
104
|
|
|
self::assertEquals($size, $response->getHeaderLine('content-length')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|