Dulo
Dulo

Reputation: 119

Laravel test If / else statement - unit test

Can somebody help me out with a test for function in controller. Cannot test if statement in controller, so please focus on this piece of code. Other tests for this controller are good, but only code from "if statement" cannot be tested.

LanguageController.php

class LanguageController extends Controller implements IEntityViewManager
{
    protected $languageRepo;

    public function __construct(LanguageRepositoryInterface $languageRepo)
    {
        $this->languageRepo = $languageRepo;
    }

    public function createAction(LanguagePostRequest $request)
    {
        $languages = $this->languageRepo->whereCharOrName($request->char, $request->name);

        if(count($languages) > 0)
        {
            return redirect()->back()->withErrors("Already exists")->withInput();
        }

        $language = new Language();
        $this->languageRepo->store($language, $request->all());

        return redirect()->route('Admin.Language.showAllView');
    }
}

Here is my Test reqirements for this test:

LanguageControllerTest.php

class LanguageControllerTest extends TestCase
{
    public function __construct($name = NULL, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
    }

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    protected function setUpMock()
    {
        $mock = Mockery::mock(LanguageRepositoryInterface::class);
        $this->app->instance(LanguageRepositoryInterface::class, $mock);

        return $mock;
    }

    public function testInvalidInsertLanguage1()
    {
        $params = array(
            'char' => 'en',
            'name' => 'English'
        );

        $mock = $this->setUpMock();

        // HELP ME TO TEST IF STATEMENT AND TO REDIRECT BACK WITH ERRORS AND INPUTS
        // NEED CONTENT

        $this->action('POST', 'Entities\LanguageController@createAction', null, $params);
    }

Or maybe I should avoid if statement and put it in some other function within controller, but it is too complicated to test, because I should mock this controller ?

Upvotes: 1

Views: 2210

Answers (1)

Farhan Rahman
Farhan Rahman

Reputation: 206

You can do the following, which sets up a mock which expects the method that initialises languages. Now since you want to only test the count method you just return an array with an element in it. This will test the redirect part of your controller. I haven't run this code but hopefully you get the idea.

public function setUp()
{
    $this->mock = Mockery::mock("LanguageRepositoryInterface");
}

public function testInvalidInsertLanguageRedirect()
{
    $params = array(
        'char' => 'en',
        'name' => 'English'
    );

    $this->mock
         ->shouldReceive("whereCharOrName")
         ->once()
         ->andReturn([1]);

    $response = $this->action('POST', 'Entities\LanguageController@createAction', null, $params);
    $this->assertEquals(301, $response->status());   
}

Upvotes: 1

Related Questions