Setting CiviCRM Buildkit to use for running Unit Tests in extension development

In this blog I will describe what I done so setup my local environment to run CiviCRM Buildkit for running unit tests in extension development.

Locally I run Arch Linux so some commands might be different on your machine.

First I download CiviCRM Buildkit to a local directory:

# first install all the required packages:
[jaap ~]$ sudo pacman -S npm nodejs #Pacman is the acrh linux command for apt-get
[jaap ~]$ cd /home/jaap
[jaap ~]$ mkdir bin
[jaap ~]$ cd bin
[jaap ~]$ git clone https://github.com/civicrm/civicrm-buildkit.git
[jaap ~]$ cd civicrm-buildkit
# This will download all tools needed to run Civicrm Buildkit
[jaap ~]$ ./bin/civi-download-tools 

# In my case I had to install/update nodejs
[jaap ~]$ npm update
[jaap ~]$ npm install fs-extra

Now I am ready to run AMP Config

[jaap ~]$ cd bin/civicrm-buildkit
[jaap ~]$ amp config
Enter db_type: mysql_dsn
Enter dsn> mysql://root:pass@localhost:3306 #my mysql dsn 
Enter perm_type: [0] "None"
Enter hosts_type: none
Enter httpd_type: none

Now I need to update my httpd config:

[jaap ~]$ sudo vim /etc/httpd/conf/httpd.conf #this is my appache config
# At the following line at the bottom of the file
Include /home/jaap/.amp/apache.d/*.conf #change this to your personal directory
#Restart apache
[jaap ~]$ sudo systemctl stop httpd
[jaap ~]$ sudo systemctl start httpd
# I have linked the build directory to my /var/www directory
[jaap ~]$ cd bin/civicrm-buildkit/
[jaap ~]$ rm -rf build
[jaap ~]$ ln -s /var/www build

Now test whether AMP is working:

[jaap ~]$ cd bin/civicrm-buildkit
[jaap ~]$ amp test

When AMP test did run without any errors you should add your civicrm-buildkit directory to your path

[jaap ~]$ vim .bashrc
# At the bottom of the file add the following line:
export PATH="$PATH:/home/jaap/bin/civicrm-buildkit/bin"

Now log out and restart after restarting you are ready to run Buildkit to deploy a site.

[jaap ~]$ cd bin/civicrm-buildkit/build
# Create a new site a new site in my /var/www will automaticly be linked to name.local.com in your situation that could differ.
[jaap ~] civibuild create dmaster --type "drupal-clean" --civi-ver "master" --url "http://dmaster.local.com" --admin-pass "admin1234"

Create an extension with a unit test in dmaster:

[jaap ~]$ cd /home/jaap/bin/civicrm-buildkit/build/dmaster/sites/all/modules/civicrm/tools/extensions
[jaap ~]$ civix generate:module org.civicoop.yourmodule
[jaap ~]$ cd org.civicoop.yourmodule
[jaap ~]$ civix generate:test CRM_Yourmodule_TestClassForTestingSomethingUseful

# Now run your test
[jaap ~]$ phpunit4 tests/phpunit/CRM/Yourmodule/TestClassForTestingSomethingUser.php

Links

* https://github.com/civicrm/civicrm-buildkit/blob/master/doc/download-update.md
* https://buildkit.civicrm.org/#/welcome
* https://civicrm.org/blog/totten/developers-extension-testapalooza
* https://phpunit.de/manual/current/en/appendixes.assertions.html