1

My controller is as follows,

class LoginAdminController extends Controller
{
    public function loginAdmin($username, $password)
    {
        dd($username);
        //check valid admin credentials
        $pwd = admin_users::select('adm_password')->where('adm_username', $username)->first();
        if(!empty($pwd))
        {
            if(Crypt::decrypt($pwd->adm_password) == $password)
            {
                $admin_users =  admin_users::select('adm_user_id')->where('adm_username', $username)->first();
                if(!empty($admin_users->adm_user_id))
                {
                    return $admin_users->adm_user_id;
                }
                else
                {
                    return FALSE;
                }
            }
            else
            {
                return FALSE;
            }
        }
        else
        {
            return FALSE;
        }
    }
}

My unit test case is follows,

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Http\Controllers\Auth\LoginAdminController;

class LoginAdminTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */

    use WithoutMiddleware;

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

    public function testloginAdminValidData()
    {
        $response = $this->action('get', 'LoginAdminController@index');
        var_dump($response->getContent());
    }
}

I have to test this loginAdmin() function for unit testing. There is no route defined for this method in routes.php. Can I call this method directly from test case? If yes, then please give me command to call it with parameters. Thanks in advance

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
Mahesh Gaikwad
  • 192
  • 2
  • 18
  • My initial thought would be to move `loginAdmin` to a Service class. – Pieter van den Ham Jul 26 '16 at 13:14
  • On a side note, you might be interested in [the difference between encrypting and hashing passwords](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it). – Pieter van den Ham Jul 26 '16 at 13:18
  • 2
    Thanks for your response. I got the solution. I have created the object of controller and then called the method by using $this->controllerobj->methodName() – Mahesh Gaikwad Jul 27 '16 at 13:00

0 Answers0