Netbeans (6.8), PHPunit and Yii

How to create PHPunit tests cases in Netbeans 6.8 for an Yii web application

Ref.:

http://www.yiiframework.com/forum/index.php?app=forums&module=forums&section=findpost&pid=6301

Environment:

Product Version: NetBeans IDE Dev (Build 200909221401)

Java: 1.6.0_10; Java HotSpot™ Client VM 11.0-b15

System: Windows XP version 5.1 running on x86; Cp1252; es_ES (nb)

Apache/2.2.6 (Win32) PHP/5.2.9-2

Yii framework 1.0.9

PHPUnit 3.3.15

  1. Create a web application project with yiic tools (e.g. yiic webapp testdrive).

  2. Create a netbeans php project from the source code obtained (File>New Project>Categories:PHP>Projects: PHP Application with Existing Sources).

  3. Make sure that your Yii framework is added to your PHP Include Path

  4. Create a directory for your tests under testdrive folder. You may want to use the Files tab.

  5. Create a file in the tests folder. Name it bootstrap.php. Add the following code and save.




<?php

require_once dirname(__FILE__).'/../../yii/1.0.9/framework/yii.php'; // make sure your path to yii.php is correct

?>



  1. Right click testdrive project. Select Properties. Project Properties pops up. select PHPUnit then in the PHPUnit configuration activate Use Bootstrap and browse to your bootstrap.php file.

  2. Right click Source Files under testdrive folder in the Projects tab and select Tools > create PHPUnits tests.

  3. You may see some folders created for you contaning unit test skeleton. You may want tweak them to fit your needs.

Example

a. In SiteControllerTest setUp(), comment as

//$this->object = new SiteController;

or change this line to

$this->object = new SiteController(‘site’);

b. Add this test

testHello() {$this->assertEquals(‘Hello’, ‘Hello’);}

c. Right click SiteController.php and select Test (or Ctrl+F6).

Note: Netbeans 6.8 is now under Comunity Acceptance Testing process.

Thx man, this is great :)

Absolutely helpful stuff, I wish I could rate twice!

I have one issue with creating/running tests right…

For example I created test for model Country.php it looks like this




require_once 'PHPUnit/Framework.php';

class CountryTest extends PHPUnit_Framework_TestCase {


  protected $object;


  protected function setUp() {

    $this->object = new Country;

  }


  function testHello() {

    $this->assertEquals('Hello', 'Hello');    

  }  

}



When running test, such output is generated




Fatal error: Class 'Country' not found in c:\xampp\htdocs\testapp\tests\protected\models\CountryTest.php on line 19



PhpUnit, Yii are of course in include path in Netbeans and bootstrap file is correct, but something is still wrong with paths.

Try to add something like this to your CountryTest class


require_once dirname(__FILE__).'/../../../protected/models/Country.php'; //make sure to point to the Country.php

Tried that too (I mean including model)… still now working, but error changed to:




Fatal error: Call to a member function getDb() on a non-object in c:\xampp\htdocs\testapp\framework\db\ar\CActiveRecord.php on line 876



ps. My Yii is 1.0.9

I ran into this because I had a condition option in one of the relationships.

/Tommy

You may want to do some changes. One solution may be as follow:

  1. Create a TestApplication class in the same folder as bootstrap.php



class TestApplication extends CWebApplication

{

	public function __construct($config=null)

	{

		Yii::setApplication(null);

		parent::__construct($config);

	}

}



  1. Add to the bootstrap.php this line

require_once(dirname(__FILE__).'/TestApplication.php');

  1. Add to your TestCountry class

public $db;

  1. Change the setUp method to



    protected function setUp() {

        $config=array(

            'basePath'=>dirname(__FILE__),

            'components'=>array(

                'db'=>array(

                    'class'=>'system.db.CDbConnection',

                    'connectionString'=>'your connection string here',

                    'username'=>'your username here',

                    'password'=>'your password here',

                ),

            ),

        );

        $app=new TestApplication($config);

        $app->db->active=true;

        $this->db=$app->db;

        $this->object = new Country;

    }

  1. (Optional)

protected function tearDown() {

        if($this->db)

           $this->db->active=false;


    }

  1. (Optional) if you want you can test tableName() if you have in your model a tableName method



    public function testTableName() {

        $this->assertEquals ($this->object->tableName(), 'Country');

    }



Nope, I deleted all relations and still the same… :confused:

On second thought I recall my problem was session-related (Yii::app->user).

May help somebody else, though.

/Tommy

Hi again everyone,

Unfortunately I didnt manage to run PhpUnit with Yii… there’s one thing that cant get off my mind - how do you able to write/run model tests without creating Yii application with db config?

Try this:

  1. Comment in bootstarp.php this line:

//require_once(dirname(__FILE__).'/TestApplication.php');

  1. Change setUp() to:

protected function setUp() {


        $this->db = new CDbConnection('mysql:host=localhost;port=3306;dbname=db_name','user','password');

        $this->db->active=true;

        CActiveRecord::$db=$this->db;

        $this->object = new Country;

}

Thx abajja, as always first to answer - now everything works :)

There’s useful!

nice… share. thanks

nice… thanks for sharing… :D

This information is very useful. So far, I was avoiding UnitTest But I’ll work from today itself. I am excited!!!

I also having some issues with PHPUnit / Netbeans / Yii .

When i right click on a model and choose "Create PHPUnit Test" i get the following error :

PHPUnit Skeleton Generator 1.2.0 by Sebastian Bergmann.

Fatal error: Class ‘CActiveRecord’ not found in C:\xampp\htdocs\cbdb\protected\models\Type.php on line 19

Call Stack:

0.0002     125368   1. {main}() C:&#092;xampp&#092;php&#092;phpunit-skelgen:0


0.0039     237776   2. SebastianBergmann&#092;PHPUnit&#092;SkeletonGenerator&#092;Command::main() C:&#092;xampp&#092;php&#092;phpunit-skelgen:45


0.0108     581264   3. ReflectionClass-&gt;newInstanceArgs() C:&#092;xampp&#092;php&#092;pear&#092;SebastianBergmann&#092;PHPUnit&#092;SkeletonGenerator&#092;Command.php:171


0.0108     581552   4. SebastianBergmann&#092;PHPUnit&#092;SkeletonGenerator&#092;TestGenerator-&gt;__construct() C:&#092;xampp&#092;php&#092;pear&#092;SebastianBergmann&#092;PHPUnit&#092;SkeletonGenerator&#092;Command.php:171


0.0114     591632   5. include_once('C:&#092;xampp&#092;htdocs&#092;cbdb&#092;protected&#092;models&#092;Type.php') C:&#092;xampp&#092;php&#092;pear&#092;SebastianBergmann&#092;PHPUnit&#092;SkeletonGenerator&#092;TestGenerator.php:125

Hello CVP,

Did you solve this issue??

Unfortunately no - However i am using a newer version of Netbeans and PHP Unit. Maybe i should post a new thread about this.