1 | <?php |
||
2 | |||
3 | namespace Ikechukwukalu\Clamavfileupload\Tests; |
||
4 | |||
5 | use Illuminate\Foundation\Testing\RefreshDatabase; |
||
6 | use Illuminate\Support\Facades\Event; |
||
7 | use Illuminate\Support\Str; |
||
8 | use Ikechukwukalu\Clamavfileupload\Events\ClamavFileScan; |
||
9 | use Ikechukwukalu\Clamavfileupload\Events\ClamavIsNotRunning; |
||
10 | use Ikechukwukalu\Clamavfileupload\Events\ClamavQueuedFileScan; |
||
11 | use Ikechukwukalu\Clamavfileupload\Events\FileForceDeleteFail; |
||
12 | use Ikechukwukalu\Clamavfileupload\Events\FileForceDeletePass; |
||
13 | use Ikechukwukalu\Clamavfileupload\Events\FileDeleteFail; |
||
14 | use Ikechukwukalu\Clamavfileupload\Events\FileDeletePass; |
||
15 | use Ikechukwukalu\Clamavfileupload\Events\FileScanFail; |
||
16 | use Ikechukwukalu\Clamavfileupload\Events\FileScanPass; |
||
17 | use Ikechukwukalu\Clamavfileupload\Events\QueuedDeleteAll; |
||
18 | use Ikechukwukalu\Clamavfileupload\Events\QueuedDeleteMultiple; |
||
19 | use Ikechukwukalu\Clamavfileupload\Events\QueuedDeleteOne; |
||
20 | use Ikechukwukalu\Clamavfileupload\Events\QueuedForceDeleteAll; |
||
21 | use Ikechukwukalu\Clamavfileupload\Events\QueuedForceDeleteMultiple; |
||
22 | use Ikechukwukalu\Clamavfileupload\Events\QueuedForceDeleteOne; |
||
23 | use Ikechukwukalu\Clamavfileupload\Events\SavedFilesIntoDB; |
||
24 | use Ikechukwukalu\Clamavfileupload\Listeners\ClamavFileUpload; |
||
25 | use Ikechukwukalu\Clamavfileupload\Listeners\FileDeleteAll; |
||
26 | use Ikechukwukalu\Clamavfileupload\Listeners\FileDeleteMultiple; |
||
27 | use Ikechukwukalu\Clamavfileupload\Listeners\FileDeleteOne; |
||
28 | use Ikechukwukalu\Clamavfileupload\Models\FileUpload as FileUploadModel; |
||
29 | |||
30 | class EventsTest extends TestCase |
||
31 | { |
||
32 | use RefreshDatabase; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
33 | |||
34 | public function test_fires_clamav_file_scan_event(): void |
||
35 | { |
||
36 | $event = Event::fake(); |
||
37 | ClamavFileScan::dispatch(); |
||
38 | $event->assertDispatched(ClamavFileScan::class); |
||
39 | } |
||
40 | |||
41 | public function test_fires_clamav_queued_file_scan_event(): void |
||
42 | { |
||
43 | $event = Event::fake(); |
||
44 | $file = __DIR__ . '/file/lorem-ipsum.pdf'; |
||
45 | if (! is_dir($tmpDir = __DIR__ . '/tmp')) { |
||
46 | mkdir($tmpDir, 0755, true); |
||
47 | } |
||
48 | |||
49 | $tmpFile = $tmpDir . '/lorem-ipsum.pdf'; |
||
50 | $this->assertTrue(copy($file, $tmpFile)); |
||
51 | |||
52 | ClamavQueuedFileScan::dispatch([$tmpFile], [], (string) Str::uuid()); |
||
53 | $event->assertDispatched(ClamavQueuedFileScan::class); |
||
54 | $event->assertListening( |
||
55 | ClamavQueuedFileScan::class, |
||
56 | ClamavFileUpload::class |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | public function test_fires_file_scan_events(): void |
||
61 | { |
||
62 | $event = Event::fake(); |
||
63 | FileScanFail::dispatch([]); |
||
64 | $event->assertDispatched(FileScanFail::class); |
||
65 | |||
66 | FileScanPass::dispatch([]); |
||
67 | $event->assertDispatched(FileScanPass::class); |
||
68 | } |
||
69 | |||
70 | public function test_saved_files_into_db_event(): void |
||
71 | { |
||
72 | $event = Event::fake(); |
||
73 | SavedFilesIntoDB::dispatch(new FileUploadModel, (string) str::uuid()); |
||
74 | $event->assertDispatched(SavedFilesIntoDB::class); |
||
75 | } |
||
76 | |||
77 | public function test_clamav_is_not_running_event(): void |
||
78 | { |
||
79 | $event = Event::fake(); |
||
80 | ClamavIsNotRunning::dispatch(); |
||
81 | $event->assertDispatched(ClamavIsNotRunning::class); |
||
82 | } |
||
83 | |||
84 | public function test_fires_file_delete_events(): void |
||
85 | { |
||
86 | $event = Event::fake(); |
||
87 | FileDeleteFail::dispatch([]); |
||
88 | $event->assertDispatched(FileDeleteFail::class); |
||
89 | |||
90 | FileDeletePass::dispatch([]); |
||
91 | $event->assertDispatched(FileDeletePass::class); |
||
92 | } |
||
93 | |||
94 | public function test_fires_file_force_delete_events(): void |
||
95 | { |
||
96 | $event = Event::fake(); |
||
97 | FileForceDeleteFail::dispatch([]); |
||
98 | $event->assertDispatched(FileForceDeleteFail::class); |
||
99 | |||
100 | FileForceDeletePass::dispatch([]); |
||
101 | $event->assertDispatched(FileForceDeletePass::class); |
||
102 | } |
||
103 | |||
104 | public function test_fires_queue_file_delete_events(): void |
||
105 | { |
||
106 | $event = Event::fake(); |
||
107 | QueuedDeleteAll::dispatch('abc'); |
||
108 | $event->assertDispatched(QueuedDeleteAll::class); |
||
109 | $event->assertListening( |
||
110 | QueuedDeleteAll::class, |
||
111 | FileDeleteAll::class |
||
112 | ); |
||
113 | |||
114 | QueuedDeleteMultiple::dispatch([], 'abc'); |
||
115 | $event->assertDispatched(QueuedDeleteMultiple::class); |
||
116 | $event->assertListening( |
||
117 | QueuedDeleteMultiple::class, |
||
118 | FileDeleteMultiple::class |
||
119 | ); |
||
120 | |||
121 | QueuedDeleteOne::dispatch(1, 'abc'); |
||
122 | $event->assertDispatched(QueuedDeleteOne::class); |
||
123 | $event->assertListening( |
||
124 | QueuedDeleteOne::class, |
||
125 | FileDeleteOne::class |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | public function test_fires_queue_file_force_delete_events(): void |
||
130 | { |
||
131 | $event = Event::fake(); |
||
132 | QueuedForceDeleteAll::dispatch('abc'); |
||
133 | $event->assertDispatched(QueuedForceDeleteAll::class); |
||
134 | $event->assertListening( |
||
135 | QueuedForceDeleteAll::class, |
||
136 | FileDeleteAll::class |
||
137 | ); |
||
138 | |||
139 | QueuedForceDeleteMultiple::dispatch([], 'abc'); |
||
140 | $event->assertDispatched(QueuedForceDeleteMultiple::class); |
||
141 | $event->assertListening( |
||
142 | QueuedForceDeleteMultiple::class, |
||
143 | FileDeleteMultiple::class |
||
144 | ); |
||
145 | |||
146 | QueuedForceDeleteOne::dispatch(1, 'abc'); |
||
147 | $event->assertDispatched(QueuedForceDeleteOne::class); |
||
148 | $event->assertListening( |
||
149 | QueuedForceDeleteOne::class, |
||
150 | FileDeleteOne::class |
||
151 | ); |
||
152 | } |
||
153 | } |
||
154 |