Skip to content

Commit

Permalink
#9899 fixed usage stats file create/update issue for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Oct 25, 2024
1 parent ba487c6 commit c06b8b1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/jobs/statistics/ArchiveUsageStatsLogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use ReflectionClass;
use PKP\task\FileLoader;
use PKP\file\FileManager;
use PKP\tests\PKPTestCase;
use APP\statistics\StatisticsHelper;
use PKP\jobs\statistics\ArchiveUsageStatsLogFile;
Expand Down Expand Up @@ -58,6 +59,7 @@ public function testRunSerializedJob(): void

// we need to create a dummy file if not existed as to avoid mocking PHP's built in functions
$dummyFileName = $this->createDummyFileIfNeeded($archiveUsageStatsLogFileJob, 'loadId');
$this->createArchiveDirectoryIfRequired();

$this->assertNull($archiveUsageStatsLogFileJob->handle());

Expand Down Expand Up @@ -88,10 +90,40 @@ protected function createDummyFileIfNeeded(ArchiveUsageStatsLogFile $job, string
. DIRECTORY_SEPARATOR;

if (!file_exists($filePath . $fileName)) {

// create the 'FileLoader::FILE_LOADER_PATH_DISPATCH' directory if not exists
if (!file_exists($filePath)) {
$fileManager = new FileManager();
$fileManager->mkdirtree($filePath);
}

touch($filePath . $fileName);

file_put_contents($filePath . $fileName, $this->dummyFileContent);
return $fileName;
}

return null;
}

/**
* Create the archive path/directory as needed
*/
protected function createArchiveDirectoryIfRequired(): bool
{
$filePath = StatisticsHelper::getUsageStatsDirPath()
. DIRECTORY_SEPARATOR
. FileLoader::FILE_LOADER_PATH_ARCHIVE
. DIRECTORY_SEPARATOR;

if (file_exists($filePath)) {
return true;
}

// create the 'FileLoader::FILE_LOADER_PATH_ARCHIVE' directory if not exists
$fileManager = new FileManager();
$fileManager->mkdirtree($filePath);

return file_exists($filePath);
}
}

0 comments on commit c06b8b1

Please sign in to comment.