Difference between revisions of "PHPUnit installation in OS X"

Jump to: navigation, search
(Configure error logging)
(Configure PHP error logging)
Line 246: Line 246:
 
==Configure PHP error logging==
 
==Configure PHP error logging==
  
Redirect error logging to log file.
+
Redirect error logging to log file by editing php.ini file
  
 
  $ sudo vi /opt/local/etc/php54/php.ini
 
  $ sudo vi /opt/local/etc/php54/php.ini

Revision as of 17:04, 29 December 2012

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

MacPorts

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.

Installation:

  • 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) and install it.
  • Open a terminal application and update MacPorts
$ sudo port selfupdate
  • If you see "Warning: xcodebuild exists but failed to execute" execute
$ sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
Optionally add ports binaries as the first path
/opt/local/bin
in default system paths in
$ 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 in bash.

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-ldap php54-xdebug
$ sudo port select --set php php54

Configure PHP

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

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

$ 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
$ 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
</Directory>

 <IfModule dir_module>
   DirectoryIndex index.php index.html
</IfModule>
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
<?php
echo "test";

Start apache server and enable autostart

$ sudo port load apache2

Test that apache displays the "test" message on http://127.0.0.1/

Install and configure PostgreSQL

Install PostgreSQL from MacPorts

$ sudo port install postgresql92-server php54-postgresql
$ sudo port select --set postgresql postgresql92

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 
  • Create new "moodle" database
$ sudo -u postgres createdb -E utf8 moodle
  • Optionally set password and protect connection to your server via TCP/IP by changing "trust" to "md5" on the line containing 127.0.0.1 in pg_hba.conf.
$ sudo vi /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
  • Restart the PostgreSQL server
$ sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql92-server/postgresql92-server.wrapper restart
  • Restart Apache server
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Install and configure MariaDB

Install MariaDB from MacPorts

$ sudo port install mariadb-server php54-mysql
$ sudo port select --set mysql mariadb

Run the following commands to configure and secure MariaDB

$ sudo -u _mysql /opt/local/lib/mariadb/bin/mysql_install_db
$ sudo port load mariadb-server 
$ /opt/local/lib/mariadb/bin/mysql_secure_installation
  • Create new "moodle" database
$ mysql -u root -p --execute="create database moodle;"
  • Optionally set default mysql socket in PHP.ini to '/opt/local/var/run/mariadb/mysqld.sock'
  • Restart Apache server
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Configure Moodle

  • Create workspace directories
$ 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
$ vi /server/workspace/moodle/config.php

with the following content:

<?php  // Moodle configuration file
unset($CFG);
global $CFG;
$CFG = new stdClass();
$CFG->dbtype    = 'pgsql';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'moodle';
$CFG->dbuser    = 'postgres';
$CFG->dbpass    = ''; // set password here if used
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
  'dbsocket' => '/tmp/',
);
 
/*
// Uncomment to test MySQL compatibility.
$CFG->dbtype    = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'moodle';
$CFG->dbuser    = 'root';
$CFG->dbpass    = ''; // set password here if used
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
  'dbsocket' => '/opt/local/var/run/mariadb/mysqld.sock',
);
*/
 
$CFG->wwwroot   = 'http://127.0.0.1/moodle';
$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');

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

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 --colors

Other tasks

Configure PHP error logging

Redirect error logging to log file by editing php.ini file

$ sudo vi /opt/local/etc/php54/php.ini

Add following line:

error_log = /server/logs/php_errors.log 

Restart apache

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Display error log

$ tail -f /server/logs/php_errors.log

Test that error is added to the log

$ php -r echoooo

Other useful applications