1<?php 2/* 3 * This file is part of PHPUnit. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11/** 12 * Constraint that checks whether a variable is empty(). 13 */ 14class PHPUnit_Framework_Constraint_IsEmpty extends PHPUnit_Framework_Constraint 15{ 16 /** 17 * Evaluates the constraint for parameter $other. Returns true if the 18 * constraint is met, false otherwise. 19 * 20 * @param mixed $other Value or object to evaluate. 21 * 22 * @return bool 23 */ 24 protected function matches($other) 25 { 26 if ($other instanceof Countable) { 27 return count($other) === 0; 28 } 29 30 return empty($other); 31 } 32 33 /** 34 * Returns a string representation of the constraint. 35 * 36 * @return string 37 */ 38 public function toString() 39 { 40 return 'is empty'; 41 } 42 43 /** 44 * Returns the description of the failure 45 * 46 * The beginning of failure messages is "Failed asserting that" in most 47 * cases. This method should return the second part of that sentence. 48 * 49 * @param mixed $other Evaluated value or object. 50 * 51 * @return string 52 */ 53 protected function failureDescription($other) 54 { 55 $type = gettype($other); 56 57 return sprintf( 58 '%s %s %s', 59 $type[0] == 'a' || $type[0] == 'o' ? 'an' : 'a', 60 $type, 61 $this->toString() 62 ); 63 } 64} 65