My Opinion on XML Based Testing in Magento

Magento architect Anton Kril asked about opinions on XML based tests:

My answer does not fit into a Tweet, so here’s a short blog post. Continue reading “My Opinion on XML Based Testing in Magento”

TDD Kata 14 – Exclamation Mark Series

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 14 – Exclamation Mark Series”

PHP 7: Type-safe Arrays of Objects

With PHP 7 you can choose to write much more type-safe code than before, thanks to scalar type hints and return types.

function repeat(string $text, int $times) : string;

But what about arrays? There’s still only the generic “array” type hint, you cannot specify what’s in the array. For the IDE, you can add PhpDoc comments:

/**
 * @return User[]
 */
function allUsers() : array;

Now IDEs like PhpStorm can help with code completion for items in the returned array. But we cannot benefit from any checks at runtime, like with real type hints.

For arguments, there is a partial workaround, using variadic arguments. Take the following function

/**
 * @param User[] $users
 */
function deleteUsers(array $users);

With variadic arguments we can rewrite it to

function deleteUsers(User ...$users);

Usage also changes, to deleteUsers(...$users); In this call, the argument $users will be “unpacked” into single variables, and in the method itself “packed” back into an array $users. Each item is validated to be of type User. $users can also be an iterator, it will be converted to an array.

Unfortunately there is no similar workaround for return types, and it only works for the last argument.

See also: Type hinting in PHP 7 – array of objects

I’ve used this technique a lot in PHP 7 code, but I’ve found another one that’s even better and does not come with the mentioned flaws:
Continue reading “PHP 7: Type-safe Arrays of Objects”

TDD Kata 13 – Boolean Expression Engine

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 13 – Boolean Expression Engine”

TDD Kata 12 – Ugly Trivia Game

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 12 – Ugly Trivia Game”

TDD Kata 11 – Reversed Binary Numbers

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 11 – Reversed Binary Numbers”

PHPUnit, PhpStorm and docker-compose

I wanted to run tests with PHPUnit on a docker environment, which was set up with docker-compose, and use the PhpStorm integration. Since PhpStorm 2013.2 there is a docker intergration which works well for single containers, but unfortunately it does not seem to use the running network of containers. So for example my PHP container does not get access to the MySQL container for integration tests.

If that’s not a problem for you, you will not need what I am going to explain here, read this instead: https://blog.jetbrains.com/phpstorm/2016/11/docker-remote-interpreters/

Continue reading “PHPUnit, PhpStorm and docker-compose”

Set up KeePass with KeeAgent on Linux

When migrated my workstation to Linux, one important tool was the password manager. I use KeePass, so there are two options around:

I tried both. On KDE, KeePassX integrates nicer into the Linux desktop while KeePass on Mono looks and feels a bit foreign. But the lack of features like auto open and plugins made me ditch KeePassX quite fast. Two important plugins for me are KeePassHttp, which integrates KeePass with Chrome as password manager, and KeeAgent which automatically registers SSH Keys with passphrases at a running SSH Agent.

Setting up KeeAgent was a bit tricky and all instructions I found on the internet were incomplete, so I’ll share how I did it (on Arch Linux with keepass-plugin-keeagent-beta 0.9.1-1). There are two ways to use the plugin: as client for an existing SSH agent or as standalone SSH agent. I choose client mode to always have an SSH agent available.

Continue reading “Set up KeePass with KeeAgent on Linux”

TDD Kata 10 – Anagrams

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 10 – Anagrams”

TDD Kata 09 – Print Diamond

This is my weekly Kata post. Read the first one to learn what it is all about.

Continue reading “TDD Kata 09 – Print Diamond”