PestPHP `isUrl()` Expectation

PestPHP `isUrl()` Expectation
Photo by Ales Nesetril / Unsplash

I added another Expectation to PestPHP recently! I'd like to introduce the isUrl() Expectation. As you may have guessed, it's to check that a given value is a valid URL.

Behind the scenes, the code is super simple and makes use of the handy FILTER_VALIDATE_URL function!

Let's look at an example of how to use it:

<?php

it ('ensures social links are urls', function() {
    expect($user->twitter)->toBeUrl()
        ->and($user->instagram)->toBeUrl()
        ->and($user->linkedin)->toBeUrl();
}

Of course, because we're using the Expectation API, we can also check that values are not URLs:

<?php

it ('ensures user name is not a url', function() {
    expect($user->name)->not->toBeUrl();
}

This is a really fluent way of ensuring certain values are / are not URLs!

You can find the PR that added it on GitHub! It has already been released and was released as v2.22 on October 10th 2023.

🧪