PHPUnit problem with fixutre data

Hi everyone,

I am currently working through the Yii book and every now and then I have problems with phpunit when testing.

I don’t understand at all whats wrong, maybe one of you guys can give me directions what I am doing wrong

The errormessage from phpunit:


ProjectTest::testDelete

Failed asserting that Project Object

(

    [_md:CActiveRecord:private] => CActiveRecordMetaData Object

        (

            [tableSchema] => CMysqlTableSchema Object

                (

                    [schemaName] => 

                    [name] => tbl_project

                    [rawName] => `tbl_project`

                    [primaryKey] => id

                    [sequenceName] => 

                    [foreignKeys] => Array

                        (

                        )


                    [columns] => Array

                        (

                            [id] => CMysqlColumnSchema Object

                                (

                                    [name] => id

                                    [rawName] => `id`

                                    [allowNull] => 

                                    [dbType] => int(11)

                                    [type] => integer

                                    [defaultValue] => 

                                    [size] => 11

                                    [precision] => 11

                                    [scale] => 

                                    [isPrimaryKey] => 1

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [name] => CMysqlColumnSchema Object

                                (

                                    [name] => name

                                    [rawName] => `name`

                                    [allowNull] => 1

                                    [dbType] => varchar(128)

                                    [type] => string

                                    [defaultValue] => 

                                    [size] => 128

                                    [precision] => 128

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [description] => CMysqlColumnSchema Object

                                (

                                    [name] => description

                                    [rawName] => `description`

                                    [allowNull] => 1

                                    [dbType] => text

                                    [type] => string

                                    [defaultValue] => 

                                    [size] => 

                                    [precision] => 

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [create_time] => CMysqlColumnSchema Object

                                (

                                    [name] => create_time

                                    [rawName] => `create_time`

                                    [allowNull] => 1

                                    [dbType] => datetime

                                    [type] => string

                                    [defaultValue] => 

                                    [size] => 

                                    [precision] => 

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [create_user_id] => CMysqlColumnSchema Object

                                (

                                    [name] => create_user_id

                                    [rawName] => `create_user_id`

                                    [allowNull] => 1

                                    [dbType] => int(11)

                                    [type] => integer

                                    [defaultValue] => 

                                    [size] => 11

                                    [precision] => 11

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [update_time] => CMysqlColumnSchema Object

                                (

                                    [name] => update_time

                                    [rawName] => `update_time`

                                    [allowNull] => 1

                                    [dbType] => datetime

                                    [type] => string

                                    [defaultValue] => 

                                    [size] => 

                                    [precision] => 

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                            [update_user_id] => CMysqlColumnSchema Object

                                (

                                    [name] => update_user_id

                                    [rawName] => `update_user_id`

                                    [allowNull] => 1

                                    [dbType] => int(11)

                                    [type] => integer

                                    [defaultValue] => 

                                    [size] => 11

                                    [precision] => 11

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:CComponent:private] => 

                                    [_m:CComponent:private] => 

                                )


                        )


                    [_e:CComponent:private] => 

                    [_m:CComponent:private] => 

                )


            [columns] => Array

                (

                    [id] => CMysqlColumnSchema Object

                        (

                            [name] => id

                            [rawName] => `id`

                            [allowNull] => 

                            [dbType] => int(11)

                            [type] => integer

                            [defaultValue] => 

                            [size] => 11

                            [precision] => 11

                            [scale] => 

                            [isPrimaryKey] => 1

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [name] => CMysqlColumnSchema Object

                        (

                            [name] => name

                            [rawName] => `name`

                            [allowNull] => 1

                            [dbType] => varchar(128)

                            [type] => string

                            [defaultValue] => 

                            [size] => 128

                            [precision] => 128

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [description] => CMysqlColumnSchema Object

                        (

                            [name] => description

                            [rawName] => `description`

                            [allowNull] => 1

                            [dbType] => text

                            [type] => string

                            [defaultValue] => 

                            [size] => 

                            [precision] => 

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [create_time] => CMysqlColumnSchema Object

                        (

                            [name] => create_time

                            [rawName] => `create_time`

                            [allowNull] => 1

                            [dbType] => datetime

                            [type] => string

                            [defaultValue] => 

                            [size] => 

                            [precision] => 

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [create_user_id] => CMysqlColumnSchema Object

                        (

                            [name] => create_user_id

                            [rawName] => `create_user_id`

                            [allowNull] => 1

                            [dbType] => int(11)

                            [type] => integer

                            [defaultValue] => 

                            [size] => 11

                            [precision] => 11

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [update_time] => CMysqlColumnSchema Object

                        (

                            [name] => update_time

                            [rawName] => `update_time`

                            [allowNull] => 1

                            [dbType] => datetime

                            [type] => string

                            [defaultValue] => 

                            [size] => 

                            [precision] => 

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [update_user_id] => CMysqlColumnSchema Object

                        (

                            [name] => update_user_id

                            [rawName] => `update_user_id`

                            [allowNull] => 1

                            [dbType] => int(11)

                            [type] => integer

                            [defaultValue] => 

                            [size] => 11

                            [precision] => 11

                            [scale] => 

                            [isPrimaryKey] => 

                            [isForeignKey] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                )


            [relations] => Array

                (

                    [issues] => CHasManyRelation Object

                        (

                            [limit] => -1

                            [offset] => -1

                            [together] => 

                            [index] => 

                            [joinType] => LEFT OUTER JOIN

                            [on] => 

                            [alias] => 

                            [with] => Array

                                (

                                )


                            [name] => issues

                            [className] => Issue

                            [foreignKey] => project_id

                            [select] => *

                            [condition] => 

                            [params] => Array

                                (

                                )


                            [group] => 

                            [join] => 

                            [having] => 

                            [order] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                    [users] => CManyManyRelation Object

                        (

                            [limit] => -1

                            [offset] => -1

                            [together] => 

                            [index] => 

                            [joinType] => LEFT OUTER JOIN

                            [on] => 

                            [alias] => 

                            [with] => Array

                                (

                                )


                            [name] => users

                            [className] => User

                            [foreignKey] => tbl_project_user_assignment(project_id, user_id)

                            [select] => *

                            [condition] => 

                            [params] => Array

                                (

                                )


                            [group] => 

                            [join] => 

                            [having] => 

                            [order] => 

                            [_e:CComponent:private] => 

                            [_m:CComponent:private] => 

                        )


                )


            [attributeDefaults] => Array

                (

                )


            [_model:CActiveRecordMetaData:private] => Project Object

                (

                    [_md:CActiveRecord:private] => CActiveRecordMetaData Object

 *RECURSION*

                    [_new:CActiveRecord:private] => 

                    [_attributes:CActiveRecord:private] => Array

                        (

                        )


                    [_related:CActiveRecord:private] => Array

                        (

                        )


                    [_c:CActiveRecord:private] => 

                    [_pk:CActiveRecord:private] => 

                    [_alias:CActiveRecord:private] => t

                    [_errors:CModel:private] => Array

                        (

                        )


                    [_validators:CModel:private] => 

                    [_scenario:CModel:private] => 

                    [_e:CComponent:private] => 

                    [_m:CComponent:private] => 

                )


        )


    [_new:CActiveRecord:private] => 

    [_attributes:CActiveRecord:private] => Array

        (

            [id] => 2

            [name] => Test Project 2

            [description] => Description of Project 2

            [create_time] => 0000-00-00 00:00:00

            [create_user_id] => 

            [update_time] => 0000-00-00 00:00:00

            [update_user_id] => 

        )


    [_related:CActiveRecord:private] => Array

        (

        )


    [_c:CActiveRecord:private] => 

    [_pk:CActiveRecord:private] => 2

    [_alias:CActiveRecord:private] => t

    [_errors:CModel:private] => Array

        (

        )


    [_validators:CModel:private] => 

    [_scenario:CModel:private] => update

    [_e:CComponent:private] => 

    [_m:CComponent:private] => 

)

 matches expected <null>.

My testDelete function


public function testDelete()

{

	$project = $this->projects('project2');

	$savedProjectId = $project->id;

	$this->assertTrue(Project::model()->delete());

	

	$deletedProject = Project::model()->findByPk($savedProjectId);

	$this->assertEquals(NULL, $deletedProject);

} // testDelete



and finally my fixture I created:


<?php

	return array 

	(

		'project1'=>array

		(

			'name'=>'Test Project 1',

			'description'=>'Description of Project 1',

			'create_time'=>'',

			'create_user_id'=>'',

			'update_time'=>'',

			'update_user_id'=>'',

		), // array

		

		'project2'=>array

		(

			'name'=>'Test Project 2',

			'description'=>'Description of Project 2',

			'create_time'=>'',

			'create_user_id'=>'',

			'update_time'=>'',

			'update_user_id'=>'',

		), // array

		

		'project3'=>array

		(

			'name'=>'Test Project 3',

			'description'=>'Description of Project 3',

			'create_time'=>'',

			'create_user_id'=>'',

			'update_time'=>'',

			'update_user_id'=>'',

		), // array

	); // array

	

?>



any help would be appreciated.

thanks in advance,

andre

it looks like in your test method, you need to be deleting the actual project.

You have:


$this->assertTrue(Project::model()->delete());



But what you NEED is:


$this->assertTrue($project->delete()); 

Thank you, jefftulsa.

That solved my issue :)

Greetings,

andre.

My fixture test update is not saving to the test database but delete is working . What could be wrong with my code?

Here is my fixture:


<?php

...

    'member2' => array(

        'member_no' => '002',

        'surname' => 'Surname2',

        'othernames' => 'Othernames2',

        'gender' => 'F',

...




    public function testUpdate() {

        // UPDATE a member details

        $member = $this->members('member2');

        $updatedSurname = 'Updated Member Surname';

        $member->surname = $updatedSurname;

        $this->assertTrue($member->save(false));

        // read back the record again to ensure the update worked

        $updatedMember = Member::model()->findByPk($member->id);

        $this->assertTrue($updatedMember instanceof Member);

        $this->assertEquals($updatedSurname, $updatedMember->surname);

    }



I would appreciate your help.

Hey Samuel

I’d first check your updated model passes validation. When you make save(false) - false means not to validate the model.

So you may do


$member = $this->members('member2');

$updatedSurname = 'Updated Member Surname';

$member->surname = $updatedSurname;

var_dump($member->validate());



validate() docs

P.S. Please use code formatting button next time ("Insert code snippet" one)

Thanx yugene for the advice.

I don’t understand. The test results


var_dump($member->validate()); 

returns a boolean(true).