Entries with the tag “phpunit”

These entries have all been tagged with “phpunit”.

The Importance of ToDo Tests

Mar 22, 2021 php phpunit testing

I’m not a huge fan of todo comments in code because I think they’re mainly forgotten. However, I don’t have the same opinion for PHPUnit tests. Let’s talk about why - and how to easily add them in your code.

Do Not Calculate Answers to Your Unit Tests

Feb 8, 2021 php phpunit testing

Since you’re a programmer, you’re always looking for ways to be a bit more efficient. Because of this, it can be easy to fall into the trap of being too programatic, too calculation-heavy in your unit tests. But, this isn’t a test then - its just another block of quite fallible code. Let’s talk about why and show what to do instead.

Finding Slow Tests in PHPUnit 9

Jan 4, 2021 php phpunit testing

When your unit test suite gets larger, it can take quite a long time. One of the many ways to speed this up is to hunt down and fix slow tests. Let’s use PHPUnit’s test listeners to do just that.

PHPUnit Code Coverage Can Help While Writing Tests

Dec 28, 2020 php phpunit testing

It’s great to run code coverage at the very end before you push your changes. This gives you some idea what’s tested and what’s not. But you don’t have to wait till the end; code coverage can help you all throughout writing your test suite, too.

Stop Using Assert Database Has in Laravel

Apr 24, 2020 laravel php phpunit testing

Please stop using assertDatabaseHas in Laravel. There are many reasons why this is bad, and there are better options available. Let’s find out why.

Test Coverage is Not as Good of a Metric as You Think

Jun 9, 2019 php phpunit testing

There are a lot of debates about trying to achieve test coverage of 100%. Some people swear you need to do this. Others say, get 80% or more… the rest doesn’t matter. Each side has strong arguments.

Two Gotchas in Laravel Unit Testing

May 20, 2019 laravel php phpunit

There’s a struggle to balance the easy-to-use Laravel helpers and functions with very verbose, complicated methods in unit tests. As I’ve been relying on Laravel’s way of doing testing more, I’ve ran into a couple of gotchas that I should share.

PHPUnit Runs Data Provider Before Setup

Dec 3, 2018 php phpunit testing

I started noticing a disturbing trend on one of my projects: developers were doing too much logic in the setup and data provider methods of their PHPUnit tests. However, before we could address this, a “limitation” popped up which helped them kick this habit.

PHPUnit Listener for Long Running Tests Update

Nov 15, 2017 php phpunit

I wrote instructions in 2011 to add a PHPUnit Listener to Watch for Long Running Tests which seem to be a bit out of date now. But, here’s a quick refresher:

Run A Single PHPUnit Test Only

Oct 11, 2017 php phpunit testing

Often times, I find myself targeting a single file with PHPUnit by specifying the file after the binary in the command line:

PHPUnit Stop On Failure

Aug 31, 2017 php phpunit

From time to time, I get thrown onto a project that has many, many errors in the unit tests. It can be hard to try to figure out what to fix first with so many errors on the screen. Sadly, I’ve had my scroll-back buffer filled many times.

AssertSame vs AssertEqual in PHPUnit

Aug 15, 2017 php phpunit testing

When you’re testing inside of your PHPUnit test methods, you will use many assert-based methods. Two that are seemingly very similar are assertSame() and assertEqual()

PHPUnit Test Helpers for Mockery and Non-Public Properties

Jul 12, 2017 php phpunit

I’ve written a few helpful methods and systems that help out my unit testing. There are many arguments for and against these tools (don’t test protected methods, don’t introduce global namespace methods, etc) - and those are discussions for another day - but perhaps these might help you out in the mean time.

Easily Test JSON Keys in Laravel API Response

Jun 27, 2017 laravel php phpunit

In my Laravel application, I have an end point that will retrieve a collection of Client models. I have many other unit tests that validate that my repository returns the proper clients when requested, that my client model is sound. My last test is a feature test checks that if I retrieve a list of clients from the end point there is proper pagination and client models exist. I don’t really need to test the exact values because I know this will work - from all my other tests.

Use Anonymous Classes to Test Traits

May 11, 2017 php phpunit testing

I’m guilty of creating stub-like classes in my tests to unit test traits, sometimes. So, you end up with a special class inside your unit test file, perhaps at the bottom, that is empty but only extends the trait or something like that. This is not a good idea, but it was my only way that I could figure out how to unit-test traits separately - especially if they were made of protected methods.

Fixing Laravel 5.4's Dependency on PHPUnit 5

Apr 14, 2017 laravel php phpunit testing

Normally, when I write unit tests, I don’t use Laravel’s facade and fakery methods. I do a lot of injection of services, but in one particular case, when working with the Queue system, I had to use the facade for faking the queue and asserting some jobs were pushed.

Composer-based Utility Installs vs Global Installs

May 11, 2015 composer php phpunit

I’m a huge fan of tools like PHPUnit. Or any other tool that will help my project carry on.

Should you unit test your dependency container?

I’ve been working with Pimple Dependency Injector lately, and I’ve come up with an interesting question.

Integrating PHP QA Tools with Phing

If you’re not familiar with having PHP QA Tools, go there right now! And then come back and read this.

Testing Error 500 Pages in Zend Framework

For the most part, ZF can capture any of your hard errors. It will generate an error 500. You’ve seen them, don’t lie… I’ve seen them way too many times. However, in my production application, I capture these with a specific controller. And because I love Unit Testing, I want to make sure that I test my implementation of my omg-this-is-broken setup.

PHPUnit Mock Objects: Only Mock What You Need

May 8, 2012 php phpunit testing

I was looking at mock objects in PHPUnit the other day and started to get confused. When I asked for a Mock object, the entire object was a mock. In all actuality, it wasn’t so much a Mock of the object but a complete shell of it.

How to Add PHPUnit to your Include Path (for autocompletion) in Eclipse PDT

Feb 7, 2012 eclipse-pdt phpunit

By default, PHPUnit is not part of the default installation of Eclipse PDT. (Zend Studio is another beast, however…)

Running PHPUnit on Remote System from Command Line

Jan 3, 2012 eclipse-pdt phpunit

If I need to run PHPUnit on a remote system against a code suite, I will write a simple shell script like the following to do it for me. (Bonus points, you can even include this as an External Tool in eclipse to do it right from your project).

Add PHPUnit Listeners to Watch for Long Running Tests

Dec 20, 2011 phpunit testing

One of the under-utilized features of PHPUnit probably is the listeners interface.

Testing protected and private attributes and methods using PHPUnit

Aug 16, 2011 phpunit testing

First, I just want to say up front that this is not a discussion of “is 100% test coverage necessary” or a discussion about testing private methods. This is simply how you may do it if you wanted to.

PHPUnit error: No release available for package

Jun 10, 2011 phpunit

One of the steps to install PHPUnit is to execute the following pear commands:

PHPUnit error with Zend_Session

May 31, 2011 phpunit zend-framework

Running a test, I ran into this error: