diff options
author | VirtualTam <virtualtam@flibidi.net> | 2016-01-16 15:48:26 +0100 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2016-01-16 16:07:16 +0100 |
commit | 1abe655597da7b3c5b59146c4351eef59f69514c (patch) | |
tree | bbd00b908dcfc329eea37ac32839f63044ceec3d /tests | |
parent | 92ba7b573f2833bd35c7eb2fc7fdbeb1a0ac7b44 (diff) | |
download | Shaarli-1abe655597da7b3c5b59146c4351eef59f69514c.tar.gz Shaarli-1abe655597da7b3c5b59146c4351eef59f69514c.tar.zst Shaarli-1abe655597da7b3c5b59146c4351eef59f69514c.zip |
Logging: move logm() from index.php to application/Utils.php
Relates to #436
Modifications:
- inject dependencies to global variables ($_SERVER, $GLOBALS)
- apply coding conventions
- add test coverage
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/UtilsTest.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 02eecda2..869a9695 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php | |||
@@ -18,6 +18,13 @@ class UtilsTest extends PHPUnit_Framework_TestCase | |||
18 | // Session ID hashes | 18 | // Session ID hashes |
19 | protected static $sidHashes = null; | 19 | protected static $sidHashes = null; |
20 | 20 | ||
21 | // Log file | ||
22 | protected static $testLogFile = 'tests.log'; | ||
23 | |||
24 | // Expected log date format | ||
25 | protected static $dateFormat = 'Y/m/d_H:i:s'; | ||
26 | |||
27 | |||
21 | /** | 28 | /** |
22 | * Assign reference data | 29 | * Assign reference data |
23 | */ | 30 | */ |
@@ -27,6 +34,65 @@ class UtilsTest extends PHPUnit_Framework_TestCase | |||
27 | } | 34 | } |
28 | 35 | ||
29 | /** | 36 | /** |
37 | * Resets test data before each test | ||
38 | */ | ||
39 | protected function setUp() | ||
40 | { | ||
41 | if (file_exists(self::$testLogFile)) { | ||
42 | unlink(self::$testLogFile); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Returns a list of the elements from the last logged entry | ||
48 | * | ||
49 | * @return list (date, ip address, message) | ||
50 | */ | ||
51 | protected function getLastLogEntry() | ||
52 | { | ||
53 | $logFile = file(self::$testLogFile); | ||
54 | return explode(' - ', trim(array_pop($logFile), '\n')); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Log a message to a file - IPv4 client address | ||
59 | */ | ||
60 | public function testLogmIp4() | ||
61 | { | ||
62 | $logMessage = 'IPv4 client connected'; | ||
63 | logm(self::$testLogFile, '127.0.0.1', $logMessage); | ||
64 | list($date, $ip, $message) = $this->getLastLogEntry(); | ||
65 | |||
66 | $this->assertInstanceOf( | ||
67 | 'DateTime', | ||
68 | DateTime::createFromFormat(self::$dateFormat, $date) | ||
69 | ); | ||
70 | $this->assertTrue( | ||
71 | filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false | ||
72 | ); | ||
73 | $this->assertEquals($logMessage, $message); | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Log a message to a file - IPv6 client address | ||
78 | */ | ||
79 | public function testLogmIp6() | ||
80 | { | ||
81 | $logMessage = 'IPv6 client connected'; | ||
82 | logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage); | ||
83 | list($date, $ip, $message) = $this->getLastLogEntry(); | ||
84 | |||
85 | $this->assertInstanceOf( | ||
86 | 'DateTime', | ||
87 | DateTime::createFromFormat(self::$dateFormat, $date) | ||
88 | ); | ||
89 | $this->assertTrue( | ||
90 | filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false | ||
91 | ); | ||
92 | $this->assertEquals($logMessage, $message); | ||
93 | } | ||
94 | |||
95 | /** | ||
30 | * Represent a link by its hash | 96 | * Represent a link by its hash |
31 | */ | 97 | */ |
32 | public function testSmallHash() | 98 | public function testSmallHash() |