Posts tagged with testing

Handcrafting mocks

by Freek Van der Herten – 8 minute read

In an application I was working on I wanted to implement automated tweets. Of course, this logic should also be tested. In this blogpost I'd like to show you how you can easily handcraft your own mocks. Setting things up Let's first take a look at how you can tweet something in PHP. We're going to…

Read more

ngrok, lvh.me and nip.io: A Trilogy for Local Development and Testing

nickjanetakis.com

Nick Janetakis shares three tools that come in handy when developing locally.

When developing real world applications you tend to run into certain road blocks such as:

  • How do I test webhooks locally?
  • How can I show a demo of my site to a client?
  • How can I develop a web app that uses subdomains on localhost?
  • How can I test Let’s Encrypt without a domain name? In this article, we’re going to cover all 4 use cases using 3 free tools and services.

Read more [nickjanetakis.com]

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.

Replacing a built-in PHP function

akrabat.com

Rob Allen performs a cool test.

Recently I needed to test part of Slim that uses the built-in PHP functions header() and headers_sent(). To do this, I took advantage of PHP’s namespace resolution rules where it will find a function within the same namespace first before finding one with the same name in the global namespace.

Read more [akrabat.com]

Some Laravel package testing tips

twitter.com

Read more [twitter.com]

The Integration Operation Segregation Principle

frederickvanbrabant.com

In a new post on his blog Senior CEO Frederick Vanbrabant explains the Integration Operation Segregation Principle, which is programmerspeak for splitting your code into nice little testable bits.

As you can see the Integration Operation Segregation Principle is just a long and complicated term to describe something very simple. This all might seem like a lot of work, but it’s worth it. Your code and especially your tests will thank you later

Read more [frederickvanbrabant.com]

The Five Types of Test Doubles & How to Create Them in PHPUnit

jmauerhan.wordpress.com

In a new post on her blog Jessica Mauerhan has some good examples on the various types of test doubles.

Did you know that a Mock is only one type of a test double? Most of us use the word “mock” to mean any kind of test double, but there’s actually five different types. It really can help you understand what you’re trying to accomplish with your test if you know a little bit more what you’re doing with your test doubles, so this article will explain the kinds of test doubles, when you use them, how you use them and why.

Read more [jmauerhan.wordpress.com]

Using Travis-CI for your Laravel Nova packages

marcelpociot.de

Now that Laravel Nova can be installed via composer, we can run tests for Nova packages and projects on Travis CI. My friend Marcel Pociot explains how.

This is great news, as this does not only simplify updating Laravel Nova, but it also allows Nova tools/package developers to add continuous integration to their projects! But there is still a problem: we do not want to provide our Laravel Nova credentials in our open source repository. But how can we solve this?

Read more [marcelpociot.de]

Test Contra-variance

blog.cleancoder.com

In an older but still relevant article on the Clean Coder Blog, Uncle Bob argues that the structure of your tests should not necessarly reflect the structure of your code.

The structure of the tests must not reflect the structure of the production code, because that much coupling makes the system fragile and obstructs refactoring. Rather, the structure of the tests must be independently designed so as to minimize the coupling to the production code.

Read more [blog.cleancoder.com]

Verifying Laravel Version Compatibility

blog.tjmiller.me

TJ Miller shares a cool Travis config to tests against all versions of Laravel.

As Laravel 5.7 is now out, I needed to add support to both the package’s composer configuration and verification in CI. Previously, I only needed to verify against 5.5 and 5.6 I thought that having --prefer-lowest in the matrix would add the coverage I needed. Now that I am supporting three versions I felt that I needed a more specific way of verifying compatibility with different versions of Laravel.

Read more [blog.tjmiller.me]

Creating custom @requires annotations for PHPUnit

mattstauffer.com

In an older but still relevant blogpost Matt Stauffer explains how you can extend PHPUnit's native @requires annotation. It's pretty handy if you want to only run certain tests in certain environments.

I was primarily interested in learning—how do PHPUnit annotations work? What does it look like to extend a pre-existing annotation? How do you not just check for the annotation, but also check its value? I'll show you what I found and then you can run willy-nilly with your own naming schemes.

Read more [mattstauffer.com]

Visual Regression Testing with Laravel

marcelpociot.de

Marcel Pociot, the mind behind BotMan, has released a cool package to create visual diff in your PHPUnit tests. Under the hood it uses our Browsershot package.

I'm not sure how you feel, but I consider myself a backend developer. Sure - I know my way around Vue.JS and really enjoy working with it, but writing CSS has never been my strong point. At one of our companies recent projects, we are working together with another development team, which is mostly taking care of frontend development. So we build controllers, repositories, services, etc. and hand it over to some basic views. They handle the rest. We introduced continuous integration to them and showed them our usual workflow, when I thought that it would be excellent to also have some kind of visual CI for frontend changes.

Read more [marcelpociot.de]

Tidy up your tests with class-based model factories

John Bonaccorsi, a developer from Tighten, wrote some good ways of structuring model factories in a Laravel app.

Thanks to class-based model factories, our test setup went from being a bloated mess to simple and succinct. The optional fluent methods give us flexibility and make it obvious when we are intentionally changing our world. Should you read this blog post and immediately go and update all of your model factories to be class-based instead? Of course not! But if you begin to notice your tests feeling top heavy, class-based model factories may be the tool to reach for.

https://tighten.co/blog/tidy-up-your-tests-with-class-based-model-factories

Read more

Separate Interactive Test Suites

If you find yourself having a bunch of slow tests that don't need to execute every time you run the tests, take a look at PHPUnit's defaultTestSuite setting. TJ Miller explains it in a blog post he wrote last year.

To avoid running the interactive test suite with the rest of my tests, manually or via a CI job, I had to explicitly include all the other suites using phpunit --testsuite Api,Feature,Unit. This felt a bit grim and I would rather exclude just that one suite. So I did some digging and found the defaultTestSuite configuration for phpunit.

https://medium.com/@sixlive/separate-interactive-test-suites-f6fd59316ec2

Read more

Regaining trust in your test suite with Docker

Frank de Jonge, creator and maintainer of Flysystem, recently started using Docker to test the FTP driver. On his blog he explains the why and how.

For Flysystem, an open source PHP package to deal with filesystems, I needed a way to test FTP (barf) interactions. FTP servers are notoriously bad at abiding by the spec. ... For Flysystem's FTP(d) adapter an integration test, using an actual FTP server, brought back the level of confidence it needed.

https://blog.frankdejonge.nl/regaining-trust-in-your-tests-with-docker/

Read more

Automatic visual diffing with Puppeteer

Monica Dinculescu, an engineer at Google, shares on her blog how you can use Puppeteer, Mocha and Pixelmatch to create automated visual tests of a webpage.

I did a little song-and-dance that sets up Puppeteer* , takes screenshots of your app (like, all the routes you care about), and then compares them to the “golden” ones. If they match, your test passes! Yes, it only works on Chrome. No, it’s not actually unit testing. Yes, it’s literally just counting pixels but you know what? It counts them in both a wide and a narrow viewport size and any testing is better than no testing at all; fight me.

https://meowni.ca/posts/2017-puppeteer-tests/

Read more

How to send a "trial expiring soon" mail in Laravel Spark

by Freek Van der Herten – 6 minute read

I'm currently building a webapp named Oh Dear: an easy to use and beautiful website monitor. It has recently gone into it's beta phase. At the moment of writing everybody can register a new account. After you've registered you'll start your trial period of 10 days. Oh Dear is built on top Laravel…

Read more