diff --git a/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php b/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php index 75c5a42..1878717 100644 --- a/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php +++ b/tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php @@ -11,22 +11,21 @@ use org\bovigo\vfs\vfsStream; use OxidEsales\Eshop\Core\Utils; -use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEvent; +use OxidEsales\GdprOptinModule\UserData\Event\UserDataExportCleanupEventInterface; use OxidEsales\GdprOptinModule\UserData\Exception\UserDataFileDownloadException; use OxidEsales\GdprOptinModule\UserData\Service\UserDataFileDownloadService; use OxidEsales\GdprOptinModule\UserData\Service\UserDataFileDownloadServiceInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\Event; class UserDataFileDownloadServiceTest extends TestCase { public function testFilenameHeaderSet(): void { - $fileName = uniqid() . '.zip'; - $tempDirectory = vfsStream::setup('root', null, [ - $fileName => uniqid() - ]); - $filePath = $tempDirectory->url() . '/' . $fileName; + $filePath = $this->createVirtualFile( + fileName: $fileName = uniqid() . '.zip' + ); $shopUtilsSpy = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); $shopUtilsSpy->expects($this->exactly(2)) @@ -46,11 +45,9 @@ public function testFilenameHeaderSet(): void public function testCorrectFileContentShown(): void { - $fileName = uniqid() . '.zip'; - $tempDirectory = vfsStream::setup('root', null, [ - $fileName => $fileContent = uniqid() - ]); - $filePath = $tempDirectory->url() . '/' . $fileName; + $filePath = $this->createVirtualFile( + fileContent: $fileContent = uniqid() + ); $shopUtilsSpy = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); $shopUtilsSpy->expects($this->atLeastOnce()) @@ -61,10 +58,20 @@ public function testCorrectFileContentShown(): void $sut->downloadFile($filePath); } - public function testFileDoesntExist(): void + public function testFileDoesntExistThrowsException(): void + { + $notExistingFilePath = uniqid(); + + $sut = $this->getSut(); + + $this->expectException(UserDataFileDownloadException::class); + $sut->downloadFile($notExistingFilePath); + } + + public function testFileNotReadableThrowsException(): void { - $tempDirectory = vfsStream::setup('root', null, []); - $filePath = $tempDirectory->url() . '/' . uniqid(); + $filePath = $this->createVirtualFile(); + chmod($filePath, 0000); $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); $eventDispatcherSpy->expects($this->never()) @@ -76,46 +83,39 @@ public function testFileDoesntExist(): void $sut->downloadFile($filePath); } - public function testFileNotReadable(): void + public function testEventNotTriggeredIfFileDoesntExist(): void { - $fileName = uniqid() . '.zip'; - $tempDirectory = vfsStream::setup('root', null, [ - $fileName => uniqid() - ]); - $filePath = $tempDirectory->url() . '/' . $fileName; - chmod($filePath, 0000); + $notExistingFilePath = uniqid(); $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); - $eventDispatcherSpy->expects($this->never()) + $eventDispatcherSpy + ->expects($this->never()) ->method('dispatch'); $sut = $this->getSut(eventDispatcher: $eventDispatcherSpy); $this->expectException(UserDataFileDownloadException::class); - $sut->downloadFile($filePath); + $sut->downloadFile($notExistingFilePath); } - public function testEventDispatchTriggered(): void + public function testEventDispatchTriggeredIfFileExist(): void { - $fileName = uniqid() . '.zip'; - $tempDirectory = vfsStream::setup('root', null, [ - $fileName => uniqid() - ]); - $filePath = $tempDirectory->url() . '/' . $fileName; - - $shopUtilsSpy = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); - $shopUtilsSpy->expects($this->atLeastOnce()) - ->method('showMessageAndExit'); + $filePath = $this->createVirtualFile(); $eventDispatcherSpy = $this->createMock(EventDispatcherInterface::class); - $eventDispatcherSpy->expects($this->atLeastOnce()) + $eventDispatcherSpy->expects($this->once()) ->method('dispatch') - ->with($this->callback(function ($event) use ($filePath) { - return $event instanceof UserDataExportCleanupEvent - && $event->getFilePath() === $filePath; - })); - - $sut = $this->getSut(shopUtils: $shopUtilsSpy, eventDispatcher: $eventDispatcherSpy); + ->with( + $this->callback(function ($event) use ($filePath) { + return $event instanceof UserDataExportCleanupEventInterface + && $event instanceof Event + && $event->getFilePath() === $filePath; + }) + ); + + $sut = $this->getSut( + eventDispatcher: $eventDispatcherSpy + ); $sut->downloadFile($filePath); } @@ -132,4 +132,18 @@ protected function getSut( eventDispatcher: $eventDispatcher, ); } + + private function createVirtualFile( + string $fileName = null, + string $fileContent = null + ): string { + $fileName ??= uniqid() . '.zip'; + + $tempDirectory = vfsStream::setup('root', null, [ + $fileName => $fileContent ?? uniqid() + ]); + $filePath = $tempDirectory->url() . '/' . $fileName; + + return $filePath; + } }