Categories
MySQL PHP

Getting started with AMP stack on Mac OS X Lion

The other day, I got myself a Macbook Pro which runs OS X Lion. Coming from the Linux world where everything is available at finger tips via package managers, I was at first lost. I tried MAMP and Zend Server CE. I had some issues with Zend Server CE and custom virtual hosts – mostly because I couldn’t figure out which bits to be changed. MAMP worked out pretty well. But I wanted to use the native builds of the AMP stack. I know about macports and homebrew but wanted to do things myself!

(1) Mac OS X ships with built in apache. I enabled “Web Sharing” from System Preference > Sharing. It turned on viewing of my ~/Sites directory at http://localhost/~masnun/. It works but not pretty helpful.

(2) I edited /etc/apache2/httpd.conf to change the document root to ~/Sites. Now I could see my home page on localhost.

(3) I tried creating virtual hosts at ~/VirtualHosts – this worked with MAMP but with the built in Apache, it can not load the virtualhosts from that location because of permission issues. So instead, I reinstated the default location for vhosts inside the “extra” directory. I appended all my virtualhosts in one file. It works!

(4) “NameVirtualHosts *” and a separate virtual host for localhost is must.

(5) Mac OS X has a built in PHP. I just created the default php configuration file at /etc/php.ini

(6) I downloaded MySQL from their website. Installed the packages and imported my existing databases.

(7) PHP by default expects the mysql unix socket to be in /var/mysql/mysql.sock but the package from mysql creates the socket at /tmp/mysql.sock. So I had to edit the socket for mysql, pdo-mysql and mysqli. I needed to change them all because WP uses mysql, phpmyadmin uses mysqli and ZF uses pdo.

(8) I ran the pear installer (as a phar file inside /usr/lib/php) to install pear and pecl.

(9) To install mcrypt extension, I downloaded libmcrypt and source of PHP 5.3.10. I changed directory to PHP’s mcrypt extension directory. Ran “phpize”, “make” and “sudo make install”. Copied the generated extension to my custom extensions directory.

(10) Restarted apache at every step. It’s now working fine 🙂

Categories
PHP

Running PHPUnit on Git hook

I use Git and I love Git! I knew about Git hooks but never used in real life. So today I decided to get my hands dirty with Git hooks. I had a feeling it’d be awesome if I could use the “pre-commit” hook to do some routine processing before the changes are committed. I had two things in mind: 1) Unit Testing with PHPUnit or 2) Generating documentation using DocBlox. I chose Unit testing favouring simplicity.

Summary: I have a git repo for my PHP project. I have written some unit tests for the project and I want to run an automated unit test before changes are committed. If the unit tests succeed, the changes will be committed, if one of the tests fails, the commit will not proceed. This will ensure that I am always committing *working* codes to my Git repo.

Understanding Git Hooks
Git hooks are nothing but executable scripts which are run by Git on special events. If the hook script returns an exit code of 0, Git continues with whatever it was doing. For any non zero exit code, Git halts the operation. It’s that simple!

Git hooks are stored in the “.git/hooks” directory of a git repo. There are some samples already generated for us to check out! The script should be named with the hook, that is for the “pre-commit” hook the file name should also be “pre-commit”.

My Lame Unit Test
To begin with, we need at least one test case. So I wrote this really lame unit test and saved it as “Mytest.php”

Try running the script with PHPUnit like this:

Output:

Okay, so we have a valid unit test that passes. Now let’s add a Git pre-commit hook to run this unit test before commit is made.

Setting Up Git Hook
Assuming that your project is already under Git DVCS, navigate to .git/hooks and create a file named “pre-commit” with the following contents:

PS: The pre-commit script is a modified version of this nice Gist: https://gist.github.com/975252

Make the “pre-commit” file executable by issuing this command:

NB: This is important that you make the script executable otherwise it will not function as a hook.

Making a Commit
We shall create some file or make some changes and try to commit. In my case, I just added the “Mytest.php” file and tried to commit. Here’s the output:

Aha! All tests passed and the commit was made. Now let’s force the unit test to fail. Change the unit test to look like this:

And let’s commit now:

Compare the output with previous output. There is nothing similar to:

The commit was not made because the test failed. So, it worked! 😀

I am really loving what it is going to offer me! I shall be writing tests for most of my projects so I never unintentionally break something in a commit. Unit testing is just one type of validation, I believe we could use lots of others (eg. PHP Code Sniffer) 🙂

Categories
Linux PHP

Installing PHPUnit on Ubuntu

Summary: I am on Ubuntu 11.10 and have the necessity to install PHPUnit. From my past experience, I know that installing via repository installs old version + fails to run. So I decided to install it via PEAR. Here’s a walk through!

Upgrade PEAR
Damn! The shipped version of pear via the “php-pear” package is outdated. The first thing we need to do is upgrade our PEAR setup. We have to ask pear to upgrade itself!

That should upgrade our pear to the latest version available!

Setup PHPUnit
To install PHPUnit, we have to set auto discovery turned on and discover the PHPUnit PEAR channel. Then we force install PHPUnit with all dependencies. That’s simple!

If everything goes okay, we have a working setup of PHPUnit by now! Type “phpunit –version” on console and check out the output! Ooops! It didn’t work! We’re getting an error message regarding “require_once()”. The issue is obvious, the PEAR directory which contains all PEAR packages haven’t been added to the php include_path.

Fix it this way:

Vim editor window will open with the empty buffer. If you’re not comfortable with vim, just use another text editor. Type in the following directives:

Save and quit. We had to do this manually because pear doesn’t automatically set the include_path to the pear location (which is “/usr/share/php/”).

Now try on terminal:

The output should look like:

Happy Unit testing!