PHP Namespace Galore

October 29, 2008

What Frederico says!

PHP CLI with parameters

October 21, 2008

For a current project, I needed a (PHP) CLI script that had to accept a certain parameter list. Using CLI, you can do:

php /path/to/script/script.php arg1 arg2 arg3

I retrieve the parameters in the script by using the $_SERVER[‘argv’] array:

$param1 = $_SERVER[‘argv’][0];

$param2 = $_SERVER[‘argv’][1];

I kinda feel dirty using the $_SERVER[‘argv’] variable. Any decent method to do this?

Lately, I started to use phpUnderControl to monitor my application under development. Since your favourite colleagues can be bitches, you can try to monitor not only your application but also your fellow developers.

Utilities like PHPCodeSniffer, PHPUnit (code coverage features)… can easily provide you with reasons to bitchslap one-another. Using non-standard-like variable names: 1 bitchslap, using non-standard-classnames: 2 bitchslaps, etc.

Seriously, you should check ik out. You can, for example, setup a project to be build after every SVN commit. A build can consist of running all tests, creating code coverage reports, generating API-documentation, …

phpUnderControl parses the generated reports and displays them to you nicely. Like so:

Automated integration metrics

Automated integration metrics

Permission to say: “Fuck Yeah”!. Granted.

Apart from the above metrics, you can generate a list giving you a code test coverage report. (example for some models):

Code test coverage

Code test coverage

I know, I still have some work to do. Enough reasons to start testing your applications, have some monitoring fun AND reasons for some bitchslap-galore?!?

Mocking your database

October 1, 2008

I think mocking the database in integration tests is a bad idea. It is not a fact, it is my personal opinion. I think that when you are running your integration tests, you should try to mimick the live environment as good as possible. Using a mock to replace your database object doesn’t fit this idea.

If you decide to use the database object you use in your application, you may want to change the actual backend. I use Mysql for different projects, but running integration tests against a mysql test-database is kind of slow. You have to consider filling up you database with a defined dataset (setUp()) for every test and that can take a while. (latest project is about 1sec/test) Instead, you could use an SQLite backend.

Using the SQLite backend is easy when you use a database component that’s worth it. Zend_Db for instance, easily offers you that functionality. By using a different adapter (SQLite instead of MySQLi) you can run your tests a lot faster.

I haven’t found much information about using the SQLite adapter and initializing the database. I guess it all comes down to exporting you MySQL-code to setup you database (DDL) to an SQLite format. Using the XML dataset feature in PHPUnit you can populate that in-memory database and you’re ready to go. Faster.

Integration testing

September 26, 2008

I recently started using the PHP continous integration tool phpUnderControl in some of the projects I’m currently working on. A little before that, I started writing integration tests.

I have to admit, I really had to convince myself to start writing tests because a lot of people writing tests sometimes make me feel a bit awkward. I don’t feel very comfortable with those kind of people. They give me the impression they always have everything under control, that everything is “all good“. Fact is, I’m trying hard to keep these guys out of my friendzone. Either way, they scare the girls, and boys shouldn’t do that.

Anyway, since I have a girlfriend, I thought what the fuck, so I started Unit Testing (how about my reason to start unit testing?)

Sebastian BergmannIt seemed I had the choice between SimpleTest and PHPUnit but since Germany always wins, I chose Sebastian Bergmann’s framework. Luckily, Seba developed a framework using his immense forehead resulting in a easy-to-use yet powerful development tool. I installed everything in a jiffy and started out with the PHPUnit Pocket Guide.

The cool thing is that you can use you terminal (Linux) or command prompt (Windows) and look like a real professional when your boss passes by. Since I wanted to test my projects models, I started using the database testing tools available by the framework. The models I use for the project aren’t really complex, hence my choice of writing integration tests. I setup a small XML datasetfile used in my setUp-blocks to run the tests against.

Now, during my intensive period (2 weeks) of using Tests, I already came across some problems concerning integration testing. The problem is that you want to test your code against a certain dataset (setup in XML) that gets loaded into a test-database by the framework. I wrote about 100 tests and since my XML dataset kinda grows like hell, It now takes about 1 second/test. I started drinking a lot (of water) these last days and I managed to start taking 100-second pisses every 45 minutes. The problem is that I want to run these tests more frequently thus faster.

I read some things about mocking objects, but I haven’t figured out whether one can use that with database objects. (say an instance of Zend_Db)

If anyone knows how to handle this kind of problem, feel free to let me know. You don’t have to come by, just write a comment or a postcard…

Take-off

September 25, 2008

A new blog, a fresh start. Since me and myself decided to try to enjoy developing webapplications even more, this might be an opportunity to start a new blog about webdevelopment.

A man has got to eat, and since I got fed up flipping burgers @McDonalds while I was studying, I started my new career about a year ago as PHP Developer. Asides PHP, I keep myself occupied in a personal (everlasting) struggle with Javascript. Hooray for AJAX, AJAH and XmlHttpRequest’s other bastard sons.

My main field of interest is still the beautiful language of PHP. This explains why I will be using PHP to illustrate stuff like design principles, design patterns or overall best practices.

Important sidenote: I will be writing articles because I think I have found a better way to figure out certain problems. I do this in English even though this isn’t “mi main language“. The only reason why I chose to do so is my goal to build up an audience of interested readers which might exceed the total number of 2. (My mother also knows about the internet.)

If you think you have a solution to a certain issue which is better than the one I proposed, please share it with me. If not, silence is golden.

Right now, I’m going to pet my ego. Ciao.