Web and application development


Unit testing models in Laravel 5 with SQLite in Memory

So you want to run Unit tests with PHPUnit on your Laravel 5 application, and do some actual database interaction instead of mocking data?

I do that for one of my applications and I'm using SQLite with in memory database to increase performance event more. I'll quickly show how i have set it up.

Config and environment in Laravel 5

Laravel 5 uses the DotEnv library for environment configuration. That means that you have .env file in the project root, to define variables specific to your enviroment, and also one .env in the production to specify the environment.

Your local .env file could look something like this:



It's just key-value pairs. They are used by the different config files.

The config/database.php file (rows not related to this text is removed):

return [
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'testing' => [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,

As you see, the config/database.php has references to the variables in the .env file using the env() function, with looks like env(key, default). that functions get the corresponding value from the .env file. And one that is particular interesting in this post is DB_CONNECTION. As you saw in the .env file that was set to mysql. Laravel will be using the mysql connection with the host, database and credentials configured in the .env file.

Testing environment

But when we run phpunit we want to use the testing connection from the config/database.php with uses in-memory sqlite database.

This is done by setting php environment variables in the phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>

This will set the environment to testing with `APP_ENV (if you use that in config/app.php) and also select the testing connection in the database config when you run phpunit.


Updated portfolio

Update portfolio with page about Hillsong Kids checkin


Raspberry Pi Printer server for LabelWriter


So I have finally come around and started playing with a Raspberry Pi. If you dont know what it is, its a one card arm mini computer, read more at What i tried to accomplish here was to get the pi to work as remote accessible printer server for a Dymo LabelWriter 450.

The goal was to build a web application that could be used from a tablet and on a click on a button in the webapp ui send a request to the pi with a number of paramterers that would for two labels and be printed. The whole thing used a a checkin system for children whit a label for the child and the parent dropping of the child.


After some thouhts i desicided to build the system with the following components and parts:

  • Dymo LabelWriter 450
  • Raspberry Pi, model B with Rasbian Wheezy
  • Apache server (with PHP) on the raspberry reciving the printing requests
  • C++ application on the raspberry doing the actual printing

Getting the printer up and running

Install cups libraries and other necessary libraries to build

pi@raspberrypi ~ $ sudo apt-get install libcups2-dev libcupsimage2-dev g++ cups cups-client

Download Dymo SDK and install the drivers

Download the Dymo LabelWriter SDK for Linux located at

pi@raspberrypi ~ $ wget

Now we need to unpack, configure and install the drivers. This is done with the autoconfigure file that is packed with the sdk. Change directory to the unpacked foleder and run ./configure

pi@raspberrypi ~ $ tar xvf dymo-cups-drivers-1.4.0.tar.gz
pi@raspberrypi ~ $ cd dymo-cups-drivers-
pi@raspberrypi ~/dymo-cups-drivers- $ sudo ./configure
pi@raspberrypi ~/dymo-cups-drivers- $ sudo make
pi@raspberrypi ~/dymo-cups-drivers- $ sudo make install

That is everything to get the drivers for the printers installed on the system.

Add the printer

To use the printer, we need to add it in the system first, this is easiest done with cups webadmin. And since the raspberry is only aceessed remote with ssh, we need to install a command line based web browser , I'm using links2. You could use the graphical interface as well. But since the rasperry is going to be a embedded system i'll do everything with command line.

pi@raspberrypi ~ $ sudo apt-get install links2

And the we need to add the pi user to the printer admin group to be allow to login to the printer admin.

pi@raspberrypi ~ $ sudo usermod -a -G lpadmin pi

Navigate to http://localhost:631/admin with links2

pi@raspberrypi ~ $ links2 http://localhost:631/admin

cups admin
Select "Add printer".

cups login
Log in with pi user. Default user pi and password raspberry.

cups local printers
In the list of local printers, the "Dymo LabelWriter 450" should appear, mark and continue.

cups add printer
Give printer a name and description, and continue.

Select the Dymo LabelWriter 450 drivers if found. otherwise you need to locate the file that make install created earlier.

cups manage
That should be everything for the printer. To verify, go the admin interface and click Manage Printers ant the LabelWriter should appear in the list.

The Code

The printing application

So I built the printing application in c++ from the example code supplied by DYMO.
The includes used for the application, some standard c++ libraries and the cups libraries:

#include <iostream>
#include <cups/cups.h>
#include <cups/ppd.h>
#include <string>
#include <stdio.h>
#include <map>
#include <exception>

This code lists all printers and can be useful to see if your program can find the LabelWriter.

int i;
cups_dest_t *dests, *dest;
int num_dests = cupsGetDests(&dests);
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
  if (dest->instance)
    printf("%s/%s\n", dest->name, dest->instance);

The actual printing does not require that much code. PrinterName is a string with the name of the LabelWriter you chose when you added it before. ImageName is a string with image to be printed, for me it's a png image that is generated by the web server in the next section.

int num_options = 0;
cups_option_t* options = NULL;

num_options = cupsAddOption("PageSize", "w167h288", num_options, &options);
num_options = cupsAddOption("scaling", "100", num_options, &options);
cupsPrintFile(PrinterName, ImageName, "Print Label", num_options, options);
cupsFreeOptions(num_options, options);

The options for the PageSize parameter can be found in the lw450.ppd file (/dymo-cups-drivers- It lists all paper types and sizes available for that printer.


For compiling the application these flags is used:

g++ `cups-config --cflags` PrintLabel.cpp `cups-config --libs` -o PrintLabel

The receiving HTTP server

The web server, running apache with PHP, is just used for receiving remote calls with parameters and generate an image and then execute the previous c++ application  to print that image.

$firstname = $_GET['firstname'];
$id = $_GET['id'];
$fileName = "kid_".$id.".png";
//... (image generation with GD and write image to disk) ...
$printOutput = array();
exec("./PrintLabel " . $fileName, $printOutput, $status);