Author Archives: Erik Bauffman

PHP Programming with E_STRICT beware!

I recently switched to programming PHP with error_reporting set to E_STRICT, since I want to know what’s going on. You have to watch out when extending classes and trying to overrule the parameters of the child method. An example:

php-e-strict-errors

When executing you will get the following error “Strict Standards: Declaration of B::test() should be compatible with that of A::test() in /path/to/file/test.php on line 18

When I remove the parameter ‘value’ from the method ‘test’ in the class ‘B’, everything’s fine. Write that down.

Running PHPUnit tests in Zend Studio

Why unit testing? There are quite a few reasons, but those I can think of right now are:

  1. It’s not hard, all it takes is some time to think things through
  2. Whenever I get a bugreport, I try to write it as a test to make sure this bug can never slip into my code unnoticed.
  3. It’s a nice way to make sure that the things you already wrote, keep working after you’ve changed some of your code.

Let’s take a look at the steps you need to do to get this working in Zend Studio (currently I’m using 6.1.2). First you need to add the PHPUNIT library location to the include of the project you want to write unit tests for. Right click your project, then click ‘properties’.

zend-unit-testing-project-properties

Then click on the ‘include path’ to the left. You will see an overview of the already added ‘include paths’ to this project.

zend-unit-testing-include-path

Click the ‘add variable’ option and select ‘PHPUNIT_HOME’ and click ‘OK’.

zend-unit-testing-add-include-path

So far, so good. Now we have to create a PHPUNIT Test Case. Rightclick on the folder where you want to this new file to go and click ‘new’ > ‘PHPUnit Test Case’.

zend-unit-testing-add-test-case

I used the details below and did not select a specific element to test. If you do choose an element or class to test, all the default test methods will be added to the new file.

zend-unit-testing-add-test-case-details

When you’re done adding the test file, you’ll get a pretty messy generated file. I’ve changed bits of it and added a few ‘demo’ functions as you can see below.

zend-unit-testing-code-example

I wrote 4 tests, using the 3 methods ‘returnTrue’, ‘returnFalse’ and ‘greaterThan’. These methods are obviously pretty straightforward, but it gives you an idea of the way that you can test them.

You can run the test by rightclicking the editor area and choosing ‘run as’ > ‘PHPunit Test’.

zend-unit-testing-run-test

You should, considering your configuration has been set properly, get the next results.

zend-unit-testing-success-results

I broke the ‘greaterThan’ function on purpose, to make sure that those tests would fail and then you get the following results.

zend-unit-testing-error-results

So far this little basic post about unit testing with PHPUnit from within Zend Studio. There are a lot more ways to test your code, but this brief introduction should get you started.

Timezone php.ini setting

date.timezone = “Europe/Brussels”

Make sure this setting is properly configured in your php.ini, since date/time functions may trigger notices otherwise.

Zend Debugger using MAMP Pro 1.8.2 PHP executable

In Zend Studio (currently using 6.1.2) you can set the default PHP executable that you want to use to run PHP. I wanted to use the PHP executable of my MAMP Pro (1.8.2) to run my Unit tests from within Zend, since it has support for mbstring and lots of other sweetness. It took me some time to figure out how to do this, but this is how I did it.

Open MAMP Pro and go to File > Edit Template > PHP5 php.ini

mamp-pro-edit-template

Then go all the way to the bottom of the opened file. There you will have to add the Zend Debugger extension as a php.ini directive. I used the ZendDebugger.so that’s included in the Zend Studio 6.1.2 installation. Make sure your path is correct.

mamp-pro-edit-template-zend-extension

Now in Zend Studio you have to go to the overview of your PHP executables that looks like below and click ‘add’.

zend-studio-overview-php-executables

I used the default details for a MAMP PRO 1.8.2 installation, as you can see below.
Name: PHP 5.2.10 (CLI)
Executable path: /Applications/MAMP/bin/php5/bin/php
PHP ini file (optional): /Library/Application Support/appsolute/MAMP PRO/conf/php.ini

zend-studio-add-php-executable