Agile Web App Dev with Yii 1.1 , common problem with fixtures

Hi guys , This is my first post on YII Forum ,

I am trying really hard to learn YII and perfect it and i am struggling to overcome a problem presented in the book i mentioned on the topic.

I have PHP 5.3.5 running on Xampp with manually configured PHPUnit 3.5 and i am certain there is nothing wrong with the rest of the code other than this line


$retrievedProject = $this->projects('project1');

which gives me an error PHP Fatal error: Call to undefined method ProjectTest::projects() in /opt/lampp/htdocs/trackstar/protected/tests/unit/ProjectTest.php

Guys What has gone wrong here while all the rest of the code is perfect , is this related to a bug in PHPUnit 3.5 ? if so How do i get out of this or bypass it ?

Please help me to finish off my learning process of Yii.

Thanks

I think the problem is how you defined fixtures, can you copy code from ProjectTest.php where you defined fixtures?

of course

Thiis the data file at trackstar/protected/tests/fixtures/tbl_project.php




<?php

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' 	=> '',

							  ),


			);

?>




But this is the file you are looking for.

this file is at trackstar/protected/tests/unit/ProjectTest.php




<?php


class ProjectTest extends CTestCase

{

	//specify the fixture file

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

							

	public function testRead()

	{


		$retrievedProject = $this->projects('project1');

		$this->assertTrue($retrievedProject instanceof Project);

		$this->assertEquals('Test Project 1',$retrievedProject->name);

	}

	

	/*public function testCreate()

	{

		//create a new project

		$newProject = new Project();

		$newProjectName = "Test Project Creation";

		$newProject->setAttributes(

									array(

											'name'			=> $newProjectName

											,

											'description'	=> "Creation of Test Project"

											,

											'create_time'	=> "2009-09-09 09:09:09"

											,

											'create_user_id'=> 1

											,

											'update_time'	=> "2009-09-09 09:09:09"

											,

											'update_user_id'=> 1

											,

										 )

									);

		//$newProject->id = 1;							

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

		

		//READ back the newly created project

		$retrievedProject=Project::model()->findByPk($newProject->id);

		$this->assertTrue($retrievedProject instanceof Project);

		$this->assertEquals($newProjectName,$retrievedProject->name);

		

	}*/

	

	/*public function testUpdate()

	{

		//update the newly created project

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

		$updatedProjectName = "Updated Test Project 2";

		$newProject->name	= $updatedProjectName;

		$this->assertTrue($newProject->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 testCRUD()

	{

		//print_r(Yii::app()->db);

		//echo "\n";

		//$this->assertNotEquals(NULL,Yii::app()->db);

		

		//create a new project

		$newProject = new Project();

		$newProjectName = "Test Project 1";

		$newProject->setAttributes(

									array(

											'name'			=> $newProjectName

											,

											'description'	=> "Test Project Number One"

											,

											'create_time'	=> "2010-01-01 00:00:00"

											,

											'create_user_id'=> 1

											,

											'update_time'	=> "2010-01-01 00:00:00"

											,

											'update_user_id'=> 1

											,

										 )

									);

		$newProject->id = 1;							

		//$this->assertTrue($newProject->save(false));

		//echo "id is".$newProject->id."\n";

		

		

		//READ back the newly created project

		$retrievedProject=Project::model()->findByPk($newProject->id);

		//$this->assertTrue($retrievedProject instanceof Project);

		//$this->assertEquals($newProjectName,$retrievedProject->name);

		

		//update the newly created project

		$updatedProjectName = "Reupdated Test Project 1";

		$newProject->name	= $updatedProjectName;

		//$this->assertTrue($newProject->save(false));

		

		// Delete the project

		$newProjectId = $newProject->id;

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

		$deletedProject = Project::model()->findByPK($newProjectId);

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


	}

	*/

}




In ProjectTest.php, please replace:


class ProjectTest extends CTestCase



with


class ProjectTest extends CDbTestCase

And I think your code should works

Hi, i am having a similar issue with fixtures and i am really struggling without finding any solution.

PhpUnit returns this:

ProjectTest.php is ok: i commented everything but $fixtures attribute and the testRead() function. Anyway, here it is:


class ProjectTest extends CDbTestCase

{

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

	

	public function testRead() 

	{ 

		$retrievedProject = $this->projects('project1'); 

		$this->assertTrue($retrievedProject instanceof Project); 

		$this->assertEquals('Test Project 1',$retrievedProject->name); 

	} 

}



I am using Yii 1.1.8: can it be a problem??

Any help would be greatly appreciated!!

Thank you.

I ran up against this same issue today. After trying all the suggestions I found within the forums, I poked around the source code, and was tipped off by the comments for the CDbFixtureManager->getFixtures method: CDbFixtureManager#getFixtures-detail … Returns the information of the available fixtures. This method will search for all PHP files under basePath. If a file’s name is the same as a table name, it is considered to be the fixture data for that table.

My database table is named ‘project’, so I renamed


/protected/tests/fixtures/tbl_project.php

to


/protected/tests/fixtures/project.php

NOTE I removed the ‘tbl_’ prefix. The book does instruct you to name it ‘tbl_project’, I just chose to use my own db naming conventions… and then forgot that I made that change.

My unit tests (with fixtures) are running fine now.

. Sherri

This hint help me a lot.

I was with error on user test and the book create the fixture using tlb_users and it have criated on database tbl_users.

thanks