Laravel Testing Decoded - baboon [PDF]

Please help JeffreyWay by spreading the word about this book on Twitter! The suggested tweet for this book is: Just boug

48 downloads 11 Views 7MB Size

Recommend Stories


PDF Decoded Full Book
If your life's work can be accomplished in your lifetime, you're not thinking big enough. Wes Jacks

Laravel Up & Running Pdf
Life isn't about getting and having, it's about giving and being. Kevin Kruse

The baboon
The wound is the place where the Light enters you. Rumi

PDF Laravel 5 Essentials
Respond to every call that excites your spirit. Rumi

[PDF] Laravel: Up and Running
The best time to plant a tree was 20 years ago. The second best time is now. Chinese Proverb

PDF Laravel: Up and Running
Never let your sense of morals prevent you from doing what is right. Isaac Asimov

[PDF] Laravel: Up and Running
How wonderful it is that nobody need wait a single moment before starting to improve the world. Anne

PDF Laravel: Up and Running
Happiness doesn't result from what we get, but from what we give. Ben Carson

[PDF] Laravel: Up and Running
The butterfly counts not months but moments, and has time enough. Rabindranath Tagore

[PDF] Laravel: Up and Running
Forget safety. Live where you fear to live. Destroy your reputation. Be notorious. Rumi

Idea Transcript


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 . . . . . . . . . . . . It Has Begun . . . . . . . . . Is This Book For Me? . . . . Why Laravel-Specific? . . . Exercises . . . . . . . . . . . Errata . . . . . . . . . . . . How to Consume This Book Get in Touch . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

1 1 2 2 3 3 3 3

Into the Great Wide Open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

Chapter 1: Test All The Things . . . . . . . . . . . . . . . . . . . . . You Already Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Wins From TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . 1. Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2. Contribution . . . . . . . . . . . . . . . . . . . . . . . . . . 3. Big-Boy Pants . . . . . . . . . . . . . . . . . . . . . . . . . 4. Testability Improves Architecture . . . . . . . . . . . . . . 5. Documentation . . . . . . . . . . . . . . . . . . . . . . . . 6. It’s Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Should I Test? . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Signs of Untestable Code . . . . . . . . . . . . . . . . . . . . . . 1. New Operators . . . . . . . . . . . . . . . . . . . . . . . . 2. Control-Freak Constructors . . . . . . . . . . . . . . . . . . 3. And, And, And . . . . . . . . . . . . . . . . . . . . . . . . 4 Ways to Spot a Class With Too Many Responsibilities 4. Too Many Paths? Polymorphism to the Rescue! . . . . . . . 5. Too Many Dependencies . . . . . . . . . . . . . . . . . . . 6. Too Many Bugs . . . . . . . . . . . . . . . . . . . . . . . . Test Jargon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . Model Testing . . . . . . . . . . . . . . . . . . . . . . . . . . Integration Testing . . . . . . . . . . . . . . . . . . . . . . . . Functional (Controller) Testing . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

6 6 7 7 7 8 9 9 9 10 10 11 12 13 13 14 17 17 18 18 18 19 19

CONTENTS

Acceptance Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Relax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19 21

Chapter 2: Introducing PHPUnit . . . . . . Installation . . . . . . . . . . . . . . . . . Making Packages Available Globally Assertions 101 . . . . . . . . . . . . . . . Decoding A Test Class Structure . . assertTrue . . . . . . . . . . . . . . assertEquals . . . . . . . . . . . . . assertSame . . . . . . . . . . . . . . assertContains . . . . . . . . . . . . assertArrayHasKey . . . . . . . . . assertInternalType . . . . . . . . . . assertInstanceOf . . . . . . . . . . . Asserting Exceptions . . . . . . . . . Summary . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

22 22 24 25 27 27 28 29 30 31 31 32 33 33

Chapter 3: Configuring PHPUnit Options . . . . . . . . . . . . . Technicolor . . . . . . . . . Bootstrapping . . . . . . . Output Formats . . . . . . XML Configuration File . . . . Continuous Testing . . . . . . . Watching Files . . . . . . . Triggering Multiple Files . . Some Vim-Specific Advice Summary . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

35 35 35 37 38 38 40 43 44 45 46

Chapter 4: Making PHPUnit Less Verbose . . . . . . . . . . . . . . . . . . . . . . . . . . . Importing Assertions as Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Applying the Laravel Style to PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47 47 48

Chapter 5: Unit Testing 101 . . . . . . . My Struggles . . . . . . . . . . . . . Unit Testing . . . . . . . . . . . . . . Arrange, Act, Assert . . . . . . . . . Testing in Isolation . . . . . . . . . . Tests Should Not Be Order-Dependent Test-Driven Development . . . . . . . Behavior-Driven Development . . . . Testing Functions . . . . . . . . . . . Slime vs. Generalize . . . . . . .

50 50 51 51 52 53 53 53 54 58

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

CONTENTS

Slime . . . . . . . . . . . Generalize . . . . . . . . Making the Test Pass . . . . . . . Testing Classes . . . . . . . . . . . . Refactoring the Tests . . . . . . . Refactoring the Production Code Polymorphism . . . . . . . . . . Extensibility . . . . . . . Mocks . . . . . . . . . . . Project Complete . . . . . . . . . . . Final Source . . . . . . . . . . . Summary . . . . . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

58 58 59 63 69 71 72 75 75 77 77 81

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

82 82 82 83 83 84 85 91 93 95 95 96 96 97 97 97 98

Chapter 7: Easier Testing With Mockery . . Mocking Decoded . . . . . . . . . . . . . . Installation . . . . . . . . . . . . . . . . . . The Dilemma . . . . . . . . . . . . . . . . Dependency Injection . . . . . . . . . The Solution . . . . . . . . . . . . . . . . . Simple Mock Objects . . . . . . . . . Return Values From Mocked Methods Expectations . . . . . . . . . . . . . . . . . Partial Mocks . . . . . . . . . . . . . . . . Hamcrest . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

99 99 101 102 103 106 108 109 111 112 114 116

Chapter 6: Testing Models . . . . . . What to Test . . . . . . . . . . . . . Accessors and Mutators . . . . . . . Cat Years Example . . . . . . . Password Hashing Example . . Custom Methods . . . . . . . . . . Validations . . . . . . . . . . . . . . Helpers . . . . . . . . . . . . . Factories . . . . . . . . . . . . . . . Laravel Test Helpers . . . . . . . . . Factories . . . . . . . . . . . . Overrides . . . . . . . . . . . . Models . . . . . . . . . . . . . Test Helpers . . . . . . . . . . assertValid and assertNotValid Asserting Relationships Summary . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

CONTENTS

Chapter 8: Test . This is a temporary alias, but can easily be added to ∼/.bash_profile.

Making Packages Available Globally The method outlined in the previous section requires PHPUnit to be installed through Composer for each new project. If you prefer, you can alternatively create a master composer project for packages that should be available globally. To allow for this, within your computer’s home directory (or anywhere, really), create a new folder, composer-packages, add its companion composer.json file, and list your desired global dependencies within the require-dev object.

Chapter 2: Introducing PHPUnit 1

{ "require": { "phpunit/phpunit": "3.7.*" }

2 3 4 5

25

}

Next, add the full path to the project’s vendor/bin directory to your path. Here’s an example for my Unix-based system. This can be added to your ∼/.bash_profile or ∼/.zshrc, if you use oh-myzsh¹³. 1

export PATH=/Users/Jeffrey/composer-packages/vendor/bin

With this technique, PHPUnit will always be available. Even better, updating these global packages only requires a simple composer update. While installing PHPUnit directly through Composer is certainly a valid route - and will do just fine for the purposes of this book - some developers prefer to stick with Pear (I have no idea why). If you fit that description, feel free to do so. It makes zero difference.

Assertions 101 It’s time for the “hello world” of testing. In this section, you’ll be introduced to your first PHPUnit assertion, assertTrue. Add a new file, called PracticeTest.php, to a tests folder within your project. Here’s some boilerplate to get you started. 1

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.