From fbb1122c87bf4bc589881dfecb94b5b0a2aa523f Mon Sep 17 00:00:00 2001 From: Alexandru Boia Date: Mon, 29 Apr 2024 16:17:36 +0300 Subject: [PATCH] Log file info tests. Conversion helper tests. Minor refactoring and reformatting. --- lib/io/FileInfo.php | 5 +- tests/test-ConversionHelper.php | 79 ++++++++++++++++++ tests/test-LogFileInfo.php | 143 ++++++++++++++++++++++++++++++++ tests/test-SystemLogManager.php | 4 +- 4 files changed, 226 insertions(+), 5 deletions(-) create mode 100644 tests/test-ConversionHelper.php create mode 100644 tests/test-LogFileInfo.php diff --git a/lib/io/FileInfo.php b/lib/io/FileInfo.php index b41e3f2..bd5ec9f 100644 --- a/lib/io/FileInfo.php +++ b/lib/io/FileInfo.php @@ -122,8 +122,6 @@ public function tail($nLines = 100): string|null { $tailLines = explode("\n", $tailContents); $actualTailLineCount = count($tailLines); - - if (count($tailLines) > $nLines) { $tailLines = array_slice($tailLines, $actualTailLineCount - $nLines); $tailContents = join("\n", $tailLines); @@ -166,6 +164,7 @@ public function getFilePath(): string { } public function exists(): bool { - return is_readable($this->_filePath) && is_file($this->_filePath); + return is_readable($this->_filePath) + && is_file($this->_filePath); } } \ No newline at end of file diff --git a/tests/test-ConversionHelper.php b/tests/test-ConversionHelper.php new file mode 100644 index 0000000..ee7e526 --- /dev/null +++ b/tests/test-ConversionHelper.php @@ -0,0 +1,79 @@ +assertEquals('0B', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(500); + $this->assertEquals('500B', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1023); + $this->assertEquals('1023B', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1024); + $this->assertEquals('1024B', $formatted); + } + + public function test_canConvert_whenInKiloBytes() { + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1026); + $this->assertEquals('1KB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1028); + $this->assertEquals('1KB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(2048); + $this->assertEquals('2KB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1048576); + $this->assertEquals('1024KB', $formatted); + } + + public function test_canConvert_whenInMegaBytes() { + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1048577); + $this->assertEquals('1MB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(10485770); + $this->assertEquals('10MB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(11 * 1024 * 1024); + $this->assertEquals('11MB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(6.6 * 1024 * 1024); + $this->assertEquals('6.6MB', $formatted); + + $formatted = Abp01_ConversionHelper::getByteSizeDescription(1024 * 1024 * 1024); + $this->assertEquals('1024MB', $formatted); + } +} \ No newline at end of file diff --git a/tests/test-LogFileInfo.php b/tests/test-LogFileInfo.php new file mode 100644 index 0000000..d9df88a --- /dev/null +++ b/tests/test-LogFileInfo.php @@ -0,0 +1,143 @@ +_removeAllLogFiles(); + } + + private function _removeAllLogFiles() { + $env = $this->_getEnv(); + $logsDir = $env->getLogStorageDir(); + $this->_removeAllFiles($logsDir, '*.txt'); + $this->_removeAllFiles($logsDir, '*.log'); + } + + protected function tearDown(): void { + parent::tearDown(); + $this->_removeAllLogFiles(); + } + + public function test_canCheckIfDebugLogFiles_whenIsDebugLogFile() { + $config = abp01_get_logger_config(); + $debugLogFiles = $this->_generateDatedFilesWithContents('wpts_debug', + $config->getLoggerDirectory(), + self::TEST_FILE_COUNT); + + foreach ($debugLogFiles as $filePath) { + $fileInfo = new Abp01_Logger_FileInfo($filePath); + $this->assertTrue($fileInfo->isDebugLogFile()); + } + } + + public function test_canCheckIfDebugLogFiles_whenIsNotDebugLogFile() { + $config = abp01_get_logger_config(); + $bogusFiles = $this->_generateDatedFilesWithContents('bogus_prefix_file', + $config->getLoggerDirectory(), + self::TEST_FILE_COUNT); + + foreach ($bogusFiles as $filePath) { + $fileInfo = new Abp01_Logger_FileInfo($filePath); + $this->assertFalse($fileInfo->isDebugLogFile()); + } + } + + public function test_canCheckIfErrorLogFiles_whenIsErrorLogFile() { + $config = abp01_get_logger_config(); + $errorLogFiles = $this->_generateDatedFilesWithContents('wpts_error', + $config->getLoggerDirectory(), + self::TEST_FILE_COUNT); + + foreach ($errorLogFiles as $filePath) { + $fileInfo = new Abp01_Logger_FileInfo($filePath); + $this->assertTrue($fileInfo->isErrorLogFile()); + } + } + + public function test_canCheckIfErrorLogFiles_whenIsNotErrorLogFile() { + $config = abp01_get_logger_config(); + $bogusFiles = $this->_generateDatedFilesWithContents('bogus_prefix_file', + $config->getLoggerDirectory(), + self::TEST_FILE_COUNT); + + foreach ($bogusFiles as $filePath) { + $fileInfo = new Abp01_Logger_FileInfo($filePath); + $this->assertFalse($fileInfo->isErrorLogFile()); + } + } + + public function test_canConvertToPlainObject_debugLogFiles() { + $this->_runToPlainTestConversionTests('wpts_debug', + true, + false); + } + + private function _runToPlainTestConversionTests($filePrefix) { + $config = abp01_get_logger_config(); + $logFiles = $this->_generateDatedFilesWithContents($filePrefix, + $config->getLoggerDirectory(), + self::TEST_FILE_COUNT); + + foreach ($logFiles as $filePath) { + $fileInfo = new Abp01_Logger_FileInfo($filePath); + $asPlainObject = $fileInfo->asPlainObject(); + + $this->assertNotNull($asPlainObject); + $this->assertEquals($asPlainObject->id, + $fileInfo->id()); + $this->assertEquals($asPlainObject->fileName, + $fileInfo->getFileName()); + $this->assertEquals($asPlainObject->fileSize, + $fileInfo->getFileSize()); + $this->assertEquals($asPlainObject->formattedSize, + $fileInfo->getFormattedFileSize()); + $this->assertEquals($asPlainObject->lastModified, + $fileInfo->getLastModified()); + $this->assertEquals($asPlainObject->fomattedLastModified, + date('Y-m-d H:i:s', $asPlainObject->lastModified)); + $this->assertEquals($asPlainObject->isErrorLogFile, + $fileInfo->isErrorLogFile()); + $this->assertEquals($asPlainObject->isDebugLogFile, + $fileInfo->isDebugLogFile()); + } + } + + public function test_canConvertToPlainObject_errorLogFiles() { + $this->_runToPlainTestConversionTests('wpts_error', + false, + true); + } +} \ No newline at end of file diff --git a/tests/test-SystemLogManager.php b/tests/test-SystemLogManager.php index cfbf815..f631144 100644 --- a/tests/test-SystemLogManager.php +++ b/tests/test-SystemLogManager.php @@ -30,10 +30,10 @@ */ class SystemLogManagerTests extends WP_UnitTestCase { - const TEST_LOG_FILE_COUNT = 10; - use GenericTestHelpers; + const TEST_LOG_FILE_COUNT = 10; + protected function setUp(): void { parent::setUp(); $this->_removeAllLogConfigFilters();