|
| 1 | +<?php namespace util\log\unittest; |
| 2 | + |
| 3 | +use io\streams\MemoryOutputStream; |
| 4 | +use io\streams\StringWriter; |
| 5 | +use unittest\TestCase; |
| 6 | +use util\cmd\Console; |
| 7 | +use util\log\ColoredConsoleAppender; |
| 8 | +use util\log\Layout; |
| 9 | +use util\log\LogCategory; |
| 10 | +use util\log\LogLevel; |
| 11 | +use util\log\LoggingEvent; |
| 12 | + |
| 13 | +class ColoredConsoleAppenderTest extends TestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates a ColoredConsoleAppender with a given target |
| 17 | + * |
| 18 | + * @param string|io.streams.OutputStream $target |
| 19 | + * @return util.log.LogCategoy |
| 20 | + */ |
| 21 | + private function category($target) { |
| 22 | + return (new LogCategory('default'))->withAppender( |
| 23 | + (new ColoredConsoleAppender(new StringWriter($target)))->withLayout(newinstance(Layout::class, [], [ |
| 24 | + 'format' => function(LoggingEvent $event) { |
| 25 | + return '[LOG] '.implode(' ', $event->getArguments()); |
| 26 | + } |
| 27 | + ])) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + #[@test] |
| 32 | + public function can_create() { |
| 33 | + new ColoredConsoleAppender(); |
| 34 | + } |
| 35 | + |
| 36 | + #[@test] |
| 37 | + public function writes_to_stdout_by_default() { |
| 38 | + $this->assertEquals(Console::$out, (new ColoredConsoleAppender())->writer()); |
| 39 | + } |
| 40 | + |
| 41 | + #[@test] |
| 42 | + public function can_overwrite_colors() { |
| 43 | + $this->assertEquals('00;30', (new ColoredConsoleAppender('out', [LogLevel::INFO => '00;30']))->colors()[LogLevel::INFO]); |
| 44 | + } |
| 45 | + |
| 46 | + #[@test, @values(map = [ |
| 47 | + # 'out' => Console::$out, |
| 48 | + # 'err' => Console::$err, |
| 49 | + #])] |
| 50 | + public function writes_to($param, $writer) { |
| 51 | + $this->assertEquals($writer, (new ColoredConsoleAppender($param))->writer()); |
| 52 | + } |
| 53 | + |
| 54 | + #[@test] |
| 55 | + public function info() { |
| 56 | + $out= new MemoryOutputStream(); |
| 57 | + $this->category($out)->info('Test'); |
| 58 | + |
| 59 | + $this->assertEquals("\033[00;00m[LOG] Test\033[0m", $out->getBytes()); |
| 60 | + } |
| 61 | + |
| 62 | + #[@test] |
| 63 | + public function warn() { |
| 64 | + $out= new MemoryOutputStream(); |
| 65 | + $this->category($out)->warn('Test'); |
| 66 | + |
| 67 | + $this->assertEquals("\033[00;31m[LOG] Test\033[0m", $out->getBytes()); |
| 68 | + } |
| 69 | + |
| 70 | + #[@test] |
| 71 | + public function error() { |
| 72 | + $out= new MemoryOutputStream(); |
| 73 | + $this->category($out)->error('Test'); |
| 74 | + |
| 75 | + $this->assertEquals("\033[01;31m[LOG] Test\033[0m", $out->getBytes()); |
| 76 | + } |
| 77 | + |
| 78 | + #[@test] |
| 79 | + public function debug() { |
| 80 | + $out= new MemoryOutputStream(); |
| 81 | + $this->category($out)->debug('Test'); |
| 82 | + |
| 83 | + $this->assertEquals("\033[00;34m[LOG] Test\033[0m", $out->getBytes()); |
| 84 | + } |
| 85 | +} |
0 commit comments