/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Foundation/Cloud/JsonFormatterTest.php
99 строк
3 KB
Lucas Michot
Remove useless parent::setUp()/tearDown() calls in tests extending PHPUnit's TestCase directly (#60994)
03 авг 2026, 20:54
Не верифицирован
03 авг 2026, 20:54
14a6983
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Foundation\Cloud; use Illuminate\Container\Container; use Illuminate\Foundation\Cloud\JsonFormatter; use Illuminate\Http\Request; use Monolog\Level; use Monolog\LogRecord; use PHPUnit\Framework\TestCase; class JsonFormatterTest extends TestCase { protected function setUp(): void { Container::setInstance(new Container); } protected function tearDown(): void { Container::setInstance(null); } protected function createRecord(): LogRecord { return new LogRecord( datetime: new \DateTimeImmutable, channel: 'test', level: Level::Info, message: 'Test message', context: [], extra: [], ); } public function test_adds_cloud_request_id_as_top_level_key() { $app = Container::getInstance(); $request = Request::create('/'); $request->headers->set('Cloud-Request-ID', '550e8400-e29b-41d4-a716-446655440000'); $app->instance('request', $request); $formatter = new JsonFormatter(); $formatted = $formatter->format($this->createRecord()); $decoded = json_decode($formatted, true); $this->assertSame('550e8400-e29b-41d4-a716-446655440000', $decoded['cloud_request_id']); } public function test_does_not_add_field_when_no_request_bound() { $formatter = new JsonFormatter; $formatted = $formatter->format($this->createRecord()); $decoded = json_decode($formatted, true); $this->assertArrayNotHasKey('cloud_request_id', $decoded); } public function test_does_not_add_field_when_no_header_present() { $app = Container::getInstance(); $request = Request::create('/'); $app->instance('request', $request); $formatter = new JsonFormatter; $formatted = $formatter->format($this->createRecord()); $decoded = json_decode($formatted, true); $this->assertArrayNotHasKey('cloud_request_id', $decoded); } public function test_preserves_existing_log_fields() { $app = Container::getInstance(); $request = Request::create('/'); $request->headers->set('Cloud-Request-ID', '6ba7b810-9dad-11d1-80b4-00c04fd430c8'); $app->instance('request', $request); $record = new LogRecord( datetime: new \DateTimeImmutable('2024-01-15 10:30:00'), channel: 'my-channel', level: Level::Warning, message: 'Test message', context: ['context_field' => 'context_value'], extra: ['extra_field' => 'extra_value'], ); $formatter = new JsonFormatter; $formatted = $formatter->format($record); $decoded = json_decode($formatted, true); $this->assertSame('Test message', $decoded['message']); $this->assertSame('WARNING', $decoded['level_name']); $this->assertSame('my-channel', $decoded['channel']); $this->assertSame('extra_value', $decoded['extra']['extra_field']); $this->assertSame('context_value', $decoded['context']['context_field']); $this->assertSame('6ba7b810-9dad-11d1-80b4-00c04fd430c8', $decoded['cloud_request_id']); } }