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.
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.
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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community