Posts tagged with testing

Commands, events, global functions and testing

Tony Messias on the madewithlove-blog:

The other day I was listening to the FullStackRadio episode 34 which is about dealing with dependencies in Active Record models. This is a very interesting topic and they suggest a few solutions for it. I liked the suggestions and I tried to implement it differently (first try and second try).

After that, I decided to talk to my colleagues about the design implementations. And they asked “why not implementing it as a command?”. At first sight I was a bit reluctant, because I’m starting to think applications are getting more complex then it really needs. Then I decided to implement it and the end result was the best one to us. Let’s discuss it a bit.

http://blog.madewithlove.be/post/commands-events-global-functions-and-testing/

Read more

Monkey patching in PHP with Patchwork

Patchwork is a PHP library that allows you to change user defined functions at runtime. This can be incredibly handy when testing. I'm by no means a Ruby expert but I heard that in the Ruby world they do this kind of stuff all the time during testing.

Here's a simple example of what Patchwork can do:

function myCounter($array)
{
    return count($array);
}

myCounter([1, 2]); // returns '2';

//now let's change the function
Patchwork\redefine('myCounter', function($array)
{
    return count($array) ? 'a lot of items' : 'empty';
});

myCounter([1, 2]); // returns 'a lot of items";

Read the implementation docs to learn how this works behind the scenes.

Read more

Join thousands of developers

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

Automatically test the quality of your code on commit

A few days ago Toon Verwerft gave an uncon talk at PHP Benelux Conference about a new code quality checking tool he has been developing. It's called GrumPHP. It can automatically perform various code quality checks when you try to commit some code.

Sick and tired of defending code quality over and over again? GrumPHP will do it for you! This composer plugin will register some git hooks in your package repository. When somebody commits changes, GrumPHP will run some tests on the committed code. If the tests fail, you won't be able to commit your changes. This handy tool will not only improve your codebase, it will also teach your co-workers to write better code following the best practices you've determined as a team.
https://github.com/phpro/grumphp

The slides of Toon's talk can be found on speakerdeck.

Read more

Writing your own test doubles

Adam Wathan posted another excellent article on testing on his blog. This time he talks about creating test doubles. Adam demonstrates that creating your own fake can result in a much more readable test than using mocks or spies.

You'll learn to create an InMemoryMailer like this:

public function test_new_users_are_sent_a_welcome_email()
{
    $mailer = new InMemoryMailer;
    Mail::swap($mailer);

    $this->post('register', [
        'name' => 'John Doe',
        'email' => 'john@example.com',
        'password' => 'secret',
    ]);

    $this->assertTrue($mailer->hasMessageFor('john@example.com'));
    $this->assertTrue($mailer->hasMessageWithSubject('Welcome to my app!'));
}

That seems like a pretty readable test to me. Read (or view) the whole tutorial here: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/

Read more

A pjax middleware for Laravel 5

by Freek Van der Herten – 1 minute read

A few days ago Jeffrey Way published an interesting lesson on how to integrate pjax with Laravel. Pjax is jquery plugin that leverages ajax to speed up the loading time of webpages. It works by only fetching specific html fragments from the server, and client-side updating only certain parts of the…

Read more

Extract till your drop

Watch a very instructive live coding session with Matthias Verraes.

Under the pressure of deadlines and endless change requests, under the weight of years of legacy, code becomes unmaintainable. With the right tools, techniques, and mindset, any codebase can be brought under test, and be refactored towards a better architecture. Let's skip the theory and dive straight into the spaghetti code. In a live coding session, I will demonstrate how you can start refactoring your way out of a mess today.

Read more

PHPUnit 5.0 will only support PHP5.6 and above

Active support for PHP 5.4 ended on September 14, 2014 and active support for PHP 5.5 will end on June 20, 2015. The active support for PHP 5.3 already ended on July 11, 2013. By the time PHPUnit 4.8 will be released, the only actively supported version of PHP will be PHP 5.6.

The next version after PHPUnit 4.8 will not support PHP 5.3, PHP 5.4, and PHP 5.5 anymore. As PHPUnit follows Semantic Versioning the major version number must be incremented when the minimum required version of PHP is increased.

https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-4.7.0#phpunit-50

Nice!

Read more

PHPCI: a CI tool specifically designed for PHP

PHPCI is a free and open source continuous integration tool specifically designed for PHP. Built with simplicity in mind and featuring integrations with all of your favourite testing tools, we've created the very best platform for testing your PHP projects.
https://www.phptesting.org/

Over at Sitepoint Peter Nijssen wrote a good overview of the offered functionality.

I've been playing around with the self-hosted version. It comes with a bunch of plugins that are installed by default.

I can recommended it if you, like me, are starting out with CI.

Read more