0
\$\begingroup\$

I am learning how to implement testing and try/catch statements into my code. I have a Laravel application with the following method. I am looking for advice on where I should add in try/catch statements and what phpunit unit-test(s) I would create to ensure this is functioning properly. Tips on comment blocks are also appreciated.

// User sign up
public function postSignup() 
{
    $validator = Validator::make(Input::all(), User::$rules);

    $confirmation_code = MyHelper::encrypt_decrypt('encrypt',Input::get('email'));

    if($validator->passes()) {
        $user = new User;
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));
        $user->emailVerifiedToken = $confirmation_code;
        $user->firstName = null;
        $user->lastName = null;
        $user->shipAddressId = null;
        $user->billAddressId = null;
        $user->phone = null;
        $user->save();

        $userdata = array(
            'token' => $confirmation_code
        );

        //Send the user an email requesting verification
        $toEmail = $user->email;
        $toName = $user->email;
        $subject = "Email Verification - example.com";

        Mail::queue('emails.account.account-creation', $userdata, function($message) use ($toEmail,$toName,$subject)
        {
            $message->to($toEmail,$toName)->subject($subject);
        });

        if(Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')), false))
        {
            return Redirect::to('/home/')
            ->with('login_message', 'Thank you for creating a new account, please sign in');
        }
    }

    return Redirect::to('/account/sign-up')
            ->with('signup_message',  'Something went wrong')
            ->withErrors($validator)
            ->withInput();
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

For try/catch, statements you should ask yourself: what are you trying to achieve? Do you need to handle the error differently in one controller compared to the rest of the application? If yes, then wrap the entire contents of your method.

If no, Laravel implements a global error handler. You simply need to throw your exceptions and Laravel handles the display of the errors.

As for you unit testing, I am only learning PHPUnit at the moment. Judging by your code, I believe you should take some particular emphasis on learning the S.O.L.I.D principles.

When you abide by these principles you will ensure your code is testable. To be testable your methods and objects need to have a single responsibility, such as taking requests (controller), validation (Request objects in Laravel and saving data (Models).

As for Doc blocks, refer to something like this.

Comment you code often. For methods I always specify in the first line a description of what the method does. (it should have a single responsibility). Then any parameters/arguments it takes (@param). Then any data it returns (@return). Specify the data type or 'void' if it returns nothing. Then finally any exceptions the method throws.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.