Category Archives: Laravel

Testing Laravel Application

<?php

class DeveloperTest extends TestCase {

    public function we_all_should_write_test()
    {
        $this-&gt;assertTrue(true);
    }
}

?&gt;

If you don’t know where to start, this is a very good starting point.

Two points to emphasize:

  1. Tests give you confident to change code. (Laracasts)
  2. The value of tests get higher as time goes on.

Usually my workflow is this:

  1. How the feature is reached?
  2. What is expected to come out?

That’s pretty much it. Write test first, not after. It helps me clarify the problem and write a better solution.

Laracon 2019

In Laracon, you usually get some hard technical talks and some soft talks. I recommend to check out @bobbybouwmann if you already have at least one year of Laravel experience, @gonedark and @colindecarlo’s talk if you are quite seasonal.

The usual speakers such as @adamwathan, @freekmurze, @gonedark, @mijustin, @colindecarlo, @youyuxi, and @stauffermatt.

Of course, @taylorotwell‘s vapor announcement was the “keynote”. Watch it.

@kthomas901 gave a talk on launching side project. She said “…wanting the security from corporate is completely OK…” and “…know when to end.” are my favorite quotes.

LiveWire created by @calebporzio is some great stuff.

I also get a chance to meet my favorite podcast hosts @michaeldyrynda and @JacobBennett. I also get to say thank you to @marcelpociot for helping me getting started with creating a PHP package.

My feeling is that @mijustin and @stauffermatt are the Laravel evangelists and they are the biggest cheerleaders of the Laravel community. I like how @mijustin just pour it out and reminded all of us that we only see a person’s positive side from the Internet but they could be struggling. He was reminding us that it’s OK to fail and it’s OK to share about failure. @stauffermatt reminded us that what a special community @taylorotwell has started. We must cherish this magic, continue to be proud of what we do, and continue to be a welcoming community.

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-&gt;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.

Laravel Socialite and Google

Google announced their plan to shutdown Google+ by March 2019. Along with the Google+ platform, the Google+ API will be depreciated too.

On Laravel Socialite, it used to use Google+ to authenticate, but it no longer uses Google+ platform.

If you are using Socialite 4.x, your project in the Google Cloud Platform no longer needs to enable Google+ API since the default scope is already selected for authentication needs.

That’s all you need to use Google OAuth with Laravel Socialite

Update Laravel Socialite from using Google+ API

68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d736f6369616c6974652e737667

The Laravel Socialite uses Google+ API to authenticate user, but since Google+ API is shutting down, the https://www.googleapis.com/plus/v1/people/me will no longer works.

If you are currently using Google+ API for Socialite Google OAuth2 authentication, you should upgrade Socialite version 4.

If are stuck with Socialite version 3.x because you need to run Laravel with lower or equal to version 5.5 (or because of PHP 7.0), change the composer package to 3.0.x-dev and run composer update as of this writing.

I am expecting Laravel Socialite 3.x Google OAuth API will be updated soon, but the package has not yet released.