When using fixtures only 1 unit test works at a time

I’m reading the book “Agile Web Development Using The Yii Framework” and in the book it gives an example of how to use fixtures. I copied the tests exactly, however once I ran them I noticed that 1 test passed and 3 failed. I started playing around and found out that each test works individually, however, when run together, only the first test will pass and the rest always return the error “Invalid argument supplied for foreach().” Has anyone else experienced this and any suggestions how to fix it?




<?php


class ProjectTest extends CDbTestCase {


    public $fixtures = array(

        'projects' => 'Project',

    );


    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);

    }


}


?>



Found the solution. I was using the latest XAMPP which uses PHP 5.3. I switched it to an older version that still supported PHP 5.2 (XAMPP 1.7.1) and now everything is working fine.

I forgot to mention I posted the problem to the bug section and they said PHP 5.3 will work if you have PHPUnit 3.5 or later. They also updated the docs.

You can run "%xampp install folder%\php\pear upgrade --alldeps --force pear", and "%xampp install folder%\php\pear upgrade --alldeps --force phpunit/PHPUnit" in a CMD prompt. This fixed a bunch of phpunit problems in NetBeans 6.9.1.

Actually, I’m also checking out this fixture part and I got all the tests passed once immediately.




# phpunit unit/ProjectTest.php

PHPUnit 3.5.13 by Sebastian Bergmann.


....


Time: 2 seconds, Memory: 7.00Mb


OK (4 tests, 10 assertions) 



However, I have some questions about the results. Since the fixture has three projects, I believe when finishing the all test cases, the tbl_project table should still have 3 projects, because testCreate created another project named "Test Project Creation", testRead, testUpdate, testDelete only delete the project2. But in my test database, I can only see project1, project3 in fixture are left in the table.

Strange thing is: if I comment out testRead, testUpdate, testDelete, only keep testCreate and run phpUnit, then the table will have 4 proejcts. Once I enable testRead case, it’s again with only 3 projects in the fixture.

Anyone could explain why?

Hello,

I do have the exact same problem.

I am using Netbeans 6.9.1 on OSX.

It looks like it only does finish one test if no other tests follow.

The funny part is, that all the other tests do not throw errors, even that they actually should as they depend on some changes of previous tests.

One good example is what is mentioned above: The tests create a fourth project which is not in any later tests deleted.

This test does not show up in the database if all the tests are run. It even does not show up if I run the testRead right after the testCreate. It does only show up if I run the testCreate solely.

I think this is a bug in PHPunit or in Netbeans.

I wil keep you updated if I find a solution.

EDIT: I just checked something an can confirm that it has nothing to do with Netbeans as this behaviour is also shown when running phpUnit from the terminal.

gb5256

I have just installed a vitual machine (fresh start) with Ubuntu.

Installed Yii and pHPunit and got the same result.

So to put it into one sentence: If I run more than one MSQL Queries in a test, non of them will be executed.

If I have only one, then it will be executed as expected.

Interesting is also that there are no errors at all.

Could somebody out there please check this:

Do you have after finishing chapter 6 and running the project_test a project called "Test Project 4" and at the same time an updated "Project 2 updated"?

Any ideas appreciated.

gb5256

Perhaps I know now what the point is:

Can anybody confirm, that the db-fixtures are done upfront every function within a test file?

This would actually explain the situation.

gb5256

Yes. The point of fixtures is to establish a consistent environment within which to run your tests. By default, when using fixtures in Yii, your tables will be reset to their initial conditions before each test (i.e. before each test method in the test class being run). You can customize the behavior of how fixtures interact with the db. See this link in the documentation for more details on this:

Defining and Using Fixtures