Yii Framework Forum: Netbeans (6.8), PHPunit and Yii - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Netbeans (6.8), PHPunit and Yii How to create PHPunit tests cases in Netbeans (6.8) for an Yii web app Rate Topic: ***** 7 Votes

#1 User is offline   abajja 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 05-March 09
  • Location:Barcelona, Spain

Posted 30 September 2009 - 10:29 AM

*
POPULAR

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

Ref.:
http://www.yiiframew...ndpost&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
?>

6. 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.
6. Right click Source Files under testdrive folder in the Projects tab and select Tools > create PHPUnits tests.
7. 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.
8

#2 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 30 September 2009 - 04:32 PM

Thx man, this is great :)
0

#3 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 01 October 2009 - 05:26 PM

Absolutely helpful stuff, I wish I could rate twice!
0

#4 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 26 October 2009 - 02:56 AM

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.
0

#5 User is offline   abajja 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 05-March 09
  • Location:Barcelona, Spain

Posted 26 October 2009 - 03:49 AM

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

0

#6 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 26 October 2009 - 06:46 AM

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
0

#7 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 October 2009 - 07:59 AM

View Postspamec, on 26 October 2009 - 06:46 AM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#8 User is offline   abajja 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 05-March 09
  • Location:Barcelona, Spain

Posted 26 October 2009 - 10:49 AM

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


2. Add to the bootstrap.php this line
require_once(dirname(__FILE__).'/TestApplication.php');


3. Add to your TestCountry class

public $db;


4. 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;
    }

5. (Optional)
protected function tearDown() {
        if($this->db)
           $this->db->active=false;

    }


6. (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');
    }

1

#9 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 26 October 2009 - 10:56 AM

Nope, I deleted all relations and still the same... :/
0

#10 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 October 2009 - 01:34 PM

View Postspamec, on 26 October 2009 - 10:56 AM, said:

Nope, I deleted all relations and still the same... :/


On second thought I recall my problem was session-related (Yii::app->user).
May help somebody else, though.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#11 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 31 October 2009 - 11:16 AM

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?
0

#12 User is offline   abajja 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 05-March 09
  • Location:Barcelona, Spain

Posted 31 October 2009 - 02:24 PM

Try this:
1. Comment in bootstarp.php this line:
//require_once(dirname(__FILE__).'/TestApplication.php');

2. 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;
}

1

#13 User is offline   spamec 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-February 09

Posted 31 October 2009 - 04:18 PM

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

#14 User is offline   巡洋艦 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 316
  • Joined: 18-December 09
  • Location:Beijing, China

Posted 27 November 2010 - 10:57 PM

There's useful!
0

#15 User is offline   Prasetyo Aji 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-February 09

Posted 09 March 2011 - 04:20 PM

nice... share. thanks
0

#16 User is offline   Kamarul 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 05-April 11
  • Location:Malaysia

Posted 28 April 2011 - 04:46 AM

nice.. thanks for sharing... :D
The only valid measurement of code quality: WTF / Minute
0

#17 User is offline   anup 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 18-January 12
  • Location:INDIA

Posted 12 December 2012 - 12:32 AM

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

#18 User is offline   CVP 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-March 12

Posted 04 February 2013 - 05:56 AM

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:\xampp\php\phpunit-skelgen:0
0.0039 237776 2. SebastianBergmann\PHPUnit\SkeletonGenerator\Command::main() C:\xampp\php\phpunit-skelgen:45
0.0108 581264 3. ReflectionClass->newInstanceArgs() C:\xampp\php\pear\SebastianBergmann\PHPUnit\SkeletonGenerator\Command.php:171
0.0108 581552 4. SebastianBergmann\PHPUnit\SkeletonGenerator\TestGenerator->__construct() C:\xampp\php\pear\SebastianBergmann\PHPUnit\SkeletonGenerator\Command.php:171
0.0114 591632 5. include_once('C:\xampp\htdocs\cbdb\protected\models\Type.php') C:\xampp\php\pear\SebastianBergmann\PHPUnit\SkeletonGenerator\TestGenerator.php:125
0

#19 User is offline   Surface 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-February 13

Posted 07 February 2013 - 09:11 PM

Hello CVP,

Did you solve this issue??
0

#20 User is offline   CVP 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-March 12

Posted 07 February 2013 - 11:33 PM

View PostSurface, on 07 February 2013 - 09:11 PM, said:

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.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users