]> BookStack Code Mirror - bookstack/blob - tests/Uploads/DrawioPngReaderTest.php
Drawings: Added class to extract drawio data from png files
[bookstack] / tests / Uploads / DrawioPngReaderTest.php
1 <?php
2
3 namespace Tests\Uploads;
4
5 use BookStack\Exceptions\DrawioPngReaderException;
6 use BookStack\Uploads\DrawioPngReader;
7 use Tests\TestCase;
8
9 class DrawioPngReaderTest extends TestCase
10 {
11     public function test_exact_drawing()
12     {
13         $file = $this->files->testFilePath('test.drawio.png');
14         $stream = fopen($file, 'r');
15
16         $reader = new DrawioPngReader($stream);
17         $drawing = $reader->extractDrawing();
18
19         $this->assertStringStartsWith('<mxfile ', $drawing);
20         $this->assertStringEndsWith("</mxfile>\n", $drawing);
21     }
22
23     public function test_extract_drawing_with_non_drawing_image_throws_exception()
24     {
25         $file = $this->files->testFilePath('test-image.png');
26         $stream = fopen($file, 'r');
27         $reader = new DrawioPngReader($stream);
28
29         $exception = null;
30         try {
31             $drawing = $reader->extractDrawing();
32         } catch (\Exception $e) {
33             $exception = $e;
34         }
35
36         $this->assertInstanceOf(DrawioPngReaderException::class, $exception);
37         $this->assertEquals($exception->getMessage(), 'Unable to find drawing data within PNG file');
38     }
39
40     public function test_extract_drawing_with_non_png_image_throws_exception()
41     {
42         $file = $this->files->testFilePath('test-image.jpg');
43         $stream = fopen($file, 'r');
44         $reader = new DrawioPngReader($stream);
45
46         $exception = null;
47         try {
48             $drawing = $reader->extractDrawing();
49         } catch (\Exception $e) {
50             $exception = $e;
51         }
52
53         $this->assertInstanceOf(DrawioPngReaderException::class, $exception);
54         $this->assertEquals($exception->getMessage(), 'File does not appear to be a valid PNG file');
55     }
56 }