| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  | <?php namespace Tests\Uploads; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  | use Illuminate\Http\UploadedFile; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  | trait UsesImages | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the path to our basic test image. | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTestImageFilePath() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return base_path('tests/test-data/test-image.png'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a test image that can be uploaded | 
					
						
							|  |  |  |      * @param $fileName | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  |      * @return UploadedFile | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function getTestImage($fileName) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  |         return new UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238, null, true); | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the raw file data for the test image. | 
					
						
							|  |  |  |      * @return false|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTestImageContent() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return file_get_contents($this->getTestImageFilePath()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the path for a test image. | 
					
						
							|  |  |  |      * @param $type | 
					
						
							|  |  |  |      * @param $fileName | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTestImagePath($type, $fileName) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Uploads an image with the given name. | 
					
						
							|  |  |  |      * @param $name | 
					
						
							|  |  |  |      * @param int $uploadedTo | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  |      * @param string $contentType | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  |      * @return \Illuminate\Foundation\Testing\TestResponse | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  |     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png') | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $file = $this->getTestImage($name); | 
					
						
							| 
									
										
										
										
											2019-03-21 07:59:55 +08:00
										 |  |  |         return $this->withHeader('Content-Type', $contentType) | 
					
						
							|  |  |  |             ->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []); | 
					
						
							| 
									
										
										
										
											2018-12-23 23:34:38 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Delete an uploaded image. | 
					
						
							|  |  |  |      * @param $relPath | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function deleteImage($relPath) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $path = public_path($relPath); | 
					
						
							|  |  |  |         if (file_exists($path)) { | 
					
						
							|  |  |  |             unlink($path); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |