I am trying to test a plugin model in a CakePHP app. The model uses a table called ‘cron_jobs’ for which I have set up this fixture:
class CronJobFixture extends CakeTestFixture
{
public $import = array('table' => 'cron_jobs');
}
My test class works well and looks like this:
<?php
App::uses('CronJob', 'Cron.Model');
class CronJobTest extends CakeTestCase
{
public $fixtures = array('plugin.cron.CronJob');
public function setUp()
{
parent::setUp();
$this->CronJob = new CronJob();
}
public function testCollectFailsOnMissingComponent()
{
$this->setExpectedException('InvalidArgumentException');
$this->CronJob->collect(null);
}
public function testCollectSilentOnMissingComponent()
{
$result = $this->CronJob->collect('SomeNonExistingComponent');
$this->assertEquals(null, $result);
}
// Some more tests that will need the fixture ....
}
If I then change the test setup by replacing
$this->CronJob = new CronJob();
with
$this->CronJob = ClassRegistry::init("CronJob");
to load the fixture, I get this error:
CronJobTest::testCollectSilentOnMissingComponent PDOException:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ‘collect’ at
line 1
The error can’t be generated by anything in the CronJob class, because the code being covered by the two tests doesn’t access the database. I am sure my test database is configured correctly, because I get a database connection error if I make changes to the test database configuration.
I am using CakePHP 2.2.1, PHPUnit 3.6.12, PHP 5.4
Fixtures prefer the lowercase underscored convention. Do this:
Since it’s a plugin; be sure to use the plugin notation with
ClassRegistry::initlike this:That error happens because CakePHP is lazy loading a non-plugin
CronJobmodel without yourcollect()method.