Difference between revisions of "PHPUnit installation in OS X"

Jump to: navigation, search
(Install and configure PostgreSQL)
(Install and configure PostgreSQL)
Line 134: Line 134:
* Install pgAdmin III from http://www.pgadmin.org/download/macosx.php.
* Install pgAdmin III from http://www.pgadmin.org/download/macosx.php.
* Launch pgAdmin and create new "moodle" database and set password for "postgres" user.
* Launch pgAdmin and create new "moodle" database and set password for "postgres" user.
* Restart apache server:
* Restart Apache server:
  sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart
  sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Revision as of 18:48, 27 December 2012

This step-by-step guide is intended for developers only, it can not be used for production servers!


The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading open-source software on the Mac OS X operating system.


  • Install the latest version of Xcode from Mac App Store.
  • In the meantime, register on the Apple Developer Connection for free (https://developer.apple.com/).
  • Launch Xcode and go to "Xcode / Preferences... / Downloads" and install "Command Line Tools" (you will be asked for your developer id and password).
  • Download the dmg image from http://www.macports.org/install.php (for example MacPorts-2.1.2-10.8-MountainLion.dmg).
  • Execute the installer from the MacPorts dmg.
  • Open a terminal application and update MacPorts using the following command.
sudo port selfupdate
  • If you see "Warning: xcodebuild exists but failed to execute" execute the following command.
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
Optionally add ports path
as the first path:
sudo vi /etc/paths

Install Apache, PHP and PostgreSQL

In older OSX versions disable the built-in web server by:

  • opening System Preferences,
  • clicking Sharing, and
  • making sure "Web Sharing" is disabled.

In OSX 10.8 unload the default Apache server:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Quit terminal application and launch it again to get updated PATH.

Install and configure PHP

Install PHP from MacPorts:

sudo port install php54 php54-gd php54-mbstring php54-iconv php54-curl php54-zip php54-soap php54-intl php54-xmlrpc php54-openssl php54-xdebug

Configure PHP with the following command.

sudo cp /opt/local/etc/php54/php.ini-development /opt/local/etc/php54/php.ini

Select newly compiled PHP binary as the default.

sudo port select --set php php54

Verify PHP installation by executing the binary.

php -v
Expected result is something like:
PHP 5.4.10 (cli) (built: Dec 20 2012 19:30:08)

Install and configure Apache

Install Apache from MacPorts:

sudo port install php54-apache2handler apache2

Configure Apache with the following command.

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php54.so
  • Edit the Apache configuration file by running the following command.
sudo vi /opt/local/apache2/conf/httpd.conf
  • In the document, add the following content, which will change the document root, add index.php as a directory index file and add the php type handler.
DocumentRoot "/server/workspace"
<Directory "/server/workspace">
   Options FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all

 <IfModule dir_module>
   DirectoryIndex index.php index.html
AddType application/x-httpd-php .php

Create workspace directory and set permissions to allow both developer and Apache to write to server directory:

sudo mkdir /server
sudo mkdir /server/workspace

sudo chmod -R +a "_www allow read,delete,write,append,file_inherit,directory_inherit" /server
sudo chmod -R +a "`whoami` allow read,delete,write,append,file_inherit,directory_inherit" /server

Create a test file:

vi /server/workspace/index.php
echo "test";

Start apache server and enable autostart:

 sudo port load apache2

Test the apache displays the "test" message.

Install and configure PostgreSQL

Install PostgreSQL from MacPorts:

 sudo port install postgresql92-server php54-postgresql

Run the following commands to configure PostgreSQL.

sudo mkdir -p /opt/local/var/db/postgresql92/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb' 

sudo port load postgresql92-server 
  • Optionally password protect connection to your server via TCP/IP by changing "trust" to "md5" on the line containing in pg_hba.conf.
sudo vi /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf
# IPv4 local connections:
host    all             all               md5
  • Restart the PostgreSQL server with the following command.
sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql92-server/postgresql92-server.wrapper restart
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Configure Moodle

You can clone the latest Moodle release using Git.

  • Create workspace directories with the following commands.
sudo mkdir /server/moodledata
sudo mkdir /server/phpu_moodledata
  • Clone the Moodle git master as follows.
cd /server/workspace
git clone git://github.com/moodle/moodle.git
  • Add a config.php file by running the following command.
vi /server/workspace/moodle/config.php
  • In the file add the following content.
<?php  // Moodle configuration file
global $CFG;
$CFG = new stdClass();
$CFG->dbtype    = 'pgsql';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'moodle';
$CFG->dbuser    = 'postgres';
$CFG->dbpass    = '';
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
  'dbpersist' => 0,
  'dbsocket' => '',
$CFG->wwwroot   = '';
$CFG->dataroot  = '/server/moodledata';
$CFG->passwordsaltmain = 'some random rubbish goes here';
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = 1;
$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/server/phpu_moodledata';
require_once(dirname(__FILE__) . '/lib/setup.php');
  • Restart Apache with the following command.
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart
  • Optionally to manually creating a config.php file you can go to and finish standard installation.

Install PHPUnit

Since Moodle 2.3.4 it is recommended to use Composer dependency manager instead of PEAR for PHPUnit installation.

Composer dependency manager

cd /server/workspace/moodle
curl -s https://getcomposer.org/installer | php
php composer.phar install --dev

PEAR (not recommended)

Unfortunately there seem to be some problems in php54-pear port, run the following commands to install pear manually.

sudo port -v install pear-install-phar
cd /opt/local/lib/php/pear/
sudo php install-pear-nozlib.phar
Add include path
include_path = ".:/opt/local/lib/php54/pear/"
to php.ini.
sudo nano /opt/local/etc/php54/php.ini

Continue phpunit installation once you can execute the pear binary.

sudo pear upgrade
sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit
sudo pear install phpunit/DbUnit

If you get the error "Moodle can not find PHPUnit PEAR library" or similar, make sure that the folder containing the PHPUnit library is in your include_path in php.ini. e.g.: if PHPUnit is located at /usr/lib/php/PHPUnit/ make sure your include path has /usr/lib/php in it.

Initialise test environment

Open a terminal application and run the following commands.

cd /server/workspace/moodle
php admin/tool/phpunit/cli/init.php

Execute all tests

Open a terminal application and run the following commands.

cd /server/workspace/moodle
vendor/bin/phpunit #if installed via Composer
phpunit            #if installed via PEAR