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 * Logical AND. 13 */ 14class PHPUnit_Framework_Constraint_And extends PHPUnit_Framework_Constraint 15{ 16 /** 17 * @var PHPUnit_Framework_Constraint[] 18 */ 19 protected $constraints = []; 20 21 /** 22 * @var PHPUnit_Framework_Constraint 23 */ 24 protected $lastConstraint = null; 25 26 /** 27 * @param PHPUnit_Framework_Constraint[] $constraints 28 * 29 * @throws PHPUnit_Framework_Exception 30 */ 31 public function setConstraints(array $constraints) 32 { 33 $this->constraints = []; 34 35 foreach ($constraints as $constraint) { 36 if (!($constraint instanceof PHPUnit_Framework_Constraint)) { 37 throw new PHPUnit_Framework_Exception( 38 'All parameters to ' . __CLASS__ . 39 ' must be a constraint object.' 40 ); 41 } 42 43 $this->constraints[] = $constraint; 44 } 45 } 46 47 /** 48 * Evaluates the constraint for parameter $other 49 * 50 * If $returnResult is set to false (the default), an exception is thrown 51 * in case of a failure. null is returned otherwise. 52 * 53 * If $returnResult is true, the result of the evaluation is returned as 54 * a boolean value instead: true in case of success, false in case of a 55 * failure. 56 * 57 * @param mixed $other Value or object to evaluate. 58 * @param string $description Additional information about the test 59 * @param bool $returnResult Whether to return a result or throw an exception 60 * 61 * @return mixed 62 * 63 * @throws PHPUnit_Framework_ExpectationFailedException 64 */ 65 public function evaluate($other, $description = '', $returnResult = false) 66 { 67 $success = true; 68 $constraint = null; 69 70 foreach ($this->constraints as $constraint) { 71 if (!$constraint->evaluate($other, $description, true)) { 72 $success = false; 73 break; 74 } 75 } 76 77 if ($returnResult) { 78 return $success; 79 } 80 81 if (!$success) { 82 $this->fail($other, $description); 83 } 84 } 85 86 /** 87 * Returns a string representation of the constraint. 88 * 89 * @return string 90 */ 91 public function toString() 92 { 93 $text = ''; 94 95 foreach ($this->constraints as $key => $constraint) { 96 if ($key > 0) { 97 $text .= ' and '; 98 } 99 100 $text .= $constraint->toString(); 101 } 102 103 return $text; 104 } 105 106 /** 107 * Counts the number of constraint elements. 108 * 109 * @return int 110 */ 111 public function count() 112 { 113 $count = 0; 114 115 foreach ($this->constraints as $constraint) { 116 $count += count($constraint); 117 } 118 119 return $count; 120 } 121} 122