PHPUnit installation in OS X

Revision as of 22:16, 4 June 2014 by Petr Škoda (škoďák) (talk | contribs) (Install and configure PostgreSQL)

Jump to: navigation, search
This step-by-step guide is intended for developers only, it cannot 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.3.0-10.9-Mavericks.pkg) and install it.
  • Open a terminal application and update MacPorts
$ sudo port selfupdate
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 or later 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 php55 php55-gd php55-mbstring php55-iconv php55-curl php55-zip php55-soap php55-intl php55-xmlrpc php55-openssl php55-ldap
$ sudo port select --set php php55

Configure PHP

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

Verify PHP installation by executing the binary

$ php -v
Expected result is something like:
PHP 5.5.13 (cli) (built: May 31 2014 20:13:49)

Optionally install php55-xdebug extension if you need it, but be ready for significant slowdown.

$ sudo port install php55-xdebug

Install and configure Apache

Install Apache from MacPorts

$ sudo port install php55-apache2handler apache2

Configure Apache

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php55.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 postgresql93-server php55-postgresql
$ sudo port select --set postgresql postgresql93

Run the following commands to configure PostgreSQL

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

$ sudo port load postgresql93-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/postgresql93/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.postgresql93-server/postgresql93-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

Install and configure OpenLDAP

Install OpenLDAP server from MacPorts

$ sudo port install openldap

Configure OpenLDAP

$ cd /opt/local/etc/openldap/
$ sudo cp /opt/local/var/openldap-data/DB_CONFIG.example /opt/local/var/openldap-data/DB_CONFIG
$ sudo cp DB_CONFIG.example DB_CONFIG
$ sudo cp ldap.conf.default ldap.conf
$ sudo cp slapd.conf.default slapd.conf

Alter slapd.conf to include following settings

$ sudo vi slapd.conf
include         /opt/local/etc/openldap/schema/core.schema
include         /opt/local/etc/openldap/schema/cosine.schema
include         /opt/local/etc/openldap/schema/nis.schema
include         /opt/local/etc/openldap/schema/inetorgperson.schema
modulepath      /opt/local/libexec/openldap
moduleload      back_bdb.la
suffix          "dc=yourcomputer,dc=local"
rootdn          "cn=admin,dc=yourcomputer,dc=local"
rootpw  yourpassword

Unfortunately there are permission problems in openldap port, you need to start the server manually each time you use it or change permissions of openldap dirs and /opt/local/var

$ sudo /opt/local/libexec/slapd -d 3

or another option is to edit the launcher file to use root account instead of ldap

$ sudo vi /opt/local/etc/LaunchDaemons/org.macports.slapd/slapd.wrapper
 /opt/local/libexec/slapd -u root -f /opt/local/etc/openldap/slapd.conf
$ sudo port load openldap

Build LDAP tree structure in a new shell prompt

$ ldapadd -H ldap://127.0.0.1 -D "cn=admin,dc=yourcomputer,dc=local" -W
dn:dc=yourcomputer,dc=local
objectClass:dcObject
objectClass:organizationalUnit
dc:yourcomputer
ou:YOURCOMPUTER

and press CRTL-D to finish input.

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';
 
// Optionally fix and uncomment settings for external LDAP tests
//define('TEST_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
//define('TEST_ENROL_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
//define('TEST_ENROL_LDAP_BIND_PW', 'yourpassword');
//define('TEST_ENROL_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
 
//define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
//define('TEST_AUTH_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
//define('TEST_AUTH_LDAP_BIND_PW', 'yourpassword');
//define('TEST_AUTH_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
 
 
require_once(dirname(__FILE__) . '/lib/setup.php');

Install PHPUnit

Note: PHPUnit installation via PEAR is not supported any more, always use Composer.

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

Create log directory

$ mkdir /server/logs

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