]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Merge pull request #1 from BookStackApp/master
[bookstack] / tests / Uploads / UsesImages.php
1 <?php namespace Tests\Uploads;
2
3
4 trait UsesImages
5 {
6     /**
7      * Get the path to our basic test image.
8      * @return string
9      */
10     protected function getTestImageFilePath()
11     {
12         return base_path('tests/test-data/test-image.png');
13     }
14
15     /**
16      * Get a test image that can be uploaded
17      * @param $fileName
18      * @return \Illuminate\Http\UploadedFile
19      */
20     protected function getTestImage($fileName)
21     {
22         return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238);
23     }
24
25     /**
26      * Get the raw file data for the test image.
27      * @return false|string
28      */
29     protected function getTestImageContent()
30     {
31         return file_get_contents($this->getTestImageFilePath());
32     }
33
34     /**
35      * Get the path for a test image.
36      * @param $type
37      * @param $fileName
38      * @return string
39      */
40     protected function getTestImagePath($type, $fileName)
41     {
42         return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
43     }
44
45     /**
46      * Uploads an image with the given name.
47      * @param $name
48      * @param int $uploadedTo
49      * @return \Illuminate\Foundation\Testing\TestResponse
50      */
51     protected function uploadImage($name, $uploadedTo = 0)
52     {
53         $file = $this->getTestImage($name);
54         return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
55     }
56
57     /**
58      * Delete an uploaded image.
59      * @param $relPath
60      */
61     protected function deleteImage($relPath)
62     {
63         $path = public_path($relPath);
64         if (file_exists($path)) {
65             unlink($path);
66         }
67     }
68
69 }