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

Refactoring to PHP 7
Jan 31, 2017 @ 10:52:42

On thePHP.cc blog today there's a new post sharing some helpful hints related to refactoring your application to PHP 7 written up by a friend of the group, Tim Bezhashvyly.

Recently I have migrated a relatively large codebase from PHP 5.6 to PHP 7 and would like to share some of my learnings. To get the most out of this article, you should be familiar with scalar type declarations (and return type declarations). To learn about these and other features of PHP 7, I recommend the "PHP 7 Explained" eBook.

He makes the recommendation of a bold first step: enabling the strict typing on every file in your application to enforce the typing of all values. Next he recommends running your current test suite to see where the failures are. Changes are pretty high that you'll find issues with type switching and magic method handling. He suggests a method for migrating your code effectively to PHP 7: a test-driven migration. This focus works fine if your coverage is good but unless you're exercising all parts of your codebase things will unfortunately be missed.

He also points out some other changes you can make with this update including the removal of some PHPDoc annotations (you'll know the type for sure now) and modifications that may need to be made to current mock objects in your tests. There's a few other smaller things he recommends looking out for as well including the use of the "silencer" operator and exception changes.

tagged: refactoring php7 testdriven unittest testing migration strict types

Link: https://thephp.cc/news/2017/01/refactoring-to-php7

Laravel News:
Review of Test-Driven Laravel
Dec 02, 2016 @ 10:44:56

The Laravel News site has posted a review of "Test Driven Laravel", a course from Adam Wathan walking you through the creation of a Laravel-based application from a completely Test-Driven Development approach.

I’ve been programming for close to a decade, and I’m constantly trying to find ways so to improve my skill set to be able to keep up with the ever-changing landscape of technology. The one area I’ve always wanted to improve in has been testing. [...] When Adam announced at Laracon that he was creating a course on building real-world applications with TDD, I was really excited to see where it would go.

Over the past couple of months, I’ve been lucky enough to preview the course as the lessons get finished. It’s been really helpful because I’ve been working on a large, complex application, and I’ve been able to apply the things I’m learning immediately in my day-to-day work.

The review goes on to talk about some of the things they've learned already from the course including test coverage for parts of the app that work with 3rd party services. They link to this talk from Adam as an example of some of the concepts taught in the course and, of course, a link where you can purchase your own Early Access pass and start learning.

tagged: review earlyaccess testdriven laravel tdd adamwathan testing course

Link: https://laravel-news.com/review-of-test-driven-laravel

Jani Hartikainen:
5 step method to make test-driven development and unit testing easy
Oct 11, 2016 @ 10:23:08

While his examples may be in Javascript, Jani Hartikainen has posted a guide that can help any developer get started with TDD - Test Driven Development - in their new or legacy applications.

What’s the hardest thing in test-driven development or unit testing in general? Writing tests! The syntax or the tools aren’t the problem – you can learn those enough to get started in 15 minutes. The problem is how to take some vague idea in your head about what you want to do, and turn it into something that verifies some function works… before you even wrote the damn thing!

People tell you to use TDD. But how can you possibly write a test for something that doesn’t exist? I don’t even know what the function is going to do yet – or if I actually want two functions instead of one – and instead you want me to think of a test for it? Are you crazy?

How do all those people who tell you to use TDD do it? That’s the thing – test-driven development requires thinking of your code in a different way. And nobody ever tells you how to do that. Until now.

He then breaks down the process of how to turn a "vague idea" into something that can be effectively tested, noting that this change in thought process can sometimes be difficult. He then breaks it down into a set of five steps:

  • Step 1: Decide the inputs and outputs
  • Step 2: Choose function signature
  • Step 3: Decide on one tiny aspect of the functionality
  • Step 4: Implement test
  • Step 5: Implement code

While the above may seem familiar to anyone that's read about TDD before, it's interesting to see how he explains each item with an emphasis on behavior not just the code required. He ends the post with a few more smaller suggestions to help you get on the road to TDD with the same emphasis on behavior rather than functionality.

tagged: tdd testdriven development steps easy unittest behavior

Link: http://codeutopia.net/blog/2016/10/10/5-step-method-to-make-test-driven-development-and-unit-testing-easy/

Test Driven API Development using Laravel, Dingo and JWT with Documentation
Jun 20, 2016 @ 10:15:04

On the DotDev.co site a tutorial has been posted showing the full set up of an API using Laravel, Dingo and JWT tokens while following test-driven development principles along the way.

As the complexity of API’s increase, improving the ways we create them becomes a necessity. Let’s take a journey exploring an efficient way of building well-tested API’s that are easy to develop and maintain by wiring up several different open-source packages.

In this tutorial, we will build a very simple API for fruits that lists all the fruits, shows one fruit, creates a fruit, and finally deletes a fruit. The API will allow anyone to list and show fruits but we will use JWT Authentication to protect creating and deleting operations so only the registered users can use them.

The tutorial starts by helping you get the TDD environment set up for the application and the required libraries installed. From there they install and configure Dingo and look at the role that transformers play in the API output. With a basic API in place the JWT tokens are integrated and another package is used to generate simple, clean API documentation. Full links to other packages, screenshots of the expected output and all the code you'll need is included.

tagged: testdriven development tdd laravel api dingo jwt token tutorial

Link: https://dotdev.co/test-driven-api-development-using-laravel-dingo-and-jwt-with-documentation-ae4014260148#.tccatytip

SitePoint PHP Blog:
Building a SparkPost Client: TDD with PhpUnit and Mockery
May 04, 2016 @ 12:26:32

On the SitePoint PHP blog they've continued their series covering the SparkPost mail delivery service and integrating it in to your application. In this latest part of the series author Christopher Pitt starts looking at the SparkPost API and uses it as a chance to practice some TDD (Test Driven Development) skills.

In a previous post, we looked at SparkPost (as an alternative to Mandrill), and explored a bit of the official PHP client. The official client handles a decent amount of work, but I got to thinking about what it would take to build a new client.

The more I thought about it, the more it made sense. I could learn about the SparkPost API, and practice Test Driven Development at the same time. So, in this post we’ll look to do just that!

He uses a few different libraries to explore the API and its endpoints: Guzzle for the HTTP requests and the Mockery+PHPUnit combination for the testing. He includes the setup and configuration for the testing environment and some sample tests for making sure things are connected. He then integrates Mockery into the testing, using it to mock the Guzzle requests and still have the tests pass even without the actual connection. He then works through several other tests and finishes the post with a mention of building coverage results for the "Client" class.

tagged: sparkpost client tutorial series tdd testdriven development mockery phpunit guzzle api

Link: http://www.sitepoint.com/building-a-sparkpost-client-tdd-with-phpunit-and-mockery/

SitePoint PHP Blog:
Clean Code Architecture and Test Driven Development in PHP
Feb 09, 2016 @ 09:13:28

The SitePoint PHP blog has an article posted by Vitalij Mik showing you how to merge the concepts of "clean code architecture" and test-driven development to make solid, maintainable code. The ideals of the "clean code architecture" were first proposed by Robert C. Martin in this post on the 8thlight blog.

The idea was to create an architecture which is independent of any external agency. Your business logic should not be coupled to a framework, a database, or to the web itself. [...] Frameworks will continue to change and evolve. With composer, it is easy to install and replace packages, but it is also easy to abandon a package (composer even has the option to mark a package as abandoned), so it is easy to make “the wrong choice”.

In this tutorial, I will show you how we can implement the Clean Code Architecture in PHP, in order to be in control of our own logic, without being dependent on external providers, but while still using them. We will create a simple guestbook application.

He starts with a first test, evaluating that a list of entries for the guestbook is empty. The code shows the "fakes" for the different object types Uncle Bob recommended in his article and how it fails because none of them exist yet. He extends this with a "can see entries" test and then starts in on the "use case" class to start making the test pass. He updates the case to pull in entries from the repository, another external dependency created later in the tutorial. He then goes through creating the classes for the "fakes" in the test and refactoring the test based on some of his changes during their development. In the remainder of the post he talks about the independence of the current setup and how to add in pagination functionality on the entries objects.

tagged: cleancode architecture testdriven development tdd tutorial entity repository decouple

Link: http://www.sitepoint.com/clean-code-architecture-and-test-driven-development-in-php/

Test-Driven Development With Laravel & Doctrine
Feb 02, 2016 @ 13:39:47

On the TutsPlus.com site they've posted a new tutorial showing you how to do test-driven development with Laravel and Doctrine, making use of Doctrine's own testing functionality inside of a Laravel application for PHPUnit based unit testing.

As a PHP developer, you may use the Test-Driven Development (TDD) technique to develop your software by writing tests. Typically, TDD will divide each task of the development into individual units. A test is then written to ensure that the unit behaves as expected. [...] TDD verifies that the code does what you expect it to do. If something goes wrong, there are only a few lines of code to recheck. Mistakes are easy to find and fix. In TDD, the test focuses on the behavior, not the implementation. TDD provides proven code that has been tested, designed, and coded.

[...] PHPUnit is the de-facto standard for unit testing PHP. It’s essentially a framework for writing tests and providing the tools that you will need to run tests and analyze the results. PHPUnit derives its structure and functionality from Kent Beck’s SUnit.

He briefly talks about some of the assertions that PHPUnit has to offer before getting into the support that Laravel includes and how to configure it so Doctrine can work with your database. He then talks about Doctrine, briefly introducing the popular database abstraction tool and how to integrate it with a Laravel application. From there he starts in on the tests themselves, showing code that uses fixture data to create several tests for Post and Comment data.

tagged: testdriven development tdd laravel doctrine fixture tutorial

Link: http://code.tutsplus.com/tutorials/test-driven-development-with-laravel-doctrine--cms-25563

Adam Wathan:
Test Driven Laravel from Scratch
Jan 15, 2016 @ 09:22:51

Adam Wathan recently presented a talk at ZendCon 2015 about test-driven Laravel development. As the talk wasn't recorded he put together a condensed version of it and has posted it to his site.

This talk covers some common obstacles people run into when trying to get started with test driving real applications, including: what’s the very first test I should write, [...] how do I test routes, [and] should I start at the unit level or the acceptance level?

The video is embedded in the post but you can also watch it separately over on Vimeo. Adam also has some other videos for those interested in things like social logins with Laravel 5, polymorphism in the database layer and refactoring loops and conditionals.

tagged: video testdriven development laravel zendcon15 screencast

Link: http://adamwathan.me/2016/01/11/test-driven-laravel-from-scratch/

Allan MacGregor:
TDD is not Dead
Sep 08, 2015 @ 10:30:01

Allan MacGregor has a post to his site with some of his thoughts on why TDD isn't dead and is still a viable option to help reduce bugs and improve software quality.

So, where does this whole TDD is DEAD thing came from? Well, it all started with let's say a provocative talk and follow up blog post by David Heinemeier Hansson (@DHH) where he expressed his frustration with testing and put into question the value of TDD. [...] TDD is not dead, not really. And it won't really ever be dead, it will change or be replaced with something better; in fact it already has, and in my Magento Extension Test Driven Development book we focus on Behavior Driven Development, an approach that emerged from the original TDD methodology.

He goes through each of the points that DHH mentions in his post and offers some of his own thoughts on the topic:

  • Developers make you feel like your is dirty if you don't practice TDD
  • Driving design from unit tests is not a good idea
  • TDD notion of "fast tests" is shortsighted
  • 100% coverage is silly.
  • TDD created test-induced design damage.

He ends with the most common misconception about testing in general too: i"t's too much work/it will make my development slower." He also looks at some of these kinds of comments specifically targeted at Magento 2.

tagged: tdd testdriven development dead opinion dhh

Link: http://coderoncode.com/testing/magento/2015/09/03/tdd-is-not-dead.html

SitePoint PHP Blog:
Building a Custom Twig Filter the TDD Way
Jun 08, 2015 @ 13:40:18

The SitePoint PHP blog has a new tutorial posted showing you how to create your own Twig template following a TDD (test-driven development) mentality.

Twig is a powerful, yet easy to master template engine. It is also my personal favorite as all my web development is based on either Symfony or Silex. Apart from its core syntax ({{ ... }} and {% ... %}), Twig has built-in support for various filters. A filter is like a “converter”. It receives certain original data (a string, a number, a date, etc) and by applying a conversion, outputs the data in a new form (as a string, a number, a date, etc).

He starts with a brief introduction to what filters in Twig are and some simple ways to use them. From there he gets into building a custom filter, starting with the tests first (hence the test-driven design). He walks you through the creation of a filter that turns times into relative strings, like "Just now" or "Within an hour". He shows how to make the extension classes and integrate it into a Symfony application.

tagged: twig filter tutorial custom timediff extension tdd testdriven development

Link: http://www.sitepoint.com/building-custom-twig-filter-tdd-way/