-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathLogWriterFactoryTest.php
More file actions
76 lines (65 loc) · 2.58 KB
/
LogWriterFactoryTest.php
File metadata and controls
76 lines (65 loc) · 2.58 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
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\API\Behavior\Internal;
use OpenTelemetry\API\Behavior\Internal\LogWriter\ErrorLogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriter\NoopLogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriter\Psr3LogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriter\StreamLogWriter;
use OpenTelemetry\API\Behavior\Internal\LogWriterFactory;
use OpenTelemetry\API\LoggerHolder;
use OpenTelemetry\Tests\TestState;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
#[CoversClass(LogWriterFactory::class)]
class LogWriterFactoryTest extends TestCase
{
use TestState;
#[\Override]
public function setUp(): void
{
LoggerHolder::unset();
}
/**
* @psalm-suppress ArgumentTypeCoercion
*/
#[DataProvider('logDestinationProvider')]
public function test_log_destination_from_env(string $value, string $expected): void
{
$this->setEnvironmentVariable('OTEL_PHP_LOG_DESTINATION', $value);
$this->assertInstanceOf($expected, (new LogWriterFactory())->create());
}
public static function logDestinationProvider(): array
{
return [
['error_log', ErrorLogWriter::class],
['stdout', StreamLogWriter::class],
['stderr', StreamLogWriter::class],
['none', NoopLogWriter::class],
['', ErrorLogWriter::class],
];
}
public function test_psr3_log_destination(): void
{
LoggerHolder::set($this->createMock(LoggerInterface::class));
$this->assertInstanceOf(Psr3LogWriter::class, (new LogWriterFactory())->create());
}
public function test_psr3_from_env_with_logger(): void
{
$this->setEnvironmentVariable('OTEL_PHP_LOG_DESTINATION', 'psr3');
LoggerHolder::set($this->createMock(LoggerInterface::class));
$this->assertInstanceOf(Psr3LogWriter::class, (new LogWriterFactory())->create());
}
public function test_psr3_from_env_without_logger_falls_back_to_error_log(): void
{
$this->setEnvironmentVariable('OTEL_PHP_LOG_DESTINATION', 'psr3');
$this->assertInstanceOf(ErrorLogWriter::class, (new LogWriterFactory())->create());
}
public function test_default_with_logger_uses_psr3(): void
{
LoggerHolder::set($this->createMock(LoggerInterface::class));
$this->setEnvironmentVariable('OTEL_PHP_LOG_DESTINATION', '');
$this->assertInstanceOf(Psr3LogWriter::class, (new LogWriterFactory())->create());
}
}