German Visitors: Sie sehen die englischsprachigen Artikel meines Blogs. Die meisten Blog-Beiträge sind auch auf deutsch erschienen, bitte zu Deutsch wechseln, um die deutschsprachigen Beiträge zu sehen.
Did you ever write unit tests for code that dealt with date and time? Since time itself is out of your control, you might have used one of these approaches:
- Test doubles, e.g. for the DateTime class in PHP. To make this work, DateTime or a factory thereof must be passed to the subject under test via dependency injection. My most popular blog post of all time is still “How to mock time() in PHPUnit” about mocking core functions like date() and time() if necessary (a hacky workaround, mind you)
- Use derived values of the current time in fixtures and assertions, probably with some margin, e.g.
assertEquals(\strtotime(“+1 day”), $cache->expiresAt(), “1”)
$cache->expiresAt()is the value we test and “1” is the allowed margin.
The latter is not very stable; it is likely that you run into edge cases where the tests are not deterministic. The former does not have this problem, but can result in a complicated setup of the test double.
Luckily, there is a third way, namely using a custom Clock (or Calendar) object that provides the current time and can easily be replaced with an (also custom) test double.
The blog has become silent, still I’d like to begin the year 2019 with a little retrospective, just like in 2017, 2016 and 2015.
Since there were no new posts in 2018 and I turned off the tracker, there are no new blog statistics this time, instead I’ll start with an overview where you find posts by me elsewhere.
If you are interested in Software Testing and/or Magento development, the most interesting event of the year is approaching: MageTestFest in Amerfoort (NL)!
- Nov 15: Workshop PHPUnit (Sebastian Bergmann)
- Nov 16: Workshop DDD (Mathias Verraes)
- Nov 17: Conference Day (Agenda)
- Nov 18: Magento Contribution Day (Hackathon)
Magento architect Anton Kril asked about opinions on XML based tests:
#magento twitter, what are your thoughts about writing tests in XML?
— Anton Kril (@AntonKril) 20. Juni 2017
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”
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
$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”
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/