Inversion of Control (IoC) in PHP Laravel 5 together with PHPUnit.

The inversion of control is a pattern that helps us to decoupling our codes in the project. These are to main benefits . First ,easy to test, second easy to maintenance code. Of course , it is the ‘I’ of SOLID principle, that means ‘Code to Abstraction’.

In Laravel 5 , there is a resolver method that save you a lot of time to make your own resolver.

I would like to start with test first called BlogTest class.

php artisan make:test BlogTest

It produces. (Note: you have to write /** @test **/ before test method otherwise the phpunit will throw ‘no test executed’ error)

<?php

use
Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class BlogTest extends TestCase
{
/**
* Test BlogController & BlogRepository
*
*
@return void
*/
/**
@test **/
public function index_should_retrieve_data()
{
$this->visit('/blog')
->see('get blog return view blog.index');
}
}

If you run

phpunit --filter BlogTest::index_should_retrieve_data

in Terminal, it will throw error. Of course , we have no any implementation.

Let’s make the implementation. Starting with creating an interface. The interface is a contract that has no any logic or implementation inside. The concrete class who implements this interface must give the body of interface’s member (properties or methods or both).

<?php
namespace
App;

interface BlogRepositoryInterface {

public function getall();
}

Now we have BlogRepositoryInterface interface located in App\repository directory, but there is no implementation of this interface. Let’s implement it to a concrete class called BlogRepository (located in the same directory)

<?php
namespace
App;
use App\Http\Requests;

class BlogRepository implements BlogRepositoryInterface{

public function getall()
{
return 'get blog return view blog.index';
}

As you can see The BlogRepository is implementing BlogRepositoryInterface and it gives the body to method.

Before doing another work we have to register our App\repository directory in site the composer.json .

"autoload": {
"classmap": [
        // other autoload above
"app/repository"
// other autoload below

and use command line , to register.

composer dump

Now our app can find App\repository directory when it is running.

Next , Let’s bind the interface and concrete class together in the routes.php

App::bind('App\BlogRepositoryInterface', 'App\BlogRepository');

And this is our route.

Route::get('blog', [
'uses' => 'BlogController@index',
'as' => 'blog.index'
]);

Sure , we have no any controller to use the repository, let’s make it!! by run the command line .

php artisan make:controller BlogController

It will produce a BlogController. After we implement index method , now we get.

<?php

namespace
App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;

class BlogController extends Controller
{
public function index()
{

}
}

Next , bring the App\BlogRepositoryInterface to BlogController and inject it into the controller by using constructor injection. While you are injecting the repository interface , you have to initialise a member and use it to store the repository’s object.

<?php

namespace
App\Http\Controllers;

use App\BlogRepositoryInterface as IBlogRepository;
use Illuminate\Http\Request;

use App\Http\Requests;

class BlogController extends Controller
{
protected $repo;

public function __construct (IBlogRepository $repo){
$this->repo = $repo;
}

public function index()
{
return $this->repo->getall();
}
}

Let’s run phpunit again it will be green.

phpunit --filter BlogTest::index_should_retrieve_data
Unit test result

Ok , this is the basic of IoC concept in the Laravel. For full documentation check on this Link. (Although it is for Laravel4 , it still works in Laravel 5.2)

If you love this session Like and share it. if you have any question, please write it down below in the comment.

See you next session.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.