Chapter 5 and 6

Hi,

I’m getting the following error when running a phpunit test on ProjectTest.php

trackstar\protected\tests>phpunit unit/ProjectTest.php

PHPUnit 3.5.6 by Sebastian Bergmann.

Exception: Unknown method ‘projects’ for class ‘ProjectTest’.

I looked through the forum and found a similar question asked, but the reason was a typo in the tbl_project.php file, which I did not happen to have. Could you please help me?

Thank you in advance!

Here is my ProjectTest.php




<?php

class ProjectTest extends CDbTestCase

{

        public $fixtures = array(

                'projects' => 'Project', 

    );

        

        /*

        public function testCRUD() 

        { 

                

                $prjs = $this->projects;

                

                

                //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, 

                                            )

                                                                ); 

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

                

                //UPDATE the newly created project  

                $updatedProjectName = 'Updated Test Project 1'; 

                $newProject->name = $updatedProjectName; 

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


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

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

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

                $this->assertEquals($updatedProjectName,$updatedProject->name); 


                //DELETE the project  

                $newProjectId = $newProject->id; 

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

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

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

                

                

                

        }

        */

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

                                  }

        

        

        

} 




Here is my tbl_project.php




<?php

return array(

        'project1'=>array(

                'name' => 'Test Project 1',

                'description' => 'This is test project 1',

                'create_time' => '2009-09-09 00:00:00',

                'create_user_id' => '',

                'update_time' => '2009-09-09 00:00:00',

                'update_user_id' => '',

        ),

        'project2'=>array(

                'name' => 'Test Project 2',

                'description' => 'This is test project 2',

                'create_time' => '2009-09-09 00:00:00',

                'create_user_id' => '',

                'update_time' => '2009-09-09 00:00:00',

                'update_user_id' => '',

        ),

        'project3'=>array(

                'name' => 'Test Project 3',

                'description' => 'This is test project 3',

                'create_time' => '2009-09-09 00:00:00',

                'create_user_id' => '',

                'update_time' => '2009-09-09 00:00:00', 

                'update_user_id' => '',

        ),

);

?>



I would really appreciate your help!

Thanks again

This is typically a result of an underlying error when trying to insert data into the table related to the fixture, tbl_project in this case. Like maybe you are trying to insert NULL into a column that is defined as NOT NULL (both of the create_user_id/update_user_id fields are candidates here)

Can you also please provide the [sql]CREATE TABLE[/sql] statement from which your table was created? And please ensure you are providing the results of the table in your test database (or whatever database your test.php config file has defined).

I had this issue today and it turned out that I had named my fixture tbl_Project instead of tbl_project.

The exception can be caused by a syntax error in the tbl_project.php fixture file.

I have a similar setup as in the original post and I’m getting the same errors.

I’m using Yii 1.1.2, as advised by the book, downloaded as yii-1.1.2.r2086p.tar.gz.

My table is set up as follows:

Host: localhost

Database: trackstar_test

Generation Time: Mar 09, 2012 at 06:33 PM

Generated by: phpMyAdmin 3.2.4 / MySQL 5.1.37

SQL query: describe tbl_project;

Rows: 7

Field Type Null Key Default Extra

id int(11) NO PRI NULL auto_increment

name varchar(100) NO NULL

description text NO NULL

create_time datetime YES NULL

create_user_id int(11) YES NULL

update_time datetime YES NULL

update_user_id int(11) YES NULL

I enabled logging in MySQL, to see what’s being sent to the DB, and here’s what I get when I run the test:

120309 18:47:01 53 Connect root@localhost on trackstar_test

	   53 Query	SET NAMES 'utf8'


	   53 Query	SET FOREIGN_KEY_CHECKS=0


	   53 Query	SHOW COLUMNS FROM `tbl_project`


	   53 Query	SHOW CREATE TABLE `tbl_project`


	   53 Query	SET FOREIGN_KEY_CHECKS=1


	   53 Query	SET FOREIGN_KEY_CHECKS=0


	   53 Query	SHOW COLUMNS FROM `tbl_project`


	   53 Query	SHOW CREATE TABLE `tbl_project`


	   53 Query	DELETE FROM `tbl_project`


	   53 Query	ALTER TABLE `tbl_project` AUTO_INCREMENT=1


	   53 Query	SET FOREIGN_KEY_CHECKS=1


	   53 Quit	

I get no errors in MySQL’s error log.

So it’s not like the definition of the table is preventing the insertion. The log shows that the insertion of records from the fixture is never even attempted.

So I decided to investigate this further, and I made this change (see the echo line) to CDbFixtureManager.php




	public function loadFixture($tableName)

	{

		$fileName=$this->basePath.DIRECTORY_SEPARATOR.$tableName.'.php';

		echo "\nWTF:$fileName:";

		if(!is_file($fileName))

			return false;




Here’s what I get:

bogdan-hapcas-macbook:tests bhapca$ phpunit unit/ProjectTest

PHPUnit 3.3.17 by Sebastian Bergmann.

WTF:/Users/bhapca/Sites/trackstar/protected/tests/fixtures//Users/bhapca/Sites/trackstar/protected/tests/fixtures/tbl_project.php.php:

WTF:/Users/bhapca/Sites/trackstar/protected/tests/fixtures/{{project}}.php:E

Time: 0 seconds

There was 1 error:

  1. testRead(ProjectTest)

Exception: Unknown method ‘projects’ for class ‘ProjectTest’.

/Users/bhapca/Sites/yii112/framework/test/CDbTestCase.php:76

/Users/bhapca/Sites/trackstar/protected/tests/unit/ProjectTest.php:7

FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

bogdan-hapcas-macbook:tests bhapca$

Look at what file names this framework is trying to pass to ‘is_file’. Those don’t seem right. The fixture stands no chance to be loaded from those paths.

I did some more investigations and came to what seems a bug in the framework. While I don’t claim to fully understand how this thing works, I compared that CDbFixtureManager.php file between version 1.1.2 and the current version 1.1.10 (from yii-1.1.10.r3566.tar.gz). Attached is a picture with a change that shows the bug.

2651

yii-crap.png

Could it be that following the examples in the book using Yii version 1.1.2 is a waste of time ? Did anyone tried it with another version and was successful ? Or perhaps is this yii-1.1.2.r2086p.tar.gz I used a bad build, and there is a better one available ?

I was getting following error;

The issues was in my unit/ProjectTest.php there was a tab in my string for ‘projects’ so it looked like ’ projects’ . After removing that all tests passed.