|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Service\Exporter; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\Card; |
|
8
|
|
|
use Application\Service\Exporter\Pptx; |
|
9
|
|
|
use Application\Service\ImageResizer; |
|
10
|
|
|
use ApplicationTest\Traits\TestWithTransaction; |
|
11
|
|
|
use Imagine\Image\ImagineInterface; |
|
12
|
|
|
use PhpOffice\PhpPresentation\PhpPresentation; |
|
13
|
|
|
use PhpOffice\PhpPresentation\Reader\PowerPoint2007; |
|
14
|
|
|
use ZipArchive; |
|
15
|
|
|
|
|
16
|
|
|
class PptxTest extends AbstractWriter |
|
17
|
|
|
{ |
|
18
|
|
|
use TestWithTransaction; |
|
19
|
|
|
|
|
20
|
|
|
public function testWrite(): void |
|
21
|
|
|
{ |
|
22
|
|
|
global $container; |
|
23
|
|
|
|
|
24
|
|
|
$imagine = $container->get(ImagineInterface::class); |
|
25
|
|
|
|
|
26
|
|
|
$imageResizer = $this->createMock(ImageResizer::class); |
|
27
|
|
|
$imageResizer->expects(self::atLeastOnce()) |
|
28
|
|
|
->method('resize') |
|
29
|
|
|
// Never resize anything |
|
30
|
|
|
->willReturnCallback(fn (Card $card, int $maxHeight, bool $useWebp): string => $card->getPath()); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$writer = new Pptx($imageResizer, $imagine); |
|
33
|
|
|
$tempFile = tempnam('data/tmp/', 'Pptx'); |
|
34
|
|
|
|
|
35
|
|
|
$this->export($writer, $tempFile); |
|
36
|
|
|
|
|
37
|
|
|
$presentation = $this->readPresentation($tempFile); |
|
38
|
|
|
self::assertSame(1, $presentation->getSlideCount()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function readPresentation(string $filename): PhpPresentation |
|
42
|
|
|
{ |
|
43
|
|
|
// Assert that it is a valid ZIP file to prevent PhpSpreadsheet from hanging |
|
44
|
|
|
$zip = new ZipArchive(); |
|
45
|
|
|
$res = $zip->open($filename, ZipArchive::CHECKCONS); |
|
46
|
|
|
self::assertTrue($res, 'exported Excel should be a valid ZIP file'); |
|
47
|
|
|
$zip->close(); |
|
48
|
|
|
|
|
49
|
|
|
// Re-read it |
|
50
|
|
|
$reader = new PowerPoint2007(); |
|
51
|
|
|
$spreadsheet = $reader->load($filename); |
|
52
|
|
|
unlink($filename); |
|
53
|
|
|
|
|
54
|
|
|
return $spreadsheet; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.