VGTech is a blog where the developers and devops of Norways most visited website share code and tricks of the trade… Read more

Are you brilliant? We're hiring. Read more

Unit testing


NPM, Travis, Node 0.8 and the “Caret Operator”


If you have a node.js project that you want to have tested using Travis-CI, you may run in to a problem if you want to support node <= 0.8. The reason for this is the new caret operator, which was introduced in node-semver.

This module is used by npm to figure out how to resolve versions for your dependencies, and while it’s a great idea, it fails to work on older node versions without upgrading npm first. It’s actually fairly trivial to fix this, however. (more…)

Using PHP’s built-in web server in your test suites


As of PHP-5.4.0 the CLI SAPI provides a built-in web server. The web server is designed for development purposes, and serves requests sequentially. This web server can come in really handy when the need for an httpd arises during (integration) tests.

In this post I’ll use PHPUnit as the testing framework, and I’ll show you how to start the web server during the bootstrap process, and how to shut it down when the test suite is finished.


Running multiple versions of PHPUnit


The latest version of PHPUnit (3.6.4 at the time of this writing) does not play well with the Zend Framework extensions (Zend_Test_PHPUnit). After asking Matthew Weier O’Phinney about this he answered that they had standardized on PHPUnit-3.4 for ZF1.

Having just upgraded to the latest version of PHPUnit on our servers we were no longer able to test our Zend Framework applications. One option was to downgrade PHPUnit, but since we were already using some of the new features this was not going to happen.

One solution to this problem is to run multiple versions of PHPUnit and use specific versions for specific test suites. Not optimal, but at least it gets the job done. (more…)

Unit testing with streams in PHP


Using the memory/temporary stream provided by php:// stream wrapper you  can create a stream with read and write access directly to RAM or to a temporary file.

This gives you the possibilty to write unit tests that does not rely on a specific file, resource or stream, but rather on data provided by the test itself.

Creating a memory stream is easy; $fp = fopen('php://memory', 'r+');. Now you have a resource that can be used and manipulated just like any other stream or file.

Mocking the file system using PHPUnit and vfsStream


This article is about how to mock the file system when writing unit tests, and it will be rather code-heavy. If you are not familiar with the concept of unit testing this article might not be the best place to start. There will be other articles regarding unit testing on this blog, so keep coming back for more. (more…)