03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Controller Testing 353<br />

1 public function store()<br />

2 {<br />

3 $validator = Validator::make(Input::all(), [<br />

4 "title" => "required|max:50",<br />

5 "subtitle" => "required|max:100",<br />

6 "body" => "required",<br />

7 "author" => "required|exists:authors"<br />

8 ]);<br />

9<br />

10 if ($validator->passes()) {<br />

11 Posts::create([<br />

12 "title" => Input::get("title"),<br />

13 "subtitle" => Input::get("subtitle"),<br />

14 "body" => Input::get("body"),<br />

15 "author_id" => Input::get("author"),<br />

16 "slug" => Str::slug(Input::get("title"))<br />

17 ]);<br />

18<br />

19 Mail::send("emails.post", Input::all(), function($email) {<br />

20 $email<br />

21 ->to("cgpitt@gmail.com", "Chris")<br />

22 ->subject("New post");<br />

23 });<br />

24<br />

25 return Redirect::route("posts.index");<br />

26 }<br />

27<br />

28 return Redirect::back()<br />

29 ->withErrors($validator)<br />

30 ->withInput();<br />

31 }<br />

.<br />

This was extracted from app/controllers/PostController.php. I generated the file with the<br />

controller:make command.<br />

This is how we first learn to use MVC frameworks, but there comes a point where we are familiar<br />

with how they work, and need to start writing tests. Testing this action would be a nightmare.<br />

Fortunately, there are a few improvements we can make.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!