Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/atoum/netbeans-sample


https://github.com/atoum/netbeans-sample

atoum atoum-starter netbeans netbeans-atoum php tutorial xdebug

Last synced: 3 months ago
JSON representation

Lists

README

        

### Netbeans + atoum = love <3

Hi all! If you are here today, it's because you know and probably use [Netbeans](https://netbeans.org/features/php/). But you probably don't know [atoum](http://atoum.org) yet. Too bad, you missed one of the most powerfull and pleasant to use unit testing framework for PHP. Today, atoum is directly integrated in Netbeans and I'm here to introduce this new feature.

We, the atoum team, have one main goal: to provide users a modern, simple and intuitive tool to write unit tests. Integrating atoum in an IDE is a step farther to ease the use of such a tool. In this blog post, I'll try to cover the main functionalities: we will bootstrap a new project using atoum, configure it to be able to trigger our tests and then, we'll see how to debug them. Do not worry, thanks to Tomas and all the Netbeans' team work, this will be easy and will only happen through the Netbeans' user interface.

#### Bootstrapping

Once you have created your new Netbeans project, you will have to build a basic directory structure. Let's use something like this:

```
.
├── src
│ └── netbeans
│ └── sample
└── tests
└── units
└── netbeans
└── sample
```

What does this tell us ? We prepared our project to host a ```netbeans\sample``` namespace where we will put our sources and a directory to host our tests. The last thing to do before writing the first line of code is to include atoum. atoum is available as a [PHAR archive](http://downloads.atoum.org/nightly/mageekguy.atoum.phar), a [composer package](https://packagist.org/packages/atoum/atoum), or [on Github](https://github.com/atoum/atoum). In the example I'll use [composer](http://getcomposer.org/) with the following content in the ```composer.json``` file:

```
{
"name": "netbeans/sample",
"description": "Netbeans + atoum = love <3",
"require-dev": {
"atoum/atoum": "dev-master"
},
"autoload": {
"psr-0": {
"netbeans\\sample": "src"
}
}
}
```

This could be automatically generated with Netbeans but I won't cover this step in detail here as it's the matter of one click: right click on your project name and choose Composer and then Install. As soon as you have your dependencies installed, you will be ready to continue.

#### Configuring

This is where we will start using Netbeans features. Let's start by opening the project properties window (right-click on the project name and select "Properties" then go to the "Testing" item in the left tree):

![Project properties - Testing](resources/images/configuring-1.png "Project properties - Testing")

Now, we have to tell Netbeans which directory will hold our tests (still in project's properties, go to the "Sources" item in the left tree):

![Project properties - Testing](resources/images/configuring-2.png "Project properties - Testing")

Use the "Browse" button next to the "Test folder" field and choose the ```tests``` directory. You can now save the settings and see that Neatbeans marks the ```tests``` as a special folder: "Test Files". This will let it automatically find your test files based on your source filenames.

For now, that's all you need to do to make Netbeans use atoum to run your tests. I told you atoum was a really simple tool: it's a self-contained tool and can run your test without any configuration! Netbeans takes full advantage of this and only asks you to choose your favorite tool by checking a single option! Of course, when you'll be more familiar with the tool, you'll be able to customize it more precisely.

For basic use cases, this should be sufficient and we can go to the next step: writing our first unit test because TDD is a good practice and atoum is oriented on this way of working (but you can also use it other way).

#### Writing unit tests

Before we start writing tests, let me explain some points I skipped until now: when we prepared our workspace, we created a deep tree in the tests directory. This is something that is required by atoum: you have to use namespace to structure your sources and tests and the tests should live in a namespace matching ```tests\units``` and your source namespace. In our example, our tests will live in the ```tests\units\netbeans\sample``` namespace as our sources are in ```netbeans\sample```. We also follow the PSR-0 convention so our directories match the namespaces to ease autoloading.

So let's go! We want to write a Calculator class which will provide some basic operations: add, multiply and divide. Let's create the file ```tests/units/netbeans/sample/Calculator.php```:

```
if($object = new \netbeans\sample\Calculator())
->then
->integer($object->add(1, 2))->isEqualTo(3)
;
}
}
```

We now have our first unit test, so just launch it: click on its filename in the left tree and, as you prefer, use the CMD+F6 shortcut on Mac OS X or use the right-click menu and select "Run". Netbeans should display the result of the test run, saying that a test failed. Exactly what we expect: we tested a method which we don't have yet implemented (TDD FTW!):

![Unit tests - Failure](resources/images/result-2.png "Unit tests - Failure")

What we have to do now is create our tested class and write the ```add``` method to make our test pass. Ass I told you, atoum and Netbeans expect your project to be PSR-0 compliant so our class will lay in the ```src/netbeans/sample``` directory:

```