jeudi 20 août 2015

How to use assertInstanceOf on a Laravel Controller that is injected with a repository?

I'm trying to create a test to see if a controller has been created, however, the controller's constructor requires a repository instance to be passed to it.

Here is my controller:

<?php namespace App\Http\Controllers\api\v1;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use ApiHandler;
use App\Repositories\ChecklistsRepository;

class EntitiesChecklistsController extends Controller {

    private $checklist;

    public function __construct(ChecklistsRepository $checklist)
    {

        $this->checklist = $checklist;
    }


    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($entityId) {

            $checklistsResponse = $this->checklist->with(["taskGroups.tasks","checklistsTaskGroups.tasks"])->findByField('entities_id',$entityId);



            return $checklistsResponse;

    }



}

Here is my repo:

<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository;

class ChecklistsRepository extends BaseRepository
{

    public function model()
    {
        return 'App\Models\Checklist';
    }



}

This is the test case I am trying to run using PHPUnit:

<?php

use App\Repositories\ChecklistsRepository;
use Illuminate\Container\Container as Application;
use Symfony\Component\HttpFoundation\Tests\JsonResponseTest;
use App\Http\Controllers\api\v1\EntitiesChecklistsController;


class EntitiesChecklistsControllerTest extends TestCase
{
    private $checklistRepoMock;

    public function setUp()
    {
        parent::setUp();
       $this->checklistRepoMock = $this->mock('App\Repositories\ChecklistsRepository');
    }




    public function testEntitiesChecklistsControllerCreated()
    {

        $actual = new EntitiesChecklistsController($this->checklistRepoMock);
        $expected = 'App\Http\Controllers\api\v1\EntitiesChecklistController';
        $this->assertInstanceOf($expected, $actual);
    }




    public function mock($class)
    {
        $mock = Mockery::mock($class);

        $this->app->instance($class, $mock);

        return $mock;
    }

}

Again the issue is that I need to pass an instance of the repository to the controller, but I keep getting the following error:

1) EntitiesChecklistsControllerTest::testEntitiesChecklistsControllerCreated ErrorException: Argument 1 passed to App\Http\Controllers\api\v1\EntitiesChecklistsController::__construct() must be an instance of App\Repositories\ChecklistsRepository, string given, called in C:\xampp\htdocs\comply-app\comply-seed-app\laravel\tests\Http\Cont

Aucun commentaire:

Enregistrer un commentaire