lundi 6 janvier 2020

Given object is not an instance of the class this method was declared in error when trying to set member variable inside PHPUnit test

I get the following error when trying to set the "logger" attribute on my class.

ReflectionException: Given object is not an instance of the class this method was declared in

public function testCheckForRecentActivity()
{
    self::bootKernel();

    $plan = new \ReflectionClass('AppBundle\Services\Courses\Revision0');

    $logger = $plan->getMethod('setLogger');
    $logger->setAccessible(true);
    $logger->invoke($plan, static::$kernel->getContainer()->get('logger.golf'));

    // ....
}

The class hierarchy looks like this:

class Revision0 extends Base

abstract class Base extends Plans\PlanBase

abstract class PlanBase which uses the LoggerTrait (use LoggerTrait;)

LoggerTrait looks like:

/**
 * @var LoggerInterface
 */
protected $logger;

/**
 * @required
 * @param LoggerInterface $logger
 */
public function setLogger(LoggerInterface $logger)
{
    $this->logger = $logger;
}

Is there something I am missing that is stopping me from setting the logger inside my test?





Aucun commentaire:

Enregistrer un commentaire