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
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 . . . . . . . . .
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
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