Using Mock in Laravel Unit Testing

When you have a test that uses a class that pull data from an external API, it might be a good idea to mock this object so it does not reach anywhere outside of your local development or your CI service provider, and you can assure you are getting the data you are expecting.

Let’s use the simple example provided by Mockery.

class Temperature
    private $service;

    public function __construct($service)
        $this->service = $service;

    public function average()
        $total = 0;
        for ($i=0; $i<3; $i++) {
            $total += $this-&gt;service-&gt;readTemp();
        return $total/3;

You need to test the $service. However, this $service class calls an external API when you call readTemp(), we can use Mockery to bypass that when running test in Laravel.

According to Laravel Mocking, in 5.8, you can do the following

use App\Service;

$service = $this->mock(Service::class, function ($mock) {
    $mock->shouldReceive('readTemp')->times(3)->andReturns(10, 12, 14);

So, in your test, you can do the following:

public function TemperatureAverageTest()
    $service = $this->mock(Service::class, function ($mock) {
        $mock->shouldReceive('readTemp')->times(3)->andReturns(10, 12, 14);  

    $temperature = new Temperature($service);
    $this->assertEquals(12, $temperature->average());

When I first use Mockery in 5.8, I was stumbleupon on why after running $this->mock(...) the object does not create a mock. That’s when I find out it actually returns a mock object not overriding automatically all Service, I actually have to use it and apply it in my code.

The Bullet Journal Method

In the digital age, pen and paper still thrive. Our brains are just better with pen and paper. (I think that’s why Apple Pen exists)

In this book, it is not a “how” to do Bullet Journal (BoJo). It’s why to BoJo.

It does not tell you how to take notes, it give you a starter method and some example for you to build on.

I personally use the Muji Weekly Monthly Planner, but using this, I can also implement the BoJo method to my note taking and journaling method.

In the end, I think having your own style to make a journal is the best, but BoJo is a good place to start if you don’t know where to begin.

Head first Design Pattern

It’s conversational style of explaining the common use design patterns makes it easier to understand and more fun to read.

Design pattern is one of the “soft skill” that developer should add to their experience. One might never use them depending on their work requirement, but having these design pattern concept in their mind will help developing better softwares.

Once you finish reading this book, I believe, it’s easier to move on to the GoF Design Pattern book.

Develop with TailwindCSS

TLDR; I just did a project using Tailwind CSS. It’s as good as it advertised.

I came from working with Bootstrap for a long time. It has been my go to HTML/CSS framework. I can quickly build a good business looking website quickly with Bootstrap.

I just had a small project that I needed to work on. It’s not difficult so I got to spend sometimes on it using Tailwind.

At first, since coming from Bootstrap, there’s some initial learning curve needed to overcome with. Once I got it setup and running, I see the benefit of using a Utility First Framework approach to develop the front-end. It’s quick, easy to customize, and importantly, easy to read. (We spend 10 times more to read code than writing code, maybe 20 times more to read HTML/CSS more than writing HTML/CSS code ).

It’s good to have more tools to tackle problems and I think both Bootstrap and Tailwind are good tools to have in the toolbox.

Managing Virtual Machines

I use multiple two virtual machines that were built with Vagrant. It is a pain most of the time to shutdown both of them when I am done for the day.

I wrote the scripts to shutdown all the running virtual machines with one command.