1

I have unit tests running for my component (using a bootstrap file to get the usual requirements) but as soon as I add any logging, I get:

Class '\Joomla\CMS\Log\Log' not found`

I have tried mocking out the Log class, but I don't know how to assign my mock to the global JLog namespace. My tests were passing before I added logging to the methods, so I know that the bootstrap is good in principle, it just seems to be the log that is throwing it.

My setup looks like this:

public function setUp():void {
    parent::setUp();
    $this->channel_store = $this->createMock('ChannelStore');
    $this->application = $this->getMockBuilder(JApplication::class)
                              ->setMethods(['get', 'set'])
                              ->getMock();
    JFactory::$application = $this->application;

    $this->log = $this->getMockBuilder(JLog::class)
                      ->setMethods(['add'])
                      ->getMock();

    //  JLog = $this->log; ???
}

What do I need to do in order to mock out JLog correctly?

1 Answer 1

1

In the end I didn't find a way to do this through mocking, but it is very simple to implement a class that does everything I need from the logger:

class JLog {
    const DEBUG = 5;
    const INFO = 4;
    const WARNING = 3;
    const ERROR = 2;

    private static $messages;

    public static function add( $message, $level, $report ) {
        if (!self::$messages) {
            self::$messages = array();
        }
        self::$messages[] = $message;
        echo("\r\n".$message); //optionally print the message for troubleshooting tests.
    }
}

Just including this in my test classes or the bootstrap file immediately got my tests working.

1
  • Please remember to mark this as the accepted answer. Commented May 27, 2020 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.