NetBeans IDE and Yii projects

59 followers

This page is created to supply short directions and general tips for managing a Yii application in NetBeans IDE.

1. Code completion

To get context sensitive code completion, follow these steps:

  • Include Yii folder (assuming it is properly placed outside project directory)
    • Open "File > Project properties > PHP Include Path" and add the Yii framework root path
  • Ignore yiilite.php to avoid doubled/missing documentation
    • Open "Tools > Options > Miscellaneous > Files"
    • Add to the front of "Files Ignored by the IDE" the file "^(yiilite\.php|CVS|SCCS|...."
    • Restart NetBeans
  • Code completion in view files ###
    • Add the following PHPDoc statement at the head of the file to use code completion in view files. (you may add additional passed parameters as well)
/* @var $this PostController */
/* @var $model Post */
$this->getSomeProValue(); // possible with code completion
$model->author; // possible with code completion

Usage:

  • Typing suggestions: Ctrl-Space
  • Show Function parameters: Ctrl-P
  • Comment your own code with PHPDoc style. Here's a good example.

2. Code templates

You can create code templates for commonly used/overridden function in Yii. For example, if you want to add an beforeSave() function in your model, by typing a shortcut you can automatically have the function template in place.

  • Download this template set to get started: http://fbe.am/6OV. It contains:
    • For models: ybehaviors, yrelations, yrules, yscopes, ydefaultscope, yafterconstruct, yafterdelete, yaftersave, yaftervalidate, ybeforedelete, ybeforefind, ybeforesave
    • For controllers: yaccessrules, yfilters, yactions, ybehaviors, ybeforeaction, yafteraction, ybeforerender, yafterrender
    • Generic: yinit, yrun
  • Go to "Tools > Options > Editor > Code Templates"
  • Hit "Import", select the file, and choose "Code Templates"

Usage:

// typing: ybeforesave + TAB
// expands to:
 
/**
 * This method is invoked before saving a record (after validation, if any).
 * @return boolean whether the saving should be executed. Defaults to true.
 */
protected function beforeSave()
{
 
    return parent::beforeSave();
}

3. Testing

To run functional tests and unit tests in Yii, recommended is installing PHPUnit and SeleniumRC.

  • Install PHPUnit
  • Install SeleniumRC by getting the NetBeans plugin
    • Open "Tools > Plugins > Available Plugins"
    • Install "Selenium Module for PHP"
  • Configure project options
    • Open "File > Project properties > Sources" and set "Test Folder" to [PROJECT ROOT]/protected/tests (If the whole project testing doesn't work, try [PROJECT ROOT]/protected/tests/unit)
    • Open "File > Project properties > PHPUnit" and set "Use Bootstrap" to [PROJECT ROOT]/protected/tests/bootstrap.php, and "Use XML Configuration" to [PROJECT ROOT]/protected/tests/phpunit.xml

Usage:

  • Test whole project: Alt+F6
  • Test single file: Shift-F6
  • Check code coverage (right click project > Code Coverage)

4. Debugging

  • Install Xdebug (usually already available in your installation):
  • Include the Xdebug extension for PHP:
    • In php.ini enable (by removing ; prefix) these settings:
      zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
      xdebug.remote_enable = 1
      xdebug.remote_handler = "dbgp"
      xdebug.remote_host = "localhost"
      xdebug.remote_port = 9000

Usage:

  • Debug project: Ctrl-F5
  • Use breakpoints, walk through running code, and watch variables and objects in real-time. :)
  • If you want to stop the debugger from pausing on the first line for every request, simply turn that "feature" off by clicking: Tools > Options > PHP > Debugging > Stop at First Line (uncheck)

Got problems or questions?

Do NOT post a comment on this wiki page, but go to the forums: http://www.yiiframework.com/forum/index.php?/topic/11735-netbeans-ide-and-test-driven-development/

Links

Total 17 comments

#8272 report it
marcovtwout at 2012/05/22 10:41am
Code templates

I discovered the advantages of using code templates and added some information to the wiki page about it. See if you like it. :)

#7475 report it
oxo at 2012/03/24 02:29pm
Code completion in view files

I had to play with the order of the PHPDoc definitions to get code completion work. Tweak the lines of your definitions if you encounter any problems.

/* @var $matters Matters */
/* @var $model Order */
/* @var $this OrderMatters */
#6078 report it
Ben at 2011/12/10 11:01am
Code completion for attached behaviors

This won't work out of the box, it requires some additional typing. But at least you can make it work. Consider one of your model has some behaviors attached:

class MyModel extends CActiveRecord
{
  public function behaviors()
  {
    return array(
      'someBehavior' => 'MyBehavior'
    );
  }
}
 
class MyBehavior extends CActiveRecordBehavior
{
  public function foo()
  {
  }
}

With this, the IDE has no idea about instances of "MyModel" providing a method "foo". You won't get suggestions for:

MyModel::model()->f <Ctrl> + <Space>

But what you can do is adding some PHPDoc to your model class:

/**
 * @method void foo()
 */
class MyModel extends CActiveRecord
{
  public function behaviors()
  {
    return array(
      'someBehavior' => 'MyBehavior'
    );
  }
}

Now, the the code completion should show the method.

#5760 report it
apotter at 2011/11/10 05:13am
Awesome

thank you for the setup explanation

#5311 report it
DavidTran at 2011/10/03 02:33pm
Selenium and chrome

How to run selenium with google chrome? IE is too slow.

#4368 report it
TechBundle at 2011/06/29 04:00pm
Manual Install of PHPUnit

If you are using 64bit windows with WAMP or XAMPP install then you most likely don't have pear so go to this site and follow the instructions for installing PHPUnit manually.

http://www.unclecode.com/2010/05/install-phpunit-on-wamp-xamp-manually-without-pear/

This is an old article so I am not sure about it's download location, I used the following site to download the current version.

https://github.com/sebastianbergmann/phpunit/

#3757 report it
yakoza at 2011/05/05 10:47am
error

i did all steps but when i test whole project i get this error

PHP Fatal error: Cannot redeclare class CActiveRecord in /srv/http/weblog/framework/db/ar/CActiveRecord.php on line 1832

#3368 report it
ykee at 2011/04/06 01:23pm
NetBeans and PHPUnit

Hi! i'm newbe... What does meands the message: "Test were not generated for the following files: Review the log i Output window"?

I have totally new XAMPP (:) ) installed in Win7. New NetBeans 6.9.1, PHPUnit installed with XAMPP, then must be new.. 3.6 I doing step by step with this tuts "NetBeans IDE and Yii projects", and... i don't see any files with log, why its not generated? Whitch one window is "Output window" when i see "the log"? I'm very flustrated... ;) Sorry for my lang, and bad mood... :) Help me please.

Thanks and regards

Ykee

#3257 report it
mbi at 2011/03/29 02:01pm
PHP_CodeSniffer
#3251 report it
marcovtwout at 2011/03/28 04:32pm
@Alix

"Please only use comments to help explain the above article. If you have any questions, please ask in the forum, instead."

Please repost your question here

#3248 report it
Alix at 2011/03/28 12:38pm
where can i fond the path script to launch ?

Yes but, when i need to configure this, "Open "Tools > Options > PHP > Unit Testing",i don't find the correct path in order to launch the correct script.

#3247 report it
RussellEngland at 2011/03/28 12:21pm
@Alix installing phpunit

I'm using Ubuntu and installed it via

sudo apt-get install phpunit

Thanks, Russ

#3245 report it
Alix at 2011/03/28 12:00pm
problem to find the phpunit on linux (ubuntu)

all is in the title. I can't find the script in this folder "/usr/bin/phpunit". someone can help me plz (NetBeans IDE 6.9.1)

#3155 report it
RussellEngland at 2011/03/22 08:50am
ubuntu/linux

Great article, worked for me, thank you. I must admit, I struggled with Eclipse but Netbeans seems a lot easier to use.

For ubuntu/linux Open a terminal and enter gksudo gedit /etc/php5/apache2/php.ini

the php.ini setting should be

zend_extension=/usr/lib/php5/<DATE+lfs>/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000

Save, close and restart apache sudo apachectl -k graceful

Also, I installed netbeans from the Ubuntu Software Centre which doesn't include php. So I downloaded the php bundle from http://netbeans.org/downloads/

Extracted the zip to a directory (/home/russ/Downloads/netbeans) Then ran Downloads/netbeans/bin/netbeans from a terminal

This seems to update the netbeans to include php projects.

#1937 report it
mdomba at 2010/10/18 03:55am
Global include

If Yii is your primary framework and you are creating many projects then adding the Yii path to every project can be tedious and sometime you can just forget to add it...

To solve this Yii path can be added to the "global include path" so that it's automaticaly included for every old and new projects...

Open "Tools > Options > PHP" and add the Yii framework root path to the "Global include path"

#39 report it
waveslider at 2010/10/06 02:28pm
Test Folder

Thanks for posting this! I've been running all my test from my shell, so this is very helpful.

Just a side note: running all tests (Alt-F6) did not work for me until I changed the test directory to /path/to/myapp/protected/tests/unit

#65 report it
Ben at 2010/09/26 07:06am
Ignoring yiilite

Don't forget to escape the dot when you insert yiilite to be ignored. The line should start with:

^(yiilite\.php|

Leave a comment

Please to leave your comment.