|
| 1 | +<?php |
| 2 | +namespace phpbu\App\Log; |
| 3 | + |
| 4 | +use phpbu\App\Configuration\Backup; |
| 5 | + |
| 6 | +/** |
| 7 | + * Json Test |
| 8 | + * |
| 9 | + * @package phpbu |
| 10 | + * @subpackage tests |
| 11 | + * @author MoeBrowne |
| 12 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 13 | + * @since Class available since Release 6.0.0 |
| 14 | + */ |
| 15 | +class PrometheusTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * Tests Prometheus::onBackupEnd |
| 20 | + */ |
| 21 | + public function testOutput() |
| 22 | + { |
| 23 | + // result mock |
| 24 | + $result = $this->getResultMock(); |
| 25 | + |
| 26 | + // config mock |
| 27 | + $backupConf = $this->createMock(Backup::class); |
| 28 | + $backupConf->method('getName')->willReturn('backupName'); |
| 29 | + |
| 30 | + // backup start event mock |
| 31 | + $backupStartEvent = $this->createMock(\phpbu\App\Event\Backup\Start::class); |
| 32 | + $backupStartEvent->method('getConfiguration')->willReturn($backupConf); |
| 33 | + |
| 34 | + // backup end event mock |
| 35 | + $backupEndEvent = $this->createMock(\phpbu\App\Event\Backup\End::class); |
| 36 | + $backupEndEvent->method('getConfiguration')->willReturn($backupConf); |
| 37 | + |
| 38 | + // phpbu end event mock |
| 39 | + $phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class); |
| 40 | + $phpbuEndEvent->method('getResult')->willReturn($result); |
| 41 | + |
| 42 | + $prometheus = new Prometheus(); |
| 43 | + $prometheus->setup(['target' => 'php://output']); |
| 44 | + |
| 45 | + $prometheus->onBackupStart($backupStartEvent); |
| 46 | + $prometheus->onBackupEnd($backupEndEvent); |
| 47 | + |
| 48 | + ob_flush(); |
| 49 | + ob_start(); |
| 50 | + $prometheus->onPhpbuEnd($phpbuEndEvent); |
| 51 | + $output = ob_get_clean(); |
| 52 | + |
| 53 | + $this->assertStringContainsString('phpbu_backup_success{name="foo"} 0', $output); |
| 54 | + $this->assertStringContainsString('phpbu_backup_duration{name="backupName"} ', $output); |
| 55 | + $this->assertStringContainsString('phpbu_backup_last_run{name="backupName"} ', $output); |
| 56 | + $this->assertStringContainsString('phpbu_backup_size{name="backupName"} ', $output); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a app result mock |
| 61 | + * |
| 62 | + * @return \phpbu\App\Result |
| 63 | + */ |
| 64 | + protected function getResultMock() |
| 65 | + { |
| 66 | + $result = $this->createMock(\phpbu\App\Result::class); |
| 67 | + $result->method('started')->willReturn(microtime(true)); |
| 68 | + $result->method('allOk')->willReturn(true); |
| 69 | + $result->method('getErrors')->willReturn([new \Exception('foo bar')]); |
| 70 | + $result->method('getBackups')->willReturn([$this->getBackupResultMock()]); |
| 71 | + $result->method('backupsFailedCount')->willReturn(0); |
| 72 | + $result->method('errorCount')->willReturn(1); |
| 73 | + |
| 74 | + return $result; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a backup result mock |
| 79 | + * |
| 80 | + * @return \phpbu\App\Result\Backup |
| 81 | + */ |
| 82 | + protected function getBackupResultMock() |
| 83 | + { |
| 84 | + $backup = $this->createMock(\phpbu\App\Result\Backup::class); |
| 85 | + $backup->method('getName')->willReturn('foo'); |
| 86 | + $backup->method('wasSuccessful')->willReturn(true); |
| 87 | + $backup->method('checkCount')->willReturn(0); |
| 88 | + $backup->method('checkCountFailed')->willReturn(0); |
| 89 | + $backup->method('syncCount')->willReturn(0); |
| 90 | + $backup->method('syncCountSkipped')->willReturn(0); |
| 91 | + $backup->method('syncCountFailed')->willReturn(0); |
| 92 | + $backup->method('cleanupCount')->willReturn(0); |
| 93 | + $backup->method('cleanupCountSkipped')->willReturn(0); |
| 94 | + $backup->method('cleanupCountFailed')->willReturn(0); |
| 95 | + |
| 96 | + return $backup; |
| 97 | + } |
| 98 | +} |
0 commit comments