diff options
Diffstat (limited to 'tests/UtilsTest.php')
-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() |