Skip to content
Alfonso Roman edited this page Nov 13, 2013 · 21 revisions

Setting up Behat environment for Mac OSX

This guide will require you to download Moodle4Mac which is a copy of Moodle that ships with a copy of MAMP. Alternatively you can download MAMP on its own, but the version that ships with Moodle seems to be configured with the proper PHP modules out of the box. We will set this up to run our Moodle codebase right next to the vanilla Moodle install. Having a vanilla Moodle install also serves as a reference of what a passing test for Behat or PHPUnit should look like.

Setting up MAMP

Download Moodle4Mac and install like any other OSX app.

Adding PHP and MySQL to path

To make it easier to access MAMP's PHP and MySQL binaries via command line, we'll add them to our system path. This is so that you can type php foobar.php instead of /Applications/MAMP/bin/php/php5.4.10/bin/php foobar.php while you're running your Behat scripts. In your ~/.bash_profile add the following:

export PATH=/Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php/php5.4.10/bin/:$PATH  

Depending on your version of MAMP, those paths may differ slightly. You will need to reload your profile, you can run the following command:

source ~/.bash_profile

Check if you can access PHP and MySQL

$ php -v
PHP 5.4.10 (cli) (built: Jan 21 2013 15:12:32) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
$ mysql -u root -p root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.29 Source distribution
...

If you are having issues, here is some reference material: http://www.metaltoad.com/blog/getting-command-line-access-php-and-mysql-running-mamp-osx

Setting up Moodle

You can access vanilla Moodle via the default port localhost:8888, but we want to run our custom code, so we'll tweak MAMP's preferences to change Apache's document root. Open your MAMP preferences and select the Apache tab. Change your document root to something that's more easily accessible, perhaps in your home folder like so /Users/alfonso/Sites. This assumes I have a directory named Sites in my home folder.

Go to your Sites folder and we'll clone our Moodle code and set up the moodledata dir.

cd ~/Sites
git clone git@github.com:ucla/moodle.git
mkdir moodledata
chmod 777 moodledata

You will now have a local Moodle instance that you can access via localhost:8888/moodle. You will need to set up a database and config file. I've found it easier to simply import a pre-loaded database dump than to have Moodle go through the install process. Here is a fine specimen: https://test.ccle.ucla.edu/vagrant/new_moodle_instance.sql

That Moodle sql dump requires that your Moodle config_private.php file contains this salt $CFG->passwordsaltmain = 'a_very_long_salt_string';. Your config should look something like:

$CFG->wwwroot   = 'http://localhost:8888/moodle';
$CFG->dataroot  = '/Users/alfonso/Sites/moodledata';
$CFG->admin     = 'admin';
$CFG->directorypermissions = 0777;
$CFG->passwordsaltmain = 'a_very_long_salt_string';

The SQL dump is updated for Moodle 2.5.2, so if you're running that codebase you will not have to run any site upgrades.

Moodle confg

Assuming you have a site that's now working, add the following to your config_private.php:

$CFG->behat_prefix = 'bht_';
$CFG->behat_dataroot = '/Users/alfonso/Sites/moodledata/behat';
$CFG->behat_switchcompletely = true;

Those config values set up your Moodle Behat environment. Moodle will generate a new set of tables for the Behat environment with the given prefix. We're also directing Moodle to completely switch off the site to 'acceptance testing' mode. We're also telling Moodle where to put all the Behat generated files in the moodledata dir.

Installing PHP Composer

PHP Composer is required to download the Behat dependencies. To install PHP Composer go to your Moodle project folder:

cd ~/Sites/moodle
curl -sS http://getcomposer.org/installer

That's it!

Switching to Behat acceptance testing mode

You now need to switch to Behat mode. If it's your first time running this, Composer will download all the required dependencies.

php admin/tool/behat/cli/init.php
...
...
...
Acceptance tests environment enabled, to run the tests use:
 vendor/bin/behat --config /Users/alfonso/Sites/moodledata/behat/behat/behat.yml

Moodle has generated the Behat yaml file, and given you the command to execute. You're all set!

Clone this wiki locally