Laravel Testing Decoded the Testing Book You’Ve Been Waiting For
Total Page:16
File Type:pdf, Size:1020Kb
Laravel Testing Decoded The testing book you’ve been waiting for. JeffreyWay This book is for sale at http://leanpub.com/laravel-testing-decoded This version was published on 2013-05-28 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2013 JeffreyWay Tweet This Book! Please help JeffreyWay by spreading the word about this book on Twitter! The suggested tweet for this book is: Just bought @jeffrey_way’s new book, Laravel Testing Decoded! The suggested hashtag for this book is #laravelTesting. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search/#laravelTesting Contents Welcome .............................................. 1 It Has Begun ........................................... 1 Is This Book For Me? ...................................... 2 Why Laravel-Specific? ..................................... 2 Exercises ............................................. 3 Errata .............................................. 3 How to Consume This Book .................................. 3 Get in Touch ........................................... 3 Into the Great Wide Open .................................... 5 Chapter 1: Test All The Things .................................. 6 You Already Test ........................................ 6 6 Wins From TDD ........................................ 7 1. Security ......................................... 7 2. Contribution ....................................... 7 3. Big-Boy Pants ...................................... 8 4. Testability Improves Architecture ........................... 9 5. Documentation ..................................... 9 6. It’s Fun .......................................... 9 What Should I Test? ....................................... 10 6 Signs of Untestable Code ................................... 10 1. New Operators ..................................... 11 2. Control-Freak Constructors ............................... 12 3. And, And, And ..................................... 13 4 Ways to Spot a Class With Too Many Responsibilities . 13 4. Too Many Paths? Polymorphism to the Rescue! .................... 14 5. Too Many Dependencies ................................ 17 6. Too Many Bugs ..................................... 17 Test Jargon ............................................ 18 Unit Testing ........................................ 18 Model Testing ....................................... 18 Integration Testing ..................................... 19 Functional (Controller) Testing .............................. 19 CONTENTS Acceptance Testing .................................... 19 Relax ............................................... 21 Chapter 2: Introducing PHPUnit ................................. 22 Installation ............................................ 22 Making Packages Available Globally ........................... 24 Assertions 101 .......................................... 25 Decoding A Test Class Structure ............................. 27 assertTrue ......................................... 27 assertEquals ........................................ 28 assertSame ......................................... 29 assertContains ....................................... 30 assertArrayHasKey .................................... 31 assertInternalType ..................................... 31 assertInstanceOf ...................................... 32 Asserting Exceptions .................................... 33 Summary ............................................ 33 Chapter 3: Configuring PHPUnit ................................ 35 Options ............................................. 35 Technicolor ......................................... 35 Bootstrapping ....................................... 37 Output Formats ...................................... 38 XML Configuration File .................................... 38 Continuous Testing ....................................... 40 Watching Files ....................................... 43 Triggering Multiple Files .................................. 44 Some Vim-Specific Advice ................................ 45 Summary ............................................ 46 Chapter 4: Making PHPUnit Less Verbose ........................... 47 Importing Assertions as Functions ............................... 47 Applying the Laravel Style to PHPUnit ............................ 48 Chapter 5: Unit Testing 101 .................................... 50 My Struggles .......................................... 50 Unit Testing ........................................... 51 Arrange, Act, Assert ...................................... 51 Testing in Isolation ....................................... 52 Tests Should Not Be Order-Dependent ............................. 53 Test-Driven Development .................................... 53 Behavior-Driven Development ................................. 53 Testing Functions ........................................ 54 Slime vs. Generalize .................................... 58 CONTENTS Slime ........................................ 58 Generalize ..................................... 58 Making the Test Pass .................................... 59 Testing Classes ......................................... 63 Refactoring the Tests .................................... 69 Refactoring the Production Code ............................. 71 Polymorphism ....................................... 72 Extensibility .................................... 75 Mocks ........................................ 75 Project Complete ........................................ 77 Final Source ........................................ 77 Summary ............................................ 81 Chapter 6: Testing Models .................................... 82 What to Test ........................................... 82 Accessors and Mutators ..................................... 82 Cat Years Example ..................................... 83 Password Hashing Example ................................ 83 Custom Methods ........................................ 84 Validations ............................................ 85 Helpers ........................................... 91 Factories ............................................. 93 Laravel Test Helpers ....................................... 95 Factories .......................................... 95 Overrides .......................................... 96 Models ........................................... 96 Test Helpers ........................................ 97 assertValid and assertNotValid .............................. 97 Asserting Relationships .............................. 97 Summary ............................................ 98 Chapter 7: Easier Testing With Mockery ............................ 99 Mocking Decoded ........................................ 99 Installation ............................................101 The Dilemma ..........................................102 Dependency Injection ...................................103 The Solution ...........................................106 Simple Mock Objects ...................................108 Return Values From Mocked Methods . 109 Expectations ...........................................111 Partial Mocks ..........................................112 Hamcrest ............................................114 Summary ............................................116 CONTENTS Chapter 8: Test Databases .................................... 117 Test Databases ..........................................117 Specifying the Environment ..................................118 Calling Artisan From Tests ...................................119 Try It Out ............................................120 Databases in Memory ......................................121 Summary ............................................122 Chapter 9: Just Swap That Thang ................................ 123 Mockery .............................................123 Testing ..............................................124 Mocking Events ......................................127 Summary ............................................128 Chapter 10: Testing Controllers ................................. 129 What Does a Controller Do? ..................................129 3 Steps to Testing Controllers ..................................130 The Hello World of Controller Testing .............................130 Overloading is Your Friend ................................131 Calling Controller Actions .................................132 Laravel’s Helper Assertions ...................................132 Mocking the Database .....................................135 Required Refactoring ...................................135 The IoC Container .....................................139 Redirections ...........................................139 Paths ...............................................140 Repositories ...........................................143 Structure .............................................147 Updating the Tests .....................................150 Crawling the DOM .......................................153 Ensure View Contains Text ................................154 Basic Traversing ......................................154 Fetch