Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monolog\TestCase class should be accessible for out of tree tests #1197

Closed
yann-soubeyrand opened this issue Sep 25, 2018 · 1 comment
Closed
Labels
Milestone

Comments

@yann-soubeyrand
Copy link

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'] === '-');
    }
}
@Seldaek Seldaek added this to the 2.0 milestone Nov 4, 2018
@Seldaek Seldaek added the Feature label Nov 4, 2018
@Seldaek
Copy link
Owner

Seldaek commented Nov 29, 2018

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

@Seldaek Seldaek closed this as completed Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants