Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Laravel News:
Tips to Speed up Your Phpunit Tests
Jan 11, 2019 @ 16:41:23

On the Laravel News site Tim MacDonald has written up a post sharing some tips on how you can speed up your PHPUnit tests, making them easier to run and more useful during the development process.

Having a fast test suite can be just as important as having a fast application. As a developer, getting feedback quickly about the state of your code allows for a much quicker development turnaround. Here we are going to run through some tips you can implement today to make your tests run faster.

The example test suites have been made intentionally slow to simulate a broader set of tests and also to emphasize the improvements possible. Your real-world mileage may vary.

He makes recommendations around the use of ParaTest to run the tests in parallel, re-running only failed tests, grouping slow tests, lowering the password hash "rounds" count and disabling XDebug. Each item in the list includes instructions on what changes need to be made and screenshots of the results of the change.

tagged: unittest phpunit tips speed performance improvement

Link: https://laravel-news.com/tips-to-speed-up-phpunit-tests

Jessica Mauerhan:
The Five Types of Test Doubles & How to Create Them in PHPUnit
Oct 11, 2018 @ 15:53:56

Jessica Mauerhan has a tutorial posted to her site that covers the five types of test doubles in PHPUnit and how to use them in your tests. Test "doubles" - the most common one being a mock - are useful for simulating resources or other objects required to test units of code without external interactions.

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.

She starts the post with a brief definition of the term "test double" and then covers each of the five with descriptions and example code:

  • Dummy
  • Stub
  • Spy
  • Mock
  • Fake

While some of the functionality is similar between the types, she defines their differences and the cases where each would be most useful.

tagged: tutorial test doubles unittest phpunit types top5

Link: https://jmauerhan.wordpress.com/2018/10/04/the-5-types-of-test-doubles-and-how-to-create-them-in-phpunit/

TJ Miller:
Separate Interactive Test Suites
Mar 26, 2018 @ 17:56:24

In a post to his Medium.com site TJ Miller has a quick post for the PHPUnit users out there showing how to isolate your tests and prevent them from interacting by splitting them into different test suites.

On a recent Full Stack Radio episode Adam Wathan and Taylor Otwell were talking about testing Laravel applications. During the episode, they spoke about isolating interactive integration tests from your normal testing e.g. payment gateways, third-party integrations.

TJ shares an example of a project he's working on where this is useful: avoiding interactions with an HTTP API every time the tests run. He then shows how, with a single PHPUnit configuration, you can split up the tests by name and directory to prevent them all from executing every time. Then the "testsuite" option can be used to isolate the execution from the command-line. An example of the XML configuration is also included in the post.

tagged: tutorial phpunit separate test suite isolation configuration

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

Ivan Enderlin:
How Automattic (WordPress.com & co.) partly moved away from PHPUnit to atoum?
Mar 22, 2018 @ 17:08:37

In a post to his blog Ivan Enderlin talks about a move that his team at Automattic (the company behind WordPress) made away from using PHPUnit for their application testing over to atoum and some of the reasoning behind it.

Few months ago at tagged: wordpress testing unittest atoum phpunit comparison migration

Link: https://mnt.io/2018/02/26/how-automattic-partly-moved-away-from-phpunit-to-atoum/

Marc Baker:
Discharging Static #1
Mar 14, 2018 @ 19:49:47

On his blog Marc Baker about static calls and the trouble they bring in your applications. It's a continuation of ideas that Kore Nordmann shared previously on his site.

It’s been seven years since Kore Nordmann first published “static considered harmful” on his blog, explaining the problems with using static method calls in classes, and the difficulties that they cause when trying to test that class. Seven years on, and those difficulties are still the same, and there is still new code being written using static calls despite that knowledge; but it’s often a more severe problem in legacy code with little or no unit tests.a

So why exactly are static calls so bad? If you’ve read Kore’s article, then you probably have a good idea already; but what that article doesn’t cover is approaches that we can use to make the code testable.

He covers the main issue static methods have when it comes to testing: they introduce coupling by hard-coding a dependency into your code. He talks about the static testing functionality that various PHP testing tools provided: PHPUnit, Phake and Mockery. He then focuses on a newer tool that he's discovered to help make the testing simpler: AspectMock. He gives an example of it in use and some examples of tests using anonymous classes to make it easier to create tests on the fly.

tagged: static testing phpunit example harmful tutorial aspectmock

Link: https://markbakeruk.net/2018/03/13/discharging-static-1/

Laravel News:
Data-driven testing with PHPUnit
Feb 27, 2018 @ 17:54:24

The Laravel News site has a tutorial posted that approaches a common development task - writing tests - from a data-driven approach rather than a strictly functional one.

Testing your code is an essential part of the development process, but sometimes it could also be expensive when you try to emulate many uses cases based on a set of different input data.

In many cases, you could end up with a massive directory of tests repeating the same block of code over and over for each possible user interaction.

They start with an example of a feature to test (Markdown parsing) and how traditional testing would have methods for each of the transformations. With a data-driven approach they turn the tests around and make use of data providers to set up the initial string, transformer to use and the correct result. Code is included showing an example of this kind of testing for the same Markdown parsing example and what a failure would look like in a dataset with multiple items.

tagged: tutorial provider phpunit testing data datadriven markdown

Link: https://laravel-news.com/data-driven-testing-phpunit

Matthias Noback:
Local and remote code coverage for Behat
Feb 12, 2018 @ 15:45:43

In a new post to his site Matthias Noback has a post showing you how to, when using the Behat functional testing framework, to create local and remote code coverage metrics.

PHPUnit has built-in several options for generating code coverage data and reports. Behat doesn't. As Konstantin Kudryashov (@everzet) points out in an issue asking for code coverage options in Behat: "Code coverage is controversial idea and code coverage for StoryBDD framework is just nonsense. If you're doing code testing with StoryBDD - you're doing it wrong."

He starts off by talking about code coverage and why you might want it for the tests created through Behat. Code coverage metrics are usually associated with unit tests but having those numbers for the functional tests can be helpful too. He then covers the two pieces of the puzzle needed to gather the coverage numbers: an extension for the local coverage and one for the remote coverage. He shows how to get both of these installed, configured and used in the code to gather the results. Finally he shows how to use the phpcov tool to merge these results with the PHPUnit results to get a better overall view of coverage numbers.

tagged: local remote behat codecoverage metric phpunit tutorial testing

Link: https://matthiasnoback.nl/2018/02/behat-local-and-remote-code-coverage/

Anna Filina:
Testing Legacy PHP Scripts
Jan 30, 2018 @ 17:56:23

Anna Filina has a quick post to her site with some recommendations around testing legacy PHP scripts giving an example of a challenge to test a controller in isolation from the rest of the application.

I gave myself a challenge: to test a legacy "controller" in isolation, yet with minimal impact on the original code.

She starts with the example code she'll be testing and then works through the steps to effectively test it:

  • isolating it from the other functionality in the application
  • mocking a statically called method
  • requiring necessary files
  • executing the controller under test

The post ends with the test class she created showing how to evaluate the result of a call with one invoice in the billing system. She makes one comment at the end to answer the question "why not just refactor" but points out that, especially in larger legacy applications, that's just not always an option.

tagged: testing legacy script tutorial isolation mock unittest phpunit

Link: https://afilina.com/testing-legacy-php-scripts

Martin Hujer:
Have you tried Composer Scripts? You may not need Phing.
Jan 15, 2018 @ 17:14:13

In a new post to his site Martin Hujer shows you that, with the help of the "script" ability in Composer, you may not need a build tool like Phing. The scripts functionality allows you to execute custom scripts as a part of the Composer workflow, enhancing the management functionality it already provides.

Phing is a great tool (I'm using it as well), but in this article, I want to show you that some projects may not need it. Composer contains a powerful feature called "Scripts", which can be used to create a simple build script.

In his example, he shows how to integrate a run of the PHP_CodeSniffer quality assurance tool as a part of your Composer configuration. This makes it possible to run a command like composer run-script phpcs and automatically run the checks. He then builds on this simple example and creates a more complex build script that still runs PHP_CodeSniffer but also executes PHPUnit tests. He post also shows how to run Composer in a command and how to document each command. There are also a few handy tips included around running Composer on Windows, listing the current scripts and executing custom callbacks to code.

tagged: composer build scripts tutorial example phing phpcodesniffer phpunit

Link: https://blog.martinhujer.cz/have-you-tried-composer-scripts/

Facile.it Engineering Blog:
How to gradually upgrade toward PHPUnit 6 with namespaced classes
Sep 13, 2017 @ 16:56:03

On the Facile.it Engineering blog there's a recent post sharing some tips on how to gradually upgrade your PHPUnit tests to work with version 6 of the popular PHP unit testing tool.

In the latest months I wrote multiple times, in different projects, code migrating PHPUnit toward major version 6. This upgrade is harder than the previous one, since in this version it was introduced a big breaking change: all classes got (finally!) namespaced.

This means that any usage of those classes in your project needs to be updated. [...] In this article I will explain which steps I applied during those migrations, highlighting the most frequent hiccups.

He then start with "the easy one" to take care of the refactor: updating tests to replace the "PHPUnit_*" classes with the namespaced versions. With those out of the way, he talks about "the bumpy one" to handle: modifying test listeners to work with the new PHPUnit structure. Once these are taken care of you can then make the move up to PHPUnit 6 and PHP 7 (if you're not there already) full time.

tagged: phpunit upgrade version unittest phpunit6 php7 tutorial

Link: https://engineering.facile.it/blog/eng/phpunit-upgrade-namespace/


Trending Topics: