Skip to content

Commit

Permalink
Log file info tests. Conversion helper tests. Minor refactoring and r…
Browse files Browse the repository at this point in the history
…eformatting.
  • Loading branch information
alexboia committed Apr 29, 2024
1 parent 8d2b1ed commit fbb1122
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/io/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
79 changes: 79 additions & 0 deletions tests/test-ConversionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class ConversionHelperTests extends WP_UnitTestCase {
use GenericTestHelpers;

public function test_canConvert_whenInBytes() {
$formatted = Abp01_ConversionHelper::getByteSizeDescription(0);
$this->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);
}
}
143 changes: 143 additions & 0 deletions tests/test-LogFileInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class LogFileInfoTests extends WP_UnitTestCase {
use GenericTestHelpers;

const TEST_FILE_COUNT = 10;

protected function setUp(): void {
parent::setUp();
$this->_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);
}
}
4 changes: 2 additions & 2 deletions tests/test-SystemLogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fbb1122

Please sign in to comment.