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

The Truth About Testing

In this week’s new videos we are diving head first into PhpSpec.

I am fully aware that this is a series that is aimed at beginners, but in my opinion it’s never too early to learn how to test.

Before we go further though, I do want to stress that PhpSpec is a highly opinionated approach to testing, and that opinion may not be one that you agree with. Or, more importantly, enjoy the feeling you get when you use it.

There are a couple of alternatives to PhpSpec that I have used and would recommend:

But this is somewhat misleading, as Codeception uses PhpUnit for unit testing.

Ok, so at this point, if you are someone new to testing, you may be feeling confused. A very valid question here is:

What the heck is unit testing?

My definition of unit testing is testing individual functions / methods.

There’s major benefits to doing this, which I am hoping to explore with you in these next few videos.

Let’s quickly cover unit testing by way of an example. Imagine we have a function that adds two numbers together:

function add($x, $y)
{
    return $x + $y;
}

We could manually test this:

echo add(1,2);

And we should expect to see 3.

One of the first things we do when writing code is find a way to output what we’ve done – to check if what we are doing is working. In PHP we can do this in a variety of ways: die , var_dump , echo , print_r , the list goes on.

This is a form of testing.

If all we are testing here is the output of a single function then heck, this is manual unit testing.

The problem is: we generally don’t have just one function. And if we do, that’s a different kind of problem in itself 🙂

Instead, what we will likely do is start using this add function in a variety of situations throughout our code base.

We might manually test those functions as well. In doing so, we indirectly test the other functions that function depends on.

And that’s all good.

Whilst all this logic is fresh in your head – whilst you’re deep into the systems internals – it’s all there as clear as day. But then another problem arises and you get sidetracked.

And when you return to this code even just a short time later, somehow the fog has begun to set in. You’re not quite sure how that method works anymore. You aren’t able to interpret that 70 line method exactly in your head.

At this point our untested code immediately becomes legacy code. At least, that’s my opinion.

We want to start refactoring it, to fix up all the confusion and restore that prior clarity.

But we can’t because the more we meddle, the more stuff breaks. Or may break. Coding is so stressful!!!!

zen-monk

Or, you can swap all this for zen.

You know your code works because the tests pass. Those tests didn’t pass to begin with. You’ve had to write code to prove how your system works. And now when you change your code, if your tests still pass then your system still works exactly as you intended.

And it’s surprising how, when doing this, you will break so many unexpected things when changing seemingly unrelated bits and pieces.

This takes your skill level further. You start to learn about better design as a result.

I’ve found PhpSpec will lead you towards a certain design.

That design is the distilled guidance from very clever people. Why not leverage their knowledge to help your project, but also you – to make your life easier, and less stressful?

Back to our function.

What if we had created our function using unit tests for guidance?

Well, we could take a look at using a unit test suite to write tests for this function.

However, the reality of it is, if you’re working with Symfony, you’re going to be writing code in a certain way.

Why not learn how to write unit tests in an environment just like your real world projects?

I think this is a better way to learn. It’s a little more effort upfront, but you’re learning how it can really and immediately help you become better on the type of code you have to write in your day job.

And so that’s what we are doing in these three videos:

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/testing-with-phpspec-to-guide-our-implementation

In case you haven’t been following along, in the past three video’s we’ve been learning how we can use EasyAdminBundle as a quick way to add a really nice UX to our admin area.

We can manage all our existing wallpapers from the back end, but it would be super useful to us if we could handle new wallpaper file uploads from the admin area, too.

EasyAdminBundle comes with a documented way to integrate with VichUploaderBundle.

We could have chosen to go that route, again leveraging the wisdom of the collective.

Instead we are doing some DIY. Our design decisions are not about sharing this code. We’re just exploring some concepts. We want to learn about file uploads, and hopefully improve ourselves as developers a little in the process.

The thing is, handling uploads is code that will be super important to the site and, if it all works, it will be used a lot.

This is the sort of problem that if you don’t handle up front, you’re going to be getting frantic calls from bosses and clients to fix as a matter of up-most urgency :/

No one wants to deal with that. Not you. Not your boss. Not the client.

So, when I hit on code like that, I reach for the unit tests.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/using-phpspec-to-test-our-filemover

In the previous video we covered a little of how the design decisions came about, and started our test routine.

Now we’re going to write some specifications of how we expect this system to behave, if it is performing as expected.

🙂

This is the insurance policy that allows us to refactor our confusing code later on.

If the implementation we write now makes the tests pass, we can know for sure if our code changes don’t lead to failing tests, we have altered the logic without altering the outcome.

If your project survives, this makes it’s lifetime more enjoyable for you as a developer.

You’ll inevitably get to work on more features, not constant and stressful bug fixing.

https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/symfony-dependency-testing-with-phpspec

Finally we get into how all this integrates with Symfony.

Understand these three pieces and the process essentially repeats over and over for whatever you want or need to test.

The truth about testing is that it’s a pain to get started.

But once you have tested one thing, you can use that one single thing as a reference and it becomes A LOT easier to add tests to an already tested system.

Ok, so that’s why I love testing.

Hopefully if you can invest just 15 minutes into your PHP skills this weekend, it will be these minutes you choose 🙂

I’d love to hear your thoughts on this series so far.

I’m also under way with the big back end refresh once again. I’m going to be deploying this new site as the topic of the upcoming Docker series, which I think is quite a cool way to cover it. It will at least show you how it all works behinds the scenes, if nothing else.

If you haven’t done so recently, why not check out what else is on the site?

Thanks for reading, and have a great weekend,

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)

 

 

 

Centrifugo Docker Compose Config

    centrifugo:
        image: centrifugo/centrifugo:1.7.3
        environment:
          - CENTRIFUGO_SECRET=potato
          - CENTRIFUGO_ADMIN_PASSWORD=potato
          - CENTRIFUGO_ADMIN_SECRET=potato
        command: centrifugo --web
        ports:
          - "8569:8000"
        networks:
          crv_network:
            aliases:
              - crv_centrifugo

Admittedly not likely useful by many, but hopefully will save someone some time in the future.

This is just stuff I found throughout various GitHub tickets that’s good enough to get Centrifugo up and running on http://127.0.0.1:8569/