Support the ongoing development of Laravel.io →
posted 10 years ago
IOC Architecture
Last updated 2 years ago.
0

Use the IoC container to inject your classes either through the controller's constructor or using the container directly App::make(); In terms of testing, you want to test each unit separately which is why you mock the other objects. You assert authentication works to test the controller and assert the controller works testing the auth.

Last updated 2 years ago.
0

Your code looks fine, inject classes through the constructor as Kevin mentioned. You can mock the authentication calls since that responsibility resides in another class which will have its own tests.

Last updated 2 years ago.
0

Mmmm, that sounds good. Mock the auth, and go for it. Does this mean I could still use Laravel's $this->be($user) construction?

As for my second question, I'm currently struggling with this:

public function postAdd()
    {
        /** @noinspection PhpUndefinedFieldInspection */
        $data = [
// get data here (Input)
        ];
        // create the new account:

        /** @var $account Account */
        $account = $this->accounts->initialize($data);

// or maybe:
$account = new Account($data);

// or something else?

// then, there's validation. I've extended my model a bit.
        if(!$account->validate()) {
            Session::flash('error', 'Validation failed. Please try harder.');
            Session::flash('error_extended', $account->errors()->first());
            return Redirect::route('addaccount')->withErrors($account->validator())->withInput();
        }
        // save it
        $account->save();
        // success!
        Cache::userFlush();
        Session::flash('success', 'The new account has been created.');
        return Redirect::to(Session::get('previous'));
    }

So the problem is:

How best to create a new account? I've built this "initialize" thing to handle validation and what-not, but I'm not sure if it's the right way to go. Dumping all my Input into a new Account does not work because reasons (user_id needs to be set, among other validation rules).

Should I extend the repository interface to do this, and implement that? Because the initialize/save object routine seems tailored to my Eloquent rules. Or maybe do $this->accounts->save($account) and accept a new Account; however it's made?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

JC5 jc5 Joined 24 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.