1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Media\Tests; |
4
|
|
|
|
5
|
|
|
use Modules\Media\Entities\File; |
6
|
|
|
use Modules\Media\Repositories\FileRepository; |
7
|
|
|
use Modules\Media\ValueObjects\MediaPath; |
8
|
|
|
|
9
|
|
|
class FileTest extends MediaTestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FileRepository |
13
|
|
|
*/ |
14
|
|
|
private $file; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
$this->resetDatabase(); |
21
|
|
|
|
22
|
|
|
$this->file = app(FileRepository::class); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** @test */ |
26
|
|
|
public function it_creates_a_file() |
27
|
|
|
{ |
28
|
|
|
$this->createFile('my/file/name.jpg'); |
29
|
|
|
|
30
|
|
|
$this->assertCount(1, $this->file->all()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @test */ |
34
|
|
|
public function it_should_return_media_path_value_object_on_path_attribtue() |
35
|
|
|
{ |
36
|
|
|
$file = $this->createFile('my/file/name.jpg'); |
37
|
|
|
|
38
|
|
|
$this->assertInstanceOf(MediaPath::class, $file->path); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @test */ |
42
|
|
|
public function it_should_cast_the_path_value_object_to_string() |
43
|
|
|
{ |
44
|
|
|
$file = $this->createFile('my/file/name.jpg'); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('http://localhost/my/file/name.jpg', $file->path_string); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @test */ |
50
|
|
|
public function it_should_guess_the_media_type_of_object() |
51
|
|
|
{ |
52
|
|
|
$file = $this->createFile('my/file/name.jpg'); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('image', $file->media_type); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @test */ |
58
|
|
|
public function it_can_check_if_file_is_an_image() |
59
|
|
|
{ |
60
|
|
|
$this->assertTrue($this->createFile('my/file/name.jpg')->isImage()); |
61
|
|
|
$this->assertTrue($this->createFile('my/file/name.png')->isImage()); |
62
|
|
|
$this->assertTrue($this->createFile('my/file/name.jpeg')->isImage()); |
63
|
|
|
$this->assertTrue($this->createFile('my/file/name.gif')->isImage()); |
64
|
|
|
$this->assertFalse($this->createFile('my/file/name.pdf')->isImage()); |
65
|
|
|
$this->assertFalse($this->createFile('my/file/name.doc')->isImage()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @test */ |
69
|
|
|
public function it_can_get_the_thumbnail() |
70
|
|
|
{ |
71
|
|
|
$file = $this->createFile('my/file/name.jpg'); |
72
|
|
|
|
73
|
|
|
$this->assertEquals('http://localhost/name_smallThumb.jpg', $file->getThumbnail('smallThumb')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @test */ |
77
|
|
|
public function it_wont_get_thumbnail_of_non_image_file() |
78
|
|
|
{ |
79
|
|
|
$file = $this->createFile('my/file/name.pdf'); |
80
|
|
|
|
81
|
|
|
$this->assertFalse($file->getThumbnail('smallThumb')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
private function createFile($fileName = 'random/name.jpg') |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return File::create([ |
87
|
|
|
'filename' => $fileName, |
88
|
|
|
'path' => config('asgard.media.config.files-path') . $fileName, |
89
|
|
|
'extension' => substr(strrchr($fileName, "."), 1), |
90
|
|
|
'mimetype' => 'image/jpg', |
91
|
|
|
'filesize' => '1024', |
92
|
|
|
'folder_id' => 0, |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function resetDatabase() |
97
|
|
|
{ |
98
|
|
|
// Relative to the testbench app folder: vendors/orchestra/testbench/src/fixture |
99
|
|
|
$migrationsPath = 'Database/Migrations'; |
100
|
|
|
// Makes sure the migrations table is created |
101
|
|
|
$this->artisan('migrate', [ |
102
|
|
|
'--database' => 'sqlite', |
103
|
|
|
'--path' => $migrationsPath, |
104
|
|
|
]); |
105
|
|
|
// We empty all tables |
106
|
|
|
$this->artisan('migrate:reset', [ |
107
|
|
|
'--database' => 'sqlite', |
108
|
|
|
]); |
109
|
|
|
// Migrate |
110
|
|
|
$this->artisan('migrate', [ |
111
|
|
|
'--database' => 'sqlite', |
112
|
|
|
'--path' => $migrationsPath, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.