Problem with Yii Fixtures?

I seem to be having a fixture problem in Yii. The problem is this:

public $fixtures=array('projects'=>'Project'); 

      

The Project model exists and I have fixtures in the file name tbl_project.php in the test fixtures folder and my table name is tbl_project. Inside the fixtures file the following is stated.

return array(
        'project1' => array(
            'name' => 'Test Project 1',
            'description' => 'This is test project 1',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
        'project2' => array(
            'name' => 'Test Project 2',
            'description' => 'This is test project 2',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
        'project3' => array(
            'name' => 'Test Project 3',
            'description' => 'This is test project 3',
            'create_time' => '',
            'create_user_id' => '',
            'update_time' => '',
            'update_user_id' => '',
         ),
    );       

      

This is actually from the book "Developing an Agile Web Application with Yii". When I run the test case, I get the following with no information about the test results.

PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from ETC/protected/tests/phpunit.xml

      

If I remove the fixture array from the top, I get the following.

Time: 0 seconds, Memory: 9.25Mb

There was 1 error:

1) ProjectTest::testRead
Exception: Unknown method 'projects' for class 'ProjectTest'. 

      

This obviously makes sense. I don't know what I am doing wrong.

+3


source to share


5 answers


  • Make sure your testing class looks like this

    class ProjectTest extends CDbTestCase{

    protected $fixtures = array(
                'projects' => 'Project',
            );

    public function testRead(){

        $receivedProject = $this->projects('Project1');
        $this->assertTrue($receivedProject instanceof Project);
        $this->assertEquals($receivedProject->name,'test 1');
    }
    ...`

      

  • Check the device configuration in the file protected / config / test.php ... It should look like ...




    ...
    'components'=>array(
            'fixture'=>array(
                'class'=>'system.test.CDbFixtureManager',
            ),
                        'db'=>array(
                            'connectionString' =>'mysql:host=localhost;dbname=your_db_name',
                            'emulatePrepare' => true,
                            'username' => 'username',
                            'password' => 'passwd',
                            'charset' => 'utf8',
                        ),
    ....

      

  • Ultimately, check the permissions on the fixture file, making sure it is readable.
+4


source


Also make sure you call the parent setUp () method in your own setUp ()



class SomeTest extends CDbTestCase {

    public $fixtures = array(
        'somes' => 'Some',
    );

    protected function setUp() {
        parent::setUp();
        // your code....
    }

    // your tests .......................

}

      

+3


source


Is your test class retrieved from CDbTestCase instead of CTestCase?

your test class should look something like this:

class ProjectTest extends CDbTestCase{

protected $fixtures = array(
            'projects' => 'Project',
        );

public function testRead(){

    $receivedProject = $this->projects('Project1');
    $this->assertTrue($receivedProject instanceof Project);
    $this->assertEquals($receivedProject->name,'test 1');
}

      

+1


source


class ProjectTest extends CDbTestCase
{

public function testCreate()
{
    //CREATE a new Project
    $newProject=new Project;
    $newProjectName = 'Test Project Creation';
    $newProject->setAttributes(array(
        'name' => $newProjectName,
        'description' => 'This is a test for new project creation',
        'createTime' => '2009-09-09 00:00:00',
        'createUser' => '1',
        'updateTime' => '2009-09-09 00:00:00',
        'updateUser' => '1',
    )
    );
    $this->assertTrue($newProject->save(false));
    //READ back the newly created Project to ensure the creation    worked
    $retrievedProject=Project::model()->findByPk($newProject->id);
    $this->assertTrue($retrievedProject instanceof Project);
    $this->assertEquals($newProjectName,$retrievedProject->name);
}
public function testRead()
{
    $retrievedProject = $this->projects('project1');
    $this->assertTrue($retrievedProject instanceof Project);
    $this->assertEquals('Test Project 1',$retrievedProject->name);
}
public function testUpdate()
{
    $project = $this->projects('project2');
    $updatedProjectName = 'Updated Test Project 2';
    $project->name = $updatedProjectName;
    $this->assertTrue($project->save(false));
    //read back the record again to ensure the update worked
    $updatedProject=Project::model()->findByPk($project->id);
    $this->assertTrue($updatedProject instanceof Project);
    $this->assertEquals($updatedProjectName,$updatedProject->name);
}
public function testDelete()
{
    $project = $this->projects('project2');
    $savedProjectId = $project->id;
    $this->assertTrue($project->delete());
    $deletedProject=Project::model()->findByPk($savedProjectId);
    $this->assertEquals(NULL,$deletedProject);
}

public function testGetUserOptions()
{
    $project = $this->projects('project1');
    $options = $project->userOptions;
    $this->assertTrue(is_array($options));
    $this->assertTrue(count($options) > 0);

}
public $fixtures=array(
'projects'=>'Project',
'users'=>'User',
'projUsrAssign'=>':tbl_project_user_assignment',
);

}

      

0


source


make sure installation is done with parent

<i>public function setUp() {
    parent::setUp();
}</i>

      

0


source







All Articles