[GH-ISSUE #1197] Monolog\TestCase class should be accessible for out of tree tests #495

Closed
opened 2026-03-04 02:15:28 +03:00 by kerem · 1 comment
Owner

Originally created by @yann-soubeyrand on GitHub (Sep 25, 2018).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1197

When writing a custom Monolog processor as part of a Symfony application, one cannot test this processor using the Monolog\TestCase base class. Indeed this class is part of the autoload-dev section of the composer.json file and this section is only used if the composer.json file is the root one (which is not the case when Monolog is used as part of a Symfony project).

Here is an example of such a test:

<?php
namespace Tests\Bridge\FrontBundle\Logger;

use Bridge\FrontBundle\Logger\RequestIdProcessor;
use Monolog\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestIdProcessorTest extends TestCase
{
    private const REQUEST_ID = 'test-request-id';

    public function testValidateRequestIdProcessorWhenRequestIdHeaderIsPresent()
    {
        $request = new Request([], [], [], [], [], ["HTTP_X_REQUEST_ID" => self::REQUEST_ID]);
        $requestStack = new RequestStack();
        $requestStack->push($request);
        $requestIdProcessor = new RequestIdProcessor($requestStack);
        $record = $requestIdProcessor($this->getRecord());
        $this->assertArrayHasKey('request_id', $record['extra']);
        $this->assertTrue($record['extra']['request_id'] === self::REQUEST_ID);
    }
    public function testValidateRequestIdProcessorWhenRequestIdHeaderIsMissing()
    {
        $request = new Request();
        $requestStack = new RequestStack();
        $requestStack->push($request);
        $requestIdProcessor = new RequestIdProcessor($requestStack);
        $record = $requestIdProcessor($this->getRecord());
        $this->assertArrayHasKey('request_id', $record['extra']);
        $this->assertTrue($record['extra']['request_id'] === '-');
    }
}
Originally created by @yann-soubeyrand on GitHub (Sep 25, 2018). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1197 When writing a custom Monolog processor as part of a Symfony application, one cannot test this processor using the Monolog\TestCase base class. Indeed this class is part of the autoload-dev section of the composer.json file and this section is only used if the composer.json file is the root one (which is not the case when Monolog is used as part of a Symfony project). Here is an example of such a test: ``` <?php namespace Tests\Bridge\FrontBundle\Logger; use Bridge\FrontBundle\Logger\RequestIdProcessor; use Monolog\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; class RequestIdProcessorTest extends TestCase { private const REQUEST_ID = 'test-request-id'; public function testValidateRequestIdProcessorWhenRequestIdHeaderIsPresent() { $request = new Request([], [], [], [], [], ["HTTP_X_REQUEST_ID" => self::REQUEST_ID]); $requestStack = new RequestStack(); $requestStack->push($request); $requestIdProcessor = new RequestIdProcessor($requestStack); $record = $requestIdProcessor($this->getRecord()); $this->assertArrayHasKey('request_id', $record['extra']); $this->assertTrue($record['extra']['request_id'] === self::REQUEST_ID); } public function testValidateRequestIdProcessorWhenRequestIdHeaderIsMissing() { $request = new Request(); $requestStack = new RequestStack(); $requestStack->push($request); $requestIdProcessor = new RequestIdProcessor($requestStack); $record = $requestIdProcessor($this->getRecord()); $this->assertArrayHasKey('request_id', $record['extra']); $this->assertTrue($record['extra']['request_id'] === '-'); } } ```
kerem 2026-03-04 02:15:28 +03:00
  • closed this issue
  • added the
    Feature
    label
Author
Owner

@Seldaek commented on GitHub (Nov 29, 2018):

Monolog\Test\TestCase is available in src/ as of 2.0 (master branch only right now).

<!-- gh-comment-id:442949848 --> @Seldaek commented on GitHub (Nov 29, 2018): Monolog\Test\TestCase is available in src/ as of 2.0 (master branch only right now).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#495
No description provided.