How I used Advent of Code to be a better at Pest testing with Laravel ?

Published at Dec 17, 2023

It's been a long time I didn't took a developer challenge. But this year Advent Of Code kept appearing in my social feed. So tried!

Introduction

For the first days of the month, two youtubers I'm subscribed to had taken the challenge daily. One of them is doing in pure PHP and the other was in multiple languages. When I've seen his video of him trying to do it in PHP. I wanted to help me so bad, show him the "correct" way. And I wanted to comment that in Laravel it's even easier!

So instead of being a PHP fanboy crying on Internet, I decided to take the challenge too. And boy it's fun ! Everyone should do try it 😃

The workflow

After day 2 or 3, I had a workflow to take the challenge as fast as possible. Not that I try to be on the scoreboard but you know nobody like doing the same initial setup every day.

  1. Going on advent of calendar for the challenge of the day
  2. Open Tinkerwell to start tinkering. An input variable, an output variable and an expect variable and we're ready!
  3. Coding!
  4. Repeat for the part 2 of the day
  5. Converting my code to a useable and testeable class
  6. Publishing my result ! (and making a tweet about it 😄)

How did it go?

One thing I didn't like about the challenge, and I think it's done on purpose, often the test input is not representative of every case you can encounter. And you make a code that works with the test data but doesn't work with the full data.

It's also been interesting to see my way of coding changing over time. Because I knew how I will export it to a class after, I was more and more capable of predicting which part would be a function, which part should be tested separately, etc..

Every time I change a functional code to class component, I make small mistakes. Testing it against the same data is very useful.

Some problem were way beyond my mathematical scope. I let one script run for hours just to see the time it takes with my code. I gave up before and did go watch videos of other participants to understand what I am missing. Call it cheating, I call it learning :)

The annoying part for me was that I was specialised on mathematics in college. And often it was obvious for me that there is a mathematical shortcut but I couldn't remember how. I took too much time remembering the existence of LCM function, and it made me angry at the end 😅

What did I learned ?

This project helped me learn the following:

  • Laravel Folio
  • Use of dataset and group in Pest.
  • And also give me a lot of experience on how to transform a proof of concept into a useable and testeable class in a project.
  • Tinkerwell is a great tool to tinker but PHPStorm has so much more to offer. At the end of every day, PHPStorm inspections had always something to say to optimize / clean my code.
  • Sometimes when a problem change by a little it's better to take a step back and reconsider the whole
  • You can forget mathematical knowledge over time 🙁

Conclusion

In the end, I think the challenge is a great way to tinker, to learn, to be better at programming. Or maybe you are already a god of programming, then you will become a better mathematician.

No matter how you take the challenge, you will gain valuable experience. Even if you do only couple of days.

#php #thinking #pest

Syntax highlighting provided by torchlight.dev