What happens to your design if you don’t use TDD?

In this week’s videos we are exploring TDD in PHP from an angle I’ve never seen covered before in other online tutorials.

To set the scene, we are adding File Upload to our EasyAdminBundle backend.

As this is a beginner friendly series, I am fully aware that adding tests – specifically PhpSpec – into the mix alongside all the other “stuff” we need to know to work effectively with Symfony can be a little overwhelming.

I said last week how I think testing is one of the most effective ways to improve yourself as a software developer. I stand by that.

However, there’s a hurdle with testing that’s seldom addressed:

How can I write code with tests if I don’t understand how to write the code without tests?

Ok, so to be absolutely clear here, I’m not talking about syntax. At this stage I am assuming you have a grasp of the fundamentals of PHP and are interested in expanding your knowledge into learning Symfony.

What I’m talking about here is how to write tests for everyday workflows inside your Symfony applications.

When starting with Symfony it can certainly feel overwhelming enough just to get a few forms up and running. Throw in saving your form data off to the database, and you might need a long lie down.

And yet now I’m saying hey, let’s learn how to write tests for all this jazz!

I’ll let you in on a little secret:

I was not planning on showing you any of this 🙂

When I started out writing up this series, I built out the prototype and was happy with the outcome.

When I started recording the videos I realised that I had bumped up the difficultly significantly about 50% of the way through the course. This was because of the inclusion of tests.

When coding up the prototype I had gone head down into “code mode”. I’d implemented a working, and tested upload system but the outcome would be really hard to explain without a lot of additional context.

At this point I had to make a decision. The outcome of that decision was to alter the course to show how I ended up with the test-driven outcome.

To get to this outcome it makes most sense to show how to write this code without tests.

In doing so, we will see one method of achieving out goal: a working Wallpaper file upload process.

However, back in the real world, doing this would fall quickly under the “technical debt” category.

The idea of this site is that it’s the real deal – a hobby site maybe, but a real, working website.

As administrators we would like to be able to log in to our site, add new wallpapers, see stats and stuff like that. These sorts of things keep our momentum going to add new features and keep improving the site. I find this is the best way I continually improve myself as a software developer.

Taking this further, it makes sense that as-and-when our wallpaper site becomes popular (hey, we can dream!) we will likely want to be adding new wallpaper content on a regular basis.

This means our Wallpaper file upload form is going to be getting battered.

Taking this from a different angle, you likely have code on other projects that is equally frequently used.

This kind of code absolutely needs to work, or you will be sat in the pub and your phone will ring and your boss will be angry as his / her client is angry. And that spoils your otherwise enjoyable life experiences.

but it was working when I left the office…

When I encounter code like this, I immediately reach for the test suite. It’s the insurance policy that keeps my phone from ringing, and ensures my pub visits are interruption free.

But stepping back into our application, we have our Symfony site as a whole, and then EasyAdminBundle where we need to start adding in this additional functionality.

The chances are, if we’ve never used Symfony, or never used EasyAdminBundle, or both, then we have no experience even doing this “the easy way”. That is to say, we have no idea how to do this without even worrying about writing tests.

It’s a big ask to correctly figure out all the requirements in advance to the point where we can write a bunch of failing tests that when passing would work as a file upload system. At least, it is in my opinion.

So here’s what I do in real life: I cheat.

I don’t bother with tests.

I create a new branch in git, and then I hack around until I have a solution that generally works. This can take minutes, hours, or sometimes days and weeks.

Going back to our wallpaper file upload, I would code up a solution that allows me to add, edit, and delete files. It very likely wouldn’t be pretty, but that doesn’t bother me.

What this does is it gives me the knowledge of how – roughly – this whole thing should work.

From here, I can switch back to master, create another new branch, and have another bite at the cherry – only this time, I have a starting point from which I can write some tests.

I will usually refer back to the untested implementation a bunch of times, and being able to switch branches and play about with some working code is often a massive help in defining assumptions and crafting test assertions.

And so, that is what we are doing in this – and next – week’s videos.

First, we are exploring how to simply get EasyAdminBundle file upload working.

Then next week we will re-do the implementation but using PhpSpec to guide us.

Until then, thanks for reading, have a great weekend and happy coding.

Chris

Learn EasyAdminBundle by Doing.

This week saw three new videos added to the site.

In these videos we’re going to start with a gentle introduction to EasyAdminBundle.

If you haven’t yet seen EasyAdminBundle then I would recommend you check it out.

In a nutshell, it’s the easy to use admin panel that Symfony has been missing.

Personally I like working on logic. The fundamentally interesting business challenge that I’m trying to solve.

Admin Panels are a necessary evil. You gotta please dem stakeholders.

Making a decent, working admin panel is a going to sap a bunch of my time and if there’s one that does a “good enough” job then I’m going to use it.

As it happens EasyAdminBundle does more than “good enough”. It’s production quality, in my opinion.

Seriously, a massive thank you to Javier Eguiluz and the other 94 contributors.

We’re going to learn EasyAdminBundle by integrating it into our Wallpaper website system.

In the first of this week’s videos we start gently.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/easyadminbundle-setup-and-category-configuration

By the end of this video we will have gone from no admin area, to a working admin area that allows us to CRUD our Categories.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/easyadminbundle-wallpaper-setup-and-list-view

It’s easy, but it’s not a one-click job.

Beyond basic entities, we will need to add a little configuration.

We’re not going to be directly creating forms, controllers, or creating services.

Instead, we provide some configuration and let the bundle take care of the rest.

Our Wallpaper entity is pretty basic so far, but even now it has complexity. We have a relation to worry about. There’s also improvements to the provided output that I’d like to make.

We’re going to add in an image preview to our List view, and a few more additions and tweaks besides.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/easyadminbundle-starting-with-wallpaper-uploads

At the moment on our imaginary Wallpaper website we have no visitors.

We haven’t “launched” yet.

The reason this pretend concept matters is that we – the humble admin – will need to both seed, and keep our site updated with Wallpapers.

We looked at this already as part of the Wallpaper Setup Command videos, and the Doctrine Fixtures videos.

We might wish to outsource this task at some point in the future.

To keep the site fresh, we will want to add in new wallpapers over time.

To make this process as painless as possible we are going to add in a way to do this from our Admin Panel.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/easyadminbundle-starting-with-wallpaper-uploads

This means we need a way to handle uploads from our Admin panel.

There is already an integration guideline on the official docs for Integrating VichUploaderBundle to Upload Files and Images.

You can go with this. Or, if you’d like to learn a little more, we’re going to implement our own file upload system.

It’s extra work, and it’s all about the learning.

This is a part of the site that is going be very heavily used.

We’re going to want this to work, as otherwise it will become a potential source of bugs. And that means distracting ourselves from writing interesting code.

If I want to make sure the code that I write works, then  I’m reaching for my test suite.

My personal preference here is PhpSpec. And that’s what we’re going to use.

By the end of this video we will have you all set up for testing in a way that not only makes sure your code is well tested, but also the massive “hidden” additional benefit of giving you a much improved architecture.

If you’re not the most “pattern oriented” person then one of the biggest wins for me is that PhpSpec has led to the concept of emergent design.

By which I mean it reveals a good system design to me as I try and solve my problem. It often does this by making bad decisions very painful to test. Sounds bizarre, and I don’t think my explanation here does it the justice it deserves.

Hopefully you’re open to improving your code. If you are, then I think PhpSpec can do just that.

I also appreciate that this is a beginners series, and we are about to ramp up the difficult here. The thing with testing is it’s probably one of the fastest ways to improve yourself as a coder.

Start Watching 🙂

Until next week, have a great weekend and happy coding.

Chris

(If you liked this, consider subscribing to the mailing list)