The Grumpy Programmer's PHPUnit Cookbook by Chris Hartjes

The Grumpy Programmer's PHPUnit Cookbook by Chris Hartjes

Author:Chris Hartjes
Language: eng
Format: epub, mobi, pdf
Publisher: leanpub.com
Published: 2014-01-20T16:00:00+00:00


More Object Testing Tricks

Testing Traits

Because Traits can be defined once, but used many times, you will not want to necessarily test the functionality defined in traits in every object in which they are consumed. At the same time, you do want to test the traits themselves.

PHPUnit 3.6 and newer offers functionality for mocking traits via the getObjectForTrait() method; this will return an object composing the trait, so that you can unit test only the trait itself.

Here’s a sample trait:

1 <?php 2 trait Registry 3 { 4 protected $values = array(); 5 6 public function set($key, $value) 7 { 8 $this->values[$key] = $value; 9 } 10 11 public function get($key) 12 { 13 if (!isset($this->values[$key])) { 14 return false; 15 } 16 17 return $this->values[$key]; 18 } 19 }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.