Search code examples
laravelphpunitlaravel-routingoctobercmsoctobercms-plugins

October CMS PHPUnit Route Testing


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...
}

Solution

  • 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.