PluginProbe
WP Discourse / trunk
WP Discourse vtrunk
2.6.4 2.6.3 1.2.1 1.2.2 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.85 1.4.0 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 All 150 releases
wp-discourse / tests / phpunit / test-logger.php

test-logger.php in WP Discourse trunk, at tests/phpunit/test-logger.php

92 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LoggerTest
4 *
5 * @package WPDiscourse
6 */
7
8 namespace WPDiscourse\Test;
9
10 use WPDiscourse\Logs\Logger;
11 use WPDiscourse\Logs\NullHandler;
12 use WPDiscourse\Logs\FileHandler;
13 use WPDiscourse\Logs\LineFormatter;
14 use WPDiscourse\Test\UnitTest;
15
16 /**
17 * Logger test case.
18 */
19 class LoggerTest extends UnitTest {
20
21 /**
22 * Teardown each test.
23 */
24 public function tearDown(): void {
25 parent::tearDown();
26
27 self::$plugin_options['logs-enabled'] = 1;
28 }
29
30 /**
31 * It creates an instance of Logger
32 */
33 public function test_create() {
34 $logger = Logger::create( 'test', self::$plugin_options );
35 $this->assertInstanceOf( Logger::class, $logger );
36
37 return $logger;
38 }
39
40 /**
41 * It attaches FileHandler as the default handler
42 *
43 * @param object $logger Instance of \WPDiscourse\Logs\Logger.
44 * @depends test_create
45 */
46 public function test_create_handler( $logger ) {
47 $handlers = $logger->getHandlers();
48 $this->assertCount( 1, $handlers );
49
50 $file_handler = reset( $handlers );
51 $this->assertInstanceOf( FileHandler::class, $file_handler );
52
53 return $file_handler;
54 }
55
56 /**
57 * It attaches LineFormatter as the default formatter
58 *
59 * @param object $file_handler Instance of \WPDiscourse\Logs\FileHandler.
60 * @depends test_create_handler
61 */
62 public function test_create_handler_formatter( $file_handler ) {
63 $this->assertInstanceOf( LineFormatter::class, $file_handler->getFormatter() );
64 }
65
66 /**
67 * It attaches NullHandler if FileHandler is not enabled
68 */
69 public function test_create_file_handler_not_enabled() {
70 $file_handler_double = \Mockery::mock( FileHandler::class )->makePartial();
71 $file_handler_double->shouldReceive( 'enabled' )->andReturn( false );
72
73 $logger = Logger::create( 'test', self::$plugin_options, $file_handler_double );
74 $handlers = $logger->getHandlers();
75
76 $this->assertCount( 1, $handlers );
77 $this->assertContainsOnlyInstancesOf( NullHandler::class, $handlers );
78 }
79
80 /**
81 * It attaches NullHandler if logs are not enabled
82 */
83 public function test_create_logs_not_enabled() {
84 self::$plugin_options['logs-enabled'] = 0;
85 $logger = Logger::create( 'test', self::$plugin_options );
86 $handlers = $logger->getHandlers();
87
88 $this->assertCount( 1, $handlers );
89 $this->assertContainsOnlyInstancesOf( NullHandler::class, $handlers );
90 }
91 }
92