Help needed writing tests for my plugin

My Plugin is essentially a PHPunit tests container, it should catch metrics from a remote Piwik installation, then from local installation.

My problem is submitting a local request to Piwik\API, I’m simply getting Exception: General_ExceptionInvalidRendererFormat

This is the code:

private $queryURL; private $local; private $remoteHost = 'http://demo.piwik.org/index.php?'; private $queryParams = array( 'module=API', 'method=API.get', 'period=day', 'date=yesterday', 'format=JSON', );

`public function setUp()
{
// set up here if needed
$this->queryURL = implode(’&’, $this->queryParams);

$url = new Url();
$this->local = $url->getCurrentUrl();

}`

public function testCompare() { $http = new Http(); $json = $http->fetchRemoteFile($this->remoteHost . $this->queryURL . '&idSite=7'); $remote_data = json_decode($json, true); $request = new Request($this->queryURL . '&idSite=1'); $response = $request->process(); $local_data = $response->getSerialized(); $this->assertEquals($remote_data, $local_data); }

Pretty unreadable in this forum lol, I hope someone could decypher it :sweat_smile:

Calling this URL in the browser works: http://demo.piwik.org/index.php?module=API&method=API.get&period=day&date=yesterday&format=JSON&idSite=7 so fetching the remote file should work and I presume it is a problem with new Request(...). There it seems to not find any renderer as the plugins are not loaded. For some reason you probably need to load the API plugin manually:

    public function setUp()
    {
        \Piwik\Plugin\Manager::getInstance()->loadPlugins(array('API'));
    }

Do you know if this is an IntegrationTestCase? Then it should do this automatically. It looks like this is a UnitTestCase maybe. For unit tests we do not bootstrap any Piwik plugins etc

1 Like

Ciao Thomas,
your hint is correct! :white_check_mark:

Infact I’ve started using console commands:

  • php console generate:plugin
  • php console generate:test

And I got a barebone extension of UnitTestCase class.
Then I’ve refactored my test class loading the plugins on setUp() as you suggest and now It’s working.
I’ve tried also to get started using a IntegrationTestCase but It’s slower because it loads all the Piwik’s environment.

Thank your for inspiration :bulb: