Chapter 6 testGetStatusText- invalid arg supplied for foreach()

Don’t even know where to look for this?? Using example right out of the book. See attached files too.

What is missing? where?

Tracing stack, looks like it might be a fixture issue??

thanks, blevy009

bash$ ./phpunit.exe unit/IssueTest.php

PHPUnit 3.5.15 by Sebastian Bergmann.

.EE

Time: 1 second, Memory: 8.25Mb

There were 2 errors:

  1. IssueTest::testGetStatusText

Invalid argument supplied for foreach()

C:\xampp\htdocs\yii\framework\test\CDbFixtureManager.php:351

C:\xampp\htdocs\yii\framework\test\CDbTestCase.php:73

C:\xampp\htdocs\trackstar\protected\tests\unit\IssueTest.php:27

  1. IssueTest::testGetTypeText

Invalid argument supplied for foreach()

C:\xampp\htdocs\yii\framework\test\CDbFixtureManager.php:351

C:\xampp\htdocs\yii\framework\test\CDbTestCase.php:73

C:\xampp\htdocs\trackstar\protected\tests\unit\IssueTest.php:31

FAILURES!

2197

IssueTest.php

<?php

class IssueTest extends CDbTestCase

{

public $fixtures=array(

  'projects'=&gt;'Project',


  'issues'=&gt;'Issue',

);

public function testGetTypes()

{

  &#036;options = Issue::model()-&gt;typeOptions;


  &#036;this-&gt;assertTrue(is_array(&#036;options));


  &#036;this-&gt;assertTrue(3 == count(&#036;options));


  &#036;this-&gt;assertTrue(in_array('Bug', &#036;options));


  &#036;this-&gt;assertTrue(in_array('Feature', &#036;options));


  &#036;this-&gt;assertTrue(in_array('Task', &#036;options));


  


  // check status options


  &#036;options = Issue::model()-&gt;statusOptions;


  &#036;this-&gt;assertTrue(is_array(&#036;options));


  &#036;this-&gt;assertTrue(3 == count(&#036;options));


  &#036;this-&gt;assertTrue(in_array('Not yet started', &#036;options));


  &#036;this-&gt;assertTrue(in_array('Started', &#036;options));


  &#036;this-&gt;assertTrue(in_array('Finished', &#036;options));

}

public function testGetStatusText()

{

  &#036;this-&gt;assertTrue('Started' == &#036;this-&gt;issues('issueBug')-&gt;getStatusText());

}

public function testGetTypeText()

{

 &#036;this-&gt;assertTrue('Bug' == &#036;this-&gt;issues('issueBug')-&gt;getTypeText());

}

}

2198

Issue.php

<?php

/**

  • This is the model class for table "tbl_issue".

  • The followings are the available columns in table ‘tbl_issue’:

  • @property integer $id

  • @property string $name

  • @property string $description

  • @property integer $project_id

  • @property integer $type_id

  • @property integer $status_id

  • @property integer $owner_id

  • @property integer $requester_id

  • @property string $create_time

  • @property integer $create_user_id

  • @property string $update_time

  • @property integer $update_user_id

  • The followings are the available model relations:

  • @property User $requester

  • @property Project $project

  • @property User $owner

*/

class Issue extends CActiveRecord

{

const TYPE_BUG=0;


const TYPE_FEATURE=1;


const TYPE_TASK=2;


const STATUS_NOTSTARTED = 0;


const STATUS_STARTED = 1 ;


const STATUS_FINISHED = 2 ;





/**


 * Returns the static model of the specified AR class.


 * @return Issue the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'tbl_issue';


}


/**


* @return array issue type names indexed by type IDs


*/


public function getTypeOptions()


{


	return array(


		self::TYPE_BUG=&gt;'Bug',


		self::TYPE_FEATURE=&gt;'Feature',


		self::TYPE_TASK=&gt;'Task',


	);


}


/**


* @return array issue type names indexed by type IDs


*/


public function getStatusOptions()


{


	return array(


		self::STATUS_NOTSTARTED=&gt;'Not yet started',


		self::STATUS_STARTED=&gt;'Started',


		self::STATUS_FINISHED=&gt;'Finished',


	);


}


/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('name', 'required'),


		array('project_id, type_id, status_id, owner_id, requester_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=&gt;true),


		array('name', 'length', 'max'=&gt;256),


		array('description', 'length', 'max'=&gt;2000),


		array('create_time, update_time', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, name, description, project_id, type_id, status_id, owner_id, requester_id, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'requester' =&gt; array(self::BELONGS_TO, 'User', 'requester_id'),


		'project' =&gt; array(self::BELONGS_TO, 'Project', 'project_id'),


		'owner' =&gt; array(self::BELONGS_TO, 'User', 'owner_id'),


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'name' =&gt; 'Name',


		'description' =&gt; 'Description',


		'project_id' =&gt; 'Project',


		'type_id' =&gt; 'Type',


		'status_id' =&gt; 'Status',


		'owner_id' =&gt; 'Owner',


		'requester_id' =&gt; 'Requester',


		'create_time' =&gt; 'Create Time',


		'create_user_id' =&gt; 'Create User',


		'update_time' =&gt; 'Update Time',


		'update_user_id' =&gt; 'Update User',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);


	&#036;criteria-&gt;compare('description',&#036;this-&gt;description,true);


	&#036;criteria-&gt;compare('project_id',&#036;this-&gt;project_id);


	&#036;criteria-&gt;compare('type_id',&#036;this-&gt;type_id);


	&#036;criteria-&gt;compare('status_id',&#036;this-&gt;status_id);


	&#036;criteria-&gt;compare('owner_id',&#036;this-&gt;owner_id);


	&#036;criteria-&gt;compare('requester_id',&#036;this-&gt;requester_id);


	&#036;criteria-&gt;compare('create_time',&#036;this-&gt;create_time,true);


	&#036;criteria-&gt;compare('create_user_id',&#036;this-&gt;create_user_id);


	&#036;criteria-&gt;compare('update_time',&#036;this-&gt;update_time,true);


	&#036;criteria-&gt;compare('update_user_id',&#036;this-&gt;update_user_id);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}


/**


* @return string the status text display for the current issue


*/


public function getStatusText()


{


			&#036;statusOptions=&#036;this-&gt;statusOptions;


	if ( isset(&#036;statusOptions[&#036;this-&gt;status_id]) ) {


		return	&#036;statusOptions[&#036;this-&gt;status_id] ;


	} else {


		return &quot;unknown status ( {&#036;this-&gt;status_id })&quot; ;


	}


}


/**


* @return string the type text display for the current issue


*/


public function getTypeText()


{


	if ( isset(&#036;typeOptions[&#036;this-&gt;status_id]) ) {


		return	&#036;typeOptions[&#036;this-&gt;status_id] ;


	} else {


		return &quot;unknown status ( {&#036;this-&gt;type_id })&quot; ;


	}}

}

2199

tbl_issue.php

<?php

return array(

‘issueBug’=>array(

‘name’ => ‘Test Bug 1’,

‘description’ => ‘This is test bug for project 1’,

‘project_id’ => 1,

‘type_id’ => 0,

‘status_id’ => 1,

‘owner_id’ => 1,

‘requester_id’ => 2,

‘create_time’ => ‘’,

‘create_user_id’ => ‘’,

‘update_time’ => ‘’,

‘update_user_id’ => ‘’,

),

‘issueFeature’=>array(

‘name’ => ‘Test Bug 2’,

‘description’ => ‘This is test bug for project 2’,

‘project_id’ => 2,

‘type_id’ => 1,

‘status_id’ => 0,

‘owner_id’ => 2,

‘requester_id’ => 1,

‘create_time’ => ‘’,

‘create_user_id’ => ‘’,

‘update_time’ => ‘’,

‘update_user_id’ => ‘’,

),

);

moving on to displaying the page trackstar/index.php?r=issue showed status text showing up but not type. This lead to finding a code error I introduced when translating the ternary to an if in:

public function getStatusText()


{


			&#036;statusOptions=&#036;this-&gt;statusOptions;


	if ( isset(&#036;statusOptions[&#036;this-&gt;status_id]) ) {


		return	&#036;statusOptions[&#036;this-&gt;status_id] ;


	} else {


		return &quot;unknown status ( {&#036;this-&gt;status_id })&quot; ;


	}


}


/**


* @return string the type text display for the current issue


*/


public function getTypeText()


{


	&#036;typeOptions=&#036;this-&gt;typeOptions; 


	if ( isset(&#036;typeOptions[&#036;this-&gt;type_id]) ) {


		return	&#036;typeOptions[&#036;this-&gt;type_id] ;


	} else {


		return &quot;unknown status ( {&#036;this-&gt;type_id })&quot; ;


	}}

After fixing the code as shown above, both the status and type text show up just fine. However the error with the test:

  1. IssueTest::testGetStatusText

Invalid argument supplied for foreach()

didn’t seem to be affected??

  • blevy009