Skip to content

Commit

Permalink
OXDEV-8733 Cleanup and improve the UserDataFileDownloadService
Browse files Browse the repository at this point in the history
* Splitted event triggering tests from download trigger tests
* Improve readability of the test file

Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
  • Loading branch information
Sieg committed Oct 9, 2024
1 parent c37502e commit 59cc94d
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions tests/Unit/UserData/Service/UserDataFileDownloadServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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);
}
Expand All @@ -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;
}
}

0 comments on commit 59cc94d

Please sign in to comment.