The Grumpy Programmer's PHPUnit Cookbook by Chris Hartjes

By Chris Hartjes

Studying how you can use PHPUnit doesnt need to suck.
My identify is Chris Hartjes. Im a long-time personal home page developer who has been selling trying out practices for nearly a decade. I wrote «The Grumpy Programmers advisor To construction Testable personal home page functions» to teach you ways you could write code you could simply try. The previous few years i've got spoken at meetings approximately most sensible practices for writing checks and been famous as knowledgeable on unit checking out. Now, i need that will help you discover ways to use PHPUnit to create checks that provide you with self belief that your code is at the correct track.
My new booklet «The Grumpy Programmers PHPUnit Cookbook» is a suite of assistance and tips for making PHPUnit do what you wish it do with the intention to write exams in your code. With the data contained during this ebook you'll get a bounce begin to make writing checks whatever you do without difficulty.

Show description

Read or Download The Grumpy Programmer's PHPUnit Cookbook PDF

Best computers & technology books

Amos 4.0 Users Guide

Ebook by way of

High-Speed Design Techniques (Seminar Series)

E-book by means of Walt Kester

Imagining the Internet: Personalities, Predictions, Perspectives

Within the early Nineteen Nineties, humans expected the loss of life of privateness, an finish to the present thought of "property," a paperless society, 500 channels of high-definition interactive tv, global peace, and the extinction of the human race after a takeover engineered through clever machines. Imagining the web zeroes in on predictions concerning the Internet's destiny and revisits previous predictions--and how they became out--to positioned that imagined destiny in point of view.

Fundamentals of Power System Protection

Strength approach is a hugely complicated dynamic entity. One malfunction or a slipshod set relay can jeopardize the whole grid. energy process safety as a topic bargains the entire components of intrigue, drama, and suspense whereas dealing with fault stipulations in genuine lifestyles.

Additional resources for The Grumpy Programmer's PHPUnit Cookbook

Sample text

If the test fails it will indicate which index in the associative array was being used for that test run. More Complex Examples Don’t feel like you can only have really simple data providers. All you need to do is return an array of arrays, with each result set matching the parameters that your testing method is expecting. csv"); $response = array(); 7 while ($data = fgetcsv($fp, 1000, ",")) { $response[] = array($data[0], $data[1], $data[2]); } 8 9 10 11 fclose($fp); 12 13 return $response; 14 15 } 42 Data Providers So don’t think you need to limit yourself in what your data providers are allowed to do.

It accepts an integer as the parameter • $this->atLeastOnce() is an interesting one, a good alternative to any() When you are creating expectations for multiple calls, be aware that whatever response you are setting through the use of this->with is only applicable to that specific expectation. php public testChangingReturnValuesBasedOnInput() { $foo = $this->getMockBuilder('Foo')->getMock(); $foo->expects($this->once()) ->method('bar') ->with('1') ->will($this->returnValue('I')); $foo->expects($this->once()) ->method('bar') Test Doubles 29 ->with('4') ->will($this->returnValue('IV')); $foo->expects($this->once()) ->method('bar') ->with('10') ->will($this->returnValue('X')); $expectedResults = array('I', 'IV', 'X'); $testResults = array(); $testResults[] = $foo->bar(1); $testResults[] = $foo->bar(4); $testResults[] = $foo->bar(10); 11 12 13 14 15 16 17 18 19 20 21 22 $this->assertEquals($expectedResults, $testResults); 23 24 } Often, the methods you invoke on dependencies accept arguments, and will return different values depending on what was passed in.

Look At All Those Tests If you didn’t know about data providers, what might your FizzBuzz tests look like? php class FizzBuzzTest extends PHPUnit_Framework_Testcase { public function setup() { $this->fb = new FizzBuzz(); } 8 9 10 11 12 13 14 15 public function testGetFizz() { $expected = 'Fizz'; $input = 3; $response = $this->fb->check($input); $this->assertEquals($expected, $response); } 16 17 18 19 20 21 22 23 public function testGetBuzz() { $expected = 'Buzz'; $input = 5; $response = $this->fb->check($input); $this->assertEquals($expected, $response); } Data Providers 39 24 public function testGetFizzBuzz() { $expected = 'FizzBuzz'; $input = 15; $response = $this->fb->check($input); $this->assertEquals($expected, $response); } 25 26 27 28 29 30 31 32 function testPassThru() { $expected = '1'; $input = 1; $response = $this->fb->check($input); $this->assertEquals($expected, $response); } 33 34 35 36 37 38 39 40 } I’m sure you can see the pattern: • multiple input values • tests that are extremely similar in setup and execution • same assertion being used over and over Creating Data Providers A data provider is another method inside your test class that returns an array of results, with each result set being an array itself.

Download PDF sample

The Grumpy Programmer's PHPUnit Cookbook by Chris Hartjes
Rated 4.52 of 5 – based on 15 votes