Skip to content

Weapon of Choice for PHP: Netbeans

As a "sort of" prerequisite to my upcoming articles about PHP, I'm going to talk about my preferred development environment when working with projects in PHP.

Now I usually only have a few requirements for an IDE when writing code and those are smart intellisense, stable, and organized project file(s) that doesn't clutter your workspace. Netbeans have fit those requirements quite nicely for the past few years. The personal experience that I have had with Netbeans that I really like is that it has worked the way I want it to, even when my way of working has been flexible and changing. It always seems to find a way to adapt to how I want to work.

In the past, I would simply use it as a fancy text editor with intellisense, and then upload the files to a webserver through Netbeans since it had an easy upload function once you configured it. I don't like to run a full web stack on my local development machine because I like to keep the number of running programs and services to a minimum, especially in a Windows environment. So nothing too complicated but I can do better nowadays thanks to the different way I work today. Let's make Netbeans able to run our tests with PHPUnit using only the PHP-CLI.

Windows instructions:
Note: At the time of this writing, the latest PHPUnit doesn't work with the current stable version 8.2 of Netbeans. I had to use a nightly dev build instead.

  • First thing we will want to do is make sure the PHPUnit plugin is installed in Netbeans. This can be found in Tools -> Plugins under either the Available Plugins tab or the Installed tab.

  • After that, we need to download PHP and extract the zip archive to a simple path like "C:\PHP7.1.4\".

  • Next, we will download the PHAR file for PHPUnit. I placed the file in the following location: "C:\PHP7.1.4\phpunit\"

  • Then let's create a phpunit.bat file in the same folder where phpunit.phar is located with the following contents:
    @echo off

    set PHPEXE=C:\PHP7.1.4\php.exe

    "%PHPEXE%" "C:\PHP7.1.4\phpunit\phpunit-6.1.0.phar" %*

  • Finally, let's tell Netbeans where this .bat file is located for PHPUnit. The dialog below can be found under Tools -> Options.

That is the minimum needed to get PHPUnit configured for Netbeans. Let's try using it in a new PHP project as there is some configuration that needs to be done per project.

First, after creating a new PHP project or opening an existing one, we will want to go into its properties and enable PHPUnit as a Testing Provider and add a test directory as shown in the image below:

Now, if you are following along with a new project, let's add the following files to our project.
Source Files: main.php
<?php
function add4($x)
{
        return $x + 4;
}
?>

Test Files: Bootstrapper.php
<?php
chdir('/Users/RJ/Desktop/Environment/TestsProject'); // Put path to your project source files here

require('main.php');
?>

Test Files: FirstTest.php
<?php
use PHPUnit\Framework\TestCase;

final class FirstTest extends TestCase
{
        /**
         * @test
         */

        public function can_add_4()
        {
                $this->assertEquals(10, add4(6));
        }
}
?>

Before we move on to running the tests, I want to explain about the "Bootstrapper.php" in Test Files. This file is needed to setup the application environment when running tests. This is configured in the Project Properties -> Testing -> PHPUnit dialog under "Use Bootstrap". Normally, I have the chdir() call and register autoload inside the Bootstrap file, but to keep things simple I'm just using a simple require call.

Now let's run the tests and see what we get.

The test passed! We finally have our first running test in Netbeans without using a webserver. There is one more thing we can do to make running tests even better. And that is to install Xdebug so that it is possible to debug our tests when something isn't going quite right. But I will save that for the next time.

Comments

Display comments as Linear | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.