Skip to content

Commit 578ff00

Browse files
Log unit testing
1 parent c4b8f49 commit 578ff00

File tree

3 files changed

+218
-6
lines changed

3 files changed

+218
-6
lines changed

src/Log/Mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function phpbuStart($settings)
142142
*/
143143
public function phpbuEnd(Result $result)
144144
{
145-
$allGood = $result->wasSuccessful() && $result->noneSkipped() && $result->noneFailed();
145+
$allGood = $result->allOk();
146146

147147
if (!$this->sendOnlyOnError || !$allGood) {
148148
$header = $this->getHeaderHtml($result);

tests/phpbu/Log/MailTest.php

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace phpbu\App\Log;
33

44
/**
5-
* Array utility test
5+
* Mail Test
66
*
77
* @package phpbu
88
* @subpackage tests
@@ -103,10 +103,8 @@ public function testSetupSendmailOk()
103103
*/
104104
public function testSequenceOk()
105105
{
106-
$appResult = $this->getAppResultMock(true);
107-
$appResult->expects($this->once())->method('wasSuccessful')->willReturn(true);
108-
$appResult->expects($this->once())->method('noneSkipped')->willReturn(true);
109-
$appResult->expects($this->once())->method('wasSuccessful')->willReturn(true);
106+
$appResult = $this->getAppResultMock();
107+
$appResult->expects($this->once())->method('allOk')->willReturn(true);
110108
$appResult->method('getBackups')->willReturn(array());
111109
$appResult->expects($this->once())->method('getErrors')->willReturn(array());
112110

@@ -132,11 +130,146 @@ public function testSequenceOk()
132130
$mail->phpbuEnd($appResult);
133131
}
134132

133+
/**
134+
* Tests Mail with a successful backup.
135+
*/
136+
public function testBackupOk()
137+
{
138+
$backup = $this->getBackupResultMock();
139+
$backup->method('getName')->willReturn('backup');
140+
$backup->method('allOk')->willReturn(true);
141+
$backup->method('checkCount')->willReturn(0);
142+
$backup->method('checkCountFailed')->willReturn(0);
143+
$backup->method('syncCount')->willReturn(0);
144+
$backup->method('syncCountSkipped')->willReturn(0);
145+
$backup->method('syncCountFailed')->willReturn(0);
146+
$backup->method('cleanupCount')->willReturn(0);
147+
$backup->method('cleanupCountSkipped')->willReturn(0);
148+
$backup->method('cleanupCountFailed')->willReturn(0);
149+
150+
$app = $this->getAppResultMock();
151+
$app->expects($this->exactly(2))->method('allOk')->willReturn(true);
152+
$app->method('getBackups')->willReturn(array($backup));
153+
$app->expects($this->once())->method('getErrors')->willReturn(array());
154+
155+
$mail = new Mail();
156+
$mail->setup(array('recipients' => 'test@example.com', 'transport' => 'null'));
157+
158+
$mail->phpbuStart(array());
159+
$mail->backupStart(array());
160+
$mail->backupEnd(array());
161+
$mail->phpbuEnd($app);
162+
}
163+
164+
/**
165+
* Tests Mail with skipped or failed Syncs or Cleanups.
166+
*/
167+
public function testBackupOkButSkipsOrFails()
168+
{
169+
$backup = $this->getBackupResultMock();
170+
$backup->method('getName')->willReturn('backup');
171+
$backup->method('allOk')->willReturn(false);
172+
$backup->method('okButSkipsOrFails')->willReturn(true);
173+
$backup->method('checkCount')->willReturn(0);
174+
$backup->method('checkCountFailed')->willReturn(0);
175+
$backup->method('syncCount')->willReturn(1);
176+
$backup->method('syncCountSkipped')->willReturn(0);
177+
$backup->method('syncCountFailed')->willReturn(1);
178+
$backup->method('cleanupCount')->willReturn(0);
179+
$backup->method('cleanupCountSkipped')->willReturn(0);
180+
$backup->method('cleanupCountFailed')->willReturn(0);
181+
182+
$app = $this->getAppResultMock();
183+
$app->expects($this->exactly(2))->method('allOk')->willReturn(false);
184+
$app->expects($this->once())->method('backupOkButSkipsOrFails')->willReturn(true);
185+
$app->method('getBackups')->willReturn(array($backup));
186+
$app->expects($this->once())->method('getErrors')->willReturn(array());
187+
188+
$mail = new Mail();
189+
$mail->setup(array('recipients' => 'test@example.com', 'transport' => 'null'));
190+
191+
$mail->phpbuStart(array());
192+
$mail->backupStart(array());
193+
$mail->backupEnd(array());
194+
$mail->phpbuEnd($app);
195+
}
196+
197+
/**
198+
* Tests Mail failed Backup.
199+
*/
200+
public function testBackupFailed()
201+
{
202+
$backup = $this->getBackupResultMock();
203+
$backup->method('getName')->willReturn('backup');
204+
$backup->method('allOk')->willReturn(false);
205+
$backup->method('okButSkipsOrFails')->willReturn(false);
206+
$backup->method('checkCount')->willReturn(0);
207+
$backup->method('checkCountFailed')->willReturn(0);
208+
$backup->method('syncCount')->willReturn(1);
209+
$backup->method('syncCountSkipped')->willReturn(0);
210+
$backup->method('syncCountFailed')->willReturn(1);
211+
$backup->method('cleanupCount')->willReturn(0);
212+
$backup->method('cleanupCountSkipped')->willReturn(0);
213+
$backup->method('cleanupCountFailed')->willReturn(0);
214+
215+
$e = $this->getExceptionMock('test', 0);
216+
217+
$app = $this->getAppResultMock();
218+
$app->expects($this->exactly(2))->method('allOk')->willReturn(false);
219+
$app->expects($this->once())->method('backupOkButSkipsOrFails')->willReturn(false);
220+
$app->method('getBackups')->willReturn(array($backup));
221+
$app->expects($this->once())->method('getErrors')->willReturn(array($e));
222+
223+
$mail = new Mail();
224+
$mail->setup(array('recipients' => 'test@example.com', 'transport' => 'null'));
225+
226+
$mail->phpbuStart(array());
227+
$mail->backupStart(array());
228+
$mail->backupEnd(array());
229+
$mail->phpbuEnd($app);
230+
}
231+
232+
/**
233+
* Return App Result mock.
234+
*
235+
* @return \phpbu\App\Result
236+
*/
135237
public function getAppResultMock()
136238
{
137239
$appResult = $this->getMockBuilder('\\phpbu\\App\\Result')
138240
->disableOriginalConstructor()
139241
->getMock();
140242
return $appResult;
141243
}
244+
245+
/**
246+
* Return Backup Result mock.
247+
*
248+
* @return \phpbu\App\Result\Backup
249+
*/
250+
public function getBackupResultMock()
251+
{
252+
$backup = $this->getMockBuilder('\\phpbu\\App\\Result\\Backup')
253+
->disableOriginalConstructor()
254+
->getMock();
255+
return $backup;
256+
}
257+
258+
/**
259+
* Return Backup Result mock.
260+
*
261+
* @param string $msg
262+
* @param string $code
263+
* @return \phpbu\App\Result\Backup
264+
*/
265+
public function getExceptionMock($msg, $code)
266+
{
267+
$e = $this->getMockBuilder('\\Exception')
268+
->disableOriginalConstructor()
269+
->getMock();
270+
271+
$e->method('getMessage')->willReturn($msg);
272+
$e->method('getCode')->willReturn($code);
273+
return $e;
274+
}
142275
}

tests/phpbu/Log/PrinterTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace phpbu\App\Log;
3+
4+
/**
5+
* Printer Test
6+
*
7+
* @package phpbu
8+
* @subpackage tests
9+
* @author Sebastian Feldmann <sebastian@phpbu.de>
10+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
11+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
12+
* @link http://www.phpbu.de/
13+
* @since Class available since Release 1.2.1
14+
*/
15+
class PrinterTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Tests Printer::getAutoFlush
19+
*/
20+
public function testAutoFlushDefault()
21+
{
22+
$printer = new Printer();
23+
24+
$af = $printer->getAutoFlush();
25+
26+
$this->assertFalse($af);
27+
}
28+
29+
/**
30+
* Tests Printer::setAutoFlush
31+
*/
32+
public function testSetAutoFlush()
33+
{
34+
$printer = new Printer();
35+
$printer->setAutoFlush(true);
36+
37+
$af = $printer->getAutoFlush();
38+
39+
$this->assertTrue($af);
40+
}
41+
42+
/**
43+
* Tests Printer::setAutoFlush
44+
*
45+
* @expectedException \InvalidArgumentException
46+
*/
47+
public function testSetAutoFlushFail()
48+
{
49+
$printer = new Printer();
50+
$printer->setAutoFlush('green');
51+
}
52+
53+
/**
54+
* Tests Printer::write
55+
*/
56+
public function testWriteToStdOut()
57+
{
58+
$printer = new Printer();
59+
ob_start();
60+
$printer->write('foo');
61+
$output = ob_get_clean();
62+
63+
$this->assertEquals('foo', $output);
64+
}
65+
66+
/**
67+
* Tests Printer::write
68+
*/
69+
public function testWriteToStdOutWithAutoFlush()
70+
{
71+
$printer = new Printer();
72+
$printer->setAutoFlush(true);
73+
ob_start();
74+
$printer->write('foo');
75+
$output = ob_get_clean();
76+
77+
$this->assertEquals('foo', $output);
78+
}
79+
}

0 commit comments

Comments
 (0)