|
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\FileScanFail; |
|
12
|
|
|
use Ikechukwukalu\Clamavfileupload\Events\FileScanPass; |
|
13
|
|
|
use Ikechukwukalu\Clamavfileupload\Listeners\ClamavFileUpload; |
|
14
|
|
|
use Ikechukwukalu\Clamavfileupload\Events\SavedFilesIntoDB; |
|
15
|
|
|
|
|
16
|
|
|
class EventsTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use RefreshDatabase; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public function test_fires_clamav_file_scan_event(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$event = Event::fake(); |
|
23
|
|
|
ClamavFileScan::dispatch(); |
|
24
|
|
|
$event->assertDispatched(ClamavFileScan::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function test_fires_clamav_queued_file_scan_event(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$event = Event::fake(); |
|
30
|
|
|
$file = __DIR__ . '/file/lorem-ipsum.pdf'; |
|
31
|
|
|
if (! is_dir($tmpDir = __DIR__ . '/tmp')) { |
|
32
|
|
|
mkdir($tmpDir, 0755, true); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$tmpFile = $tmpDir . '/lorem-ipsum.pdf'; |
|
36
|
|
|
$this->assertTrue(copy($file, $tmpFile)); |
|
37
|
|
|
|
|
38
|
|
|
ClamavQueuedFileScan::dispatch([$tmpFile], [], (string) Str::uuid()); |
|
39
|
|
|
$event->assertDispatched(ClamavQueuedFileScan::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_fires_clamav_file_upload_listener(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$event = Event::fake(); |
|
45
|
|
|
$file = __DIR__ . '/file/lorem-ipsum.pdf'; |
|
46
|
|
|
if (! is_dir($tmpDir = __DIR__ . '/tmp')) { |
|
47
|
|
|
mkdir($tmpDir, 0755, true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$tmpFile = $tmpDir . '/lorem-ipsum.pdf'; |
|
51
|
|
|
$this->assertTrue(copy($file, $tmpFile)); |
|
52
|
|
|
|
|
53
|
|
|
ClamavQueuedFileScan::dispatch([$tmpFile], [], (string) Str::uuid()); |
|
54
|
|
|
|
|
55
|
|
|
$event->assertListening( |
|
56
|
|
|
ClamavQueuedFileScan::class, |
|
57
|
|
|
ClamavFileUpload::class |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function test_fires_file_scan_fail_event(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$event = Event::fake(); |
|
64
|
|
|
FileScanFail::dispatch([]); |
|
65
|
|
|
$event->assertDispatched(FileScanFail::class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function test_fires_file_scan_pass_event(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$event = Event::fake(); |
|
71
|
|
|
FileScanPass::dispatch([]); |
|
72
|
|
|
$event->assertDispatched(FileScanPass::class); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function test_saved_files_into_db_event(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$event = Event::fake(); |
|
78
|
|
|
SavedFilesIntoDB::dispatch([]); |
|
79
|
|
|
$event->assertDispatched(SavedFilesIntoDB::class); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function test_clamav_is_not_running_event(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$event = Event::fake(); |
|
85
|
|
|
ClamavIsNotRunning::dispatch([]); |
|
86
|
|
|
$event->assertDispatched(ClamavIsNotRunning::class); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|