CFDBErrorLogTest.php
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
include_once(dirname(dirname(__FILE__)) . '/CFDBErrorLog.php');
class CFDBErrorLogTest extends PHPUnit_Framework_TestCase {
public function testIsEmailAddress() {
$dateFormatter = new MockDateFormatter();
$err = new CFDBErrorLog($dateFormatter);
$email = 'me@here.com';
$this->assertTrue($err->isEmailAddress($email), $email);
$email = 'michael_d_simpson@gmail.com';
$this->assertTrue($err->isEmailAddress($email), $email);
$email = 'some-body@xxx.pl';
$this->assertTrue($err->isEmailAddress($email), $email);
}
public function testIsEmailAddress_isPath() {
$dateFormatter = new MockDateFormatter();
$err = new CFDBErrorLog($dateFormatter);
$this->assertFalse($err->isEmailAddress('/path/to/something'), '/path/to/something');
}
public function testIsEmailAddress_emptyString() {
$dateFormatter = new MockDateFormatter();
$err = new CFDBErrorLog($dateFormatter);
$this->assertFalse($err->isEmailAddress(''), 'empty string');
}
public function testIsEmailAddress_null() {
$dateFormatter = new MockDateFormatter();
$err = new CFDBErrorLog($dateFormatter);
$this->assertFalse($err->isEmailAddress(null), 'null');
}
public function testNoDestination() {
$dateFormatter = new MockDateFormatter();
$err = new CFDBErrorLog($dateFormatter);
$this->assertNull($err->getOutputFilePath());
$this->assertNull($err->getEmailAddress());
}
public function testFileDestination() {
$dateFormatter = new MockDateFormatter();
$path = '/path/to/file';
$err = new CFDBErrorLog($dateFormatter, $path);
$this->assertEquals($path, $err->getOutputFilePath());
$this->assertNull($err->getEmailAddress());
}
public function testEmailDestination() {
$dateFormatter = new MockDateFormatter();
$email = 'I_am_here@somwhere-here.com';
$err = new CFDBErrorLog($dateFormatter, $email);
$this->assertNull($err->getOutputFilePath());
$this->assertEquals($email, $err->getEmailAddress());
}
public function testWriteToFile() {
$dateFormatter = new MockDateFormatter();
$path = tempnam(sys_get_temp_dir(), 'CFDBLogTest-');
$err = new CFDBErrorLog($dateFormatter, $path);
$err->log("Test message");
$this->assertFileExists($path, 'file exists: ' . $path);
$fileContents = file_get_contents($path);
$this->assertEquals("CFDB Error (Today): Test message\n", $fileContents);
unlink($path);
}
public function testWriteExceptionToFile() {
$dateFormatter = new MockDateFormatter();
$path = tempnam(sys_get_temp_dir(), 'CFDBLogTest-');
$err = new CFDBErrorLog($dateFormatter, $path);
try {
throw new Exception('Test Exception');
} catch (Exception $ex) {
$err->logException($ex);
}
$this->assertFileExists($path, 'file exists: ' . $path);
$fileContents = file_get_contents($path);
$this->assertStringStartsWith('CFDB Error (Today): Test Exception', $fileContents);
unlink($path);
}
// Can only test this by running then checking email
/*
public function testEmailException() {
$dateFormatter = new MockDateFormatter();
$email = 'michael.d.simpson@gmail.com';
$err = new CFDBErrorLog($dateFormatter, $email);
try {
throw new Exception('Test Exception');
} catch (Exception $ex) {
$err->logException($ex);
}
}
*/
}
class MockDateFormatter implements CFDBDateFormatter {
public function formatDate($time) {
return 'Today';
}
}