Skip to content

Commit

Permalink
pkp/pkp-lib#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 21, 2024
1 parent 42e05ba commit 3a99dd7
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions tests/jobs/statistics/ProcessUsageStatsLogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

namespace APP\tests\jobs\statistics;

use APP\jobs\statistics\ProcessUsageStatsLogFile;
use APP\statistics\StatisticsHelper;
use Mockery;
use ReflectionClass;
use PKP\db\DAORegistry;
use PKP\task\FileLoader;
use PKP\file\FileManager;
use PKP\tests\PKPTestCase;
use ReflectionClass;
use APP\statistics\StatisticsHelper;
use APP\jobs\statistics\ProcessUsageStatsLogFile;

/**
* @runTestsInSeparateProcesses
Expand Down Expand Up @@ -75,6 +76,8 @@ public function testRunSerializedJob(): void
// we need to create a dummy file if not existed as to avoid mocking PHP's built in functions
$dummyFile = $this->createDummyFileIfNeeded($processUsageStatsLogFileJob, 'loadId');

$this->createArchiveDirectoryIfRequired();

$temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class)
->makePartial()
->shouldReceive([
Expand Down Expand Up @@ -142,10 +145,40 @@ protected function createDummyFileIfNeeded(ProcessUsageStatsLogFile $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 $filePath . $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 3a99dd7

Please sign in to comment.