1

I'm trying to write some tests for an October CMS plugin's custom routes using PHPUnit, but running into some errors getting the tests to run correctly.

Each test passes when run individually, but when run as a group, the first test will pass and the rest fail with 500 errors. The error message for the failing tests is:

in Resolver.php line 44
at HandleExceptions->handleError('8', 'Undefined index: myThemeName',
'/Users/me/src/myProject/vendor/october/rain/src/Halcyon/Datasource/
Resolver.php', '44', array('name' => 'myThemeName')) in Resolver.php line 
44

The test case looks like this:

class RoutesTest extends PluginTestCase
{
  protected $baseUrl = "/";

  public function setUp() {
    parent::setUp();
    DB::beginTransaction();
 }

  public function tearDown()
  {
    DB::rollBack();
    parent::tearDown();
  }

  public function testRootPath()
  {
    $response = $this->call('GET', '/');
    $this->assertEquals(200, $response->status());
  }

  public function testIntroPath()
  {
    $response = $this->call('GET', '/intro');
    $this->assertEquals(200, $response->status());
  }

  etc...
}
mindlis
  • 1,546
  • 11
  • 17
  • Could you share the `use` statements you have, please? I can't seem to get `use Illuminate\Foundation\Testing\TestCase;` to work, and I've seen `use \PHPUnit_Framework_TestCase;` somewhere else - which is the preferred/valid `use` style? – icedwater Nov 03 '17 at 04:41

3 Answers3

0

I don't know why, but works if you add the flag --process-isolation on your phpunit call, i think maybe is a cache problem

OsDev
  • 1,243
  • 9
  • 20
  • This does get the tests to pass, but seems like more of a workaround than actually addressing the issue. – mindlis Aug 17 '17 at 22:27
  • :) good luck with that, i have been for a year trying to discover what is happening, but... – OsDev Aug 17 '17 at 22:59
0

We ended up using curl to make an actual request and get the response code from that.

protected function getResponseCode($url) {
    $ch = curl_init($this->baseUrl.$url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_exec($ch);
    return curl_getinfo($ch, CURLINFO_HTTP_CODE);
}

public function testRootPath()
{
    $this->assertEquals(200, $this->getResponseCode('/'));
}

Now all tests pass without using isolated processes.

mindlis
  • 1,546
  • 11
  • 17
0

Unfortunately no solution, just a different angle. I think it has to do with October's "testing" configuration (config/testing/cms.php). That will look for files in the directory "tests/fixtures/themes/pages/". The index.htm file it finds there, has the url-parameter set as '/'. That's why phpunit will find it, but not the other urls. I have seen serveral suggested solutions to that problem, but none of them worked for me:

  1. The use of "Config::set('cms.activeTheme', 'myTheme');" before running the test.
  2. Changing the "pluginsPathLocal" from "base_path('tests/fixtures/plugins')" to "base_path('plugins')" in config/testing/cms.php
  3. Changing the "themesPathLocal" from "base_path('tests/fixtures/themes')" to "base_path('themes')" in config/testing/cms.php

Life would be so much easier if I could figure out how to visit one of my theme's pages the correct way........