Skip to content
Rex Lorenzo edited this page May 19, 2016 · 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

Go to Moodle4Mac and download the MAMP version of Moodle you are developing for and install like any other OSX app. Then start running the MAMP application. You may get a notice saying that the application is from an unrecognized developer. To get pass this message, right click the application and click "Open", then "Okay" on the message that appears.

You may also want to turn off the OpCache for MAMP because there might be a delay between making a change in your code and it taking effect. Click on "Preferences" > "PHP" and then set "Cache" to "Off".

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.5.3/bin/php foobar.php while you're running your Behat scripts. In your ~/.bash_profile (you may need to create it) add the following:

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

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

source ~/.bash_profile

Check if you are now accessing the MAMP versions of PHP and MySQL and can start using MySQL:

$ which php
/Applications/MAMP/bin/php/php5.5.3/bin/php
$ which mysql
/Applications/MAMP/Library/bin/mysql
$ mysql -u root --password=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

Another cause of problems is if the temp directory that stores PHP sessions does not exist or has the incorrect permissions. Try this fix:

mkdir -m 777 /Applications/MAMP/tmp/php

Mysql connection errors

If you see this, the mysql.sock file might not be where OSX expects it. Here's how to fix it: Warning: mysql_connect(): [2002] No such file or directory

Setting up Moodle

Right now, you can access vanilla Moodle via the default location http://localhost:8888/moodle, but we want to run our custom code, so we'll add a link to our own code.

  1. Create a Sites folder, clone our Moodle code and set up the data dir in the MAMP directory.
mkdir ~/Sites
cd ~/Sites
git clone git@github.com:ucla/moodle.git ccle
cd ccle
git submodule init && git submodule update
mkdir -m 777 /Applications/MAMP/data/ccle
mkdir -m 777 /Applications/MAMP/data/behat
  1. Link the newly created Moodle code to MAMP.
ln -s ~/Sites/ccle /Applications/MAMP/htdocs/ccle
  1. Setup the configuration file for Moodle.
cd ~/Sites/ccle
ln -s local/ucla/config/shared_behat_moodle-config.php config.php
cp -p config_private-dist.php config_private.php

Note that the configuration file linked to should be "shared_behat_moodle-config.php" when running Behat tests.

  1. Edit config_private.php and set/add the following:
$CFG->dbname    = 'ccle';
$CFG->dbuser    = 'ccle';
$CFG->wwwroot   = 'http://localhost:8888/ccle';
$CFG->dataroot  = '/Applications/MAMP/data/ccle';
  1. Create the ccle database.
mysql -u root --password=root --execute="CREATE DATABASE ccle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
mysql -u root --password=root --execute="GRANT ALL PRIVILEGES ON ccle.* TO 'ccle'@'localhost' IDENTIFIED BY 'test'; FLUSH PRIVILEGES;"
  1. Go to http://localhost:8888/ccle/install.php and install Moodle. Once installed, you can start using your new Moodle instance via http://localhost:8888/ccle, while still being able to access vanilla Moodle at http://localhost:8888/moodle.

Post install configuration

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

$CFG->behat_prefix = 'bht_';
$CFG->behat_dataroot = '/Applications/MAMP/data/behat';
$CFG->behat_wwwroot = 'http://127.0.0.1:8888/ccle';
$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/ccle
curl -sS http://getcomposer.org/installer | php

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 /Applications/MAMP/data/behat/behat/behat.yml

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