0
votes

So I have a simple unit test file like this:

class testService extends UnitTestCase {
    function setUp() {
        parent::setUp();
    }
    function testCall() {
        $service = \Drupal::service('plugin.manager.rest');
    }
    ...
}

vendor/bin/phpunit -c core/ modules/custom/module-name/

has the error of ReflectionException: Class does not exist

in Devel, I can execute PHP with this line:

$service = \Drupal::service('plugin.manager.rest');

What do I miss?

Thanks

1

1 Answers

-1
votes
  • \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.

  • You should declare $container and set for "plugin.manager.rest"

$resourceManager = $this->getMockBuilder(ResourcePluginManager::class)->disableOriginalConstructor()->getMock();

$this->container->set('plugin.manager.rest', $resourceManager);