|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners; |
| 6 | + |
| 7 | +use Mockery as m; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 11 | +use Spiral\RoadRunnerLaravel\Events\Contracts\WithHttpRequest; |
| 12 | +use Spiral\RoadRunnerLaravel\Listeners\CleanupUploadedFilesListener; |
| 13 | + |
| 14 | +/** |
| 15 | + * @covers \Spiral\RoadRunnerLaravel\Listeners\CleanupUploadedFilesListener |
| 16 | + */ |
| 17 | +class CleanupUploadedFilesListenerTest extends AbstractListenerTestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * {@inheritdoc} |
| 21 | + */ |
| 22 | + public function testHandle(): void |
| 23 | + { |
| 24 | + $tmp_dir = $this->createTemporaryDirectory(); |
| 25 | + |
| 26 | + $this->assertNotFalse( |
| 27 | + \file_put_contents( |
| 28 | + $file_1_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_1_name = Str::random()), |
| 29 | + Str::random(), |
| 30 | + ) |
| 31 | + ); |
| 32 | + |
| 33 | + $this->assertNotFalse( |
| 34 | + \file_put_contents( |
| 35 | + $file_2_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_2_name = Str::random()), |
| 36 | + Str::random(), |
| 37 | + ) |
| 38 | + ); |
| 39 | + |
| 40 | + $this->assertNotFalse( |
| 41 | + \file_put_contents( |
| 42 | + $file_3_path = $tmp_dir . DIRECTORY_SEPARATOR . ($file_3_name = Str::random()), |
| 43 | + Str::random(), |
| 44 | + ) |
| 45 | + ); |
| 46 | + |
| 47 | + $request = Request::create('http://127.0.0.1:123/foo'); |
| 48 | + |
| 49 | + $request->files->add([ |
| 50 | + new UploadedFile($file_1_path, $file_1_name), |
| 51 | + new UploadedFile($file_2_path, $file_2_name), |
| 52 | + new UploadedFile($file_3_path, $file_3_name), |
| 53 | + ]); |
| 54 | + |
| 55 | + \rename($file_3_path, $file_3_new_path = $file_3_path . Str::random()); |
| 56 | + |
| 57 | + /** @var m\MockInterface|WithHttpRequest $event */ |
| 58 | + $event = m::mock(WithHttpRequest::class) |
| 59 | + ->makePartial() |
| 60 | + ->expects('httpRequest') |
| 61 | + ->atLeast() |
| 62 | + ->once() |
| 63 | + ->andReturn($request) |
| 64 | + ->getMock(); |
| 65 | + |
| 66 | + $this->assertFileExists($file_1_path); |
| 67 | + $this->assertFileExists($file_2_path); |
| 68 | + $this->assertFileExists($file_3_new_path); |
| 69 | + |
| 70 | + $this->listenerFactory()->handle($event); |
| 71 | + |
| 72 | + $this->assertFileDoesNotExist($file_1_path); |
| 73 | + $this->assertFileDoesNotExist($file_2_path); |
| 74 | + $this->assertFileExists($file_3_new_path); // still exists |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return CleanupUploadedFilesListener |
| 79 | + */ |
| 80 | + protected function listenerFactory(): CleanupUploadedFilesListener |
| 81 | + { |
| 82 | + return new CleanupUploadedFilesListener(); |
| 83 | + } |
| 84 | +} |
0 commit comments