diff options
Diffstat (limited to 'tests/UtilsTest.php')
-rw-r--r-- | tests/UtilsTest.php | 44 |
1 files changed, 14 insertions, 30 deletions
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 26d2a6b8..59dca75f 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php | |||
@@ -10,7 +10,7 @@ require_once 'application/Languages.php'; | |||
10 | /** | 10 | /** |
11 | * Unitary tests for Shaarli utilities | 11 | * Unitary tests for Shaarli utilities |
12 | */ | 12 | */ |
13 | class UtilsTest extends PHPUnit\Framework\TestCase | 13 | class UtilsTest extends \Shaarli\TestCase |
14 | { | 14 | { |
15 | // Log file | 15 | // Log file |
16 | protected static $testLogFile = 'tests.log'; | 16 | protected static $testLogFile = 'tests.log'; |
@@ -26,7 +26,7 @@ class UtilsTest extends PHPUnit\Framework\TestCase | |||
26 | /** | 26 | /** |
27 | * Assign reference data | 27 | * Assign reference data |
28 | */ | 28 | */ |
29 | public static function setUpBeforeClass() | 29 | public static function setUpBeforeClass(): void |
30 | { | 30 | { |
31 | self::$defaultTimeZone = date_default_timezone_get(); | 31 | self::$defaultTimeZone = date_default_timezone_get(); |
32 | // Timezone without DST for test consistency | 32 | // Timezone without DST for test consistency |
@@ -36,7 +36,7 @@ class UtilsTest extends PHPUnit\Framework\TestCase | |||
36 | /** | 36 | /** |
37 | * Reset the timezone | 37 | * Reset the timezone |
38 | */ | 38 | */ |
39 | public static function tearDownAfterClass() | 39 | public static function tearDownAfterClass(): void |
40 | { | 40 | { |
41 | date_default_timezone_set(self::$defaultTimeZone); | 41 | date_default_timezone_set(self::$defaultTimeZone); |
42 | } | 42 | } |
@@ -44,7 +44,7 @@ class UtilsTest extends PHPUnit\Framework\TestCase | |||
44 | /** | 44 | /** |
45 | * Resets test data before each test | 45 | * Resets test data before each test |
46 | */ | 46 | */ |
47 | protected function setUp() | 47 | protected function setUp(): void |
48 | { | 48 | { |
49 | if (file_exists(self::$testLogFile)) { | 49 | if (file_exists(self::$testLogFile)) { |
50 | unlink(self::$testLogFile); | 50 | unlink(self::$testLogFile); |
@@ -63,41 +63,25 @@ class UtilsTest extends PHPUnit\Framework\TestCase | |||
63 | } | 63 | } |
64 | 64 | ||
65 | /** | 65 | /** |
66 | * Log a message to a file - IPv4 client address | 66 | * Format a log a message - IPv4 client address |
67 | */ | 67 | */ |
68 | public function testLogmIp4() | 68 | public function testFormatLogIp4() |
69 | { | 69 | { |
70 | $logMessage = 'IPv4 client connected'; | 70 | $message = 'IPv4 client connected'; |
71 | logm(self::$testLogFile, '127.0.0.1', $logMessage); | 71 | $log = format_log($message, '127.0.0.1'); |
72 | list($date, $ip, $message) = $this->getLastLogEntry(); | ||
73 | 72 | ||
74 | $this->assertInstanceOf( | 73 | static::assertSame('- 127.0.0.1 - IPv4 client connected', $log); |
75 | 'DateTime', | ||
76 | DateTime::createFromFormat(self::$dateFormat, $date) | ||
77 | ); | ||
78 | $this->assertTrue( | ||
79 | filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false | ||
80 | ); | ||
81 | $this->assertEquals($logMessage, $message); | ||
82 | } | 74 | } |
83 | 75 | ||
84 | /** | 76 | /** |
85 | * Log a message to a file - IPv6 client address | 77 | * Format a log a message - IPv6 client address |
86 | */ | 78 | */ |
87 | public function testLogmIp6() | 79 | public function testFormatLogIp6() |
88 | { | 80 | { |
89 | $logMessage = 'IPv6 client connected'; | 81 | $message = 'IPv6 client connected'; |
90 | logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage); | 82 | $log = format_log($message, '2001:db8::ff00:42:8329'); |
91 | list($date, $ip, $message) = $this->getLastLogEntry(); | ||
92 | 83 | ||
93 | $this->assertInstanceOf( | 84 | static::assertSame('- 2001:db8::ff00:42:8329 - IPv6 client connected', $log); |
94 | 'DateTime', | ||
95 | DateTime::createFromFormat(self::$dateFormat, $date) | ||
96 | ); | ||
97 | $this->assertTrue( | ||
98 | filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false | ||
99 | ); | ||
100 | $this->assertEquals($logMessage, $message); | ||
101 | } | 85 | } |
102 | 86 | ||
103 | /** | 87 | /** |