3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Constraint that asserts that the Traversable it is applied to contains
13 * only values of a given type.
15 * @since Class available since Release 3.1.4
17 class PHPUnit_Framework_Constraint_TraversableContainsOnly extends PHPUnit_Framework_Constraint
20 * @var PHPUnit_Framework_Constraint
22 protected $constraint;
31 * @param bool $isNativeType
33 public function __construct($type, $isNativeType = true)
35 parent::__construct();
38 $this->constraint = new PHPUnit_Framework_Constraint_IsType($type);
40 $this->constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
49 * Evaluates the constraint for parameter $other
51 * If $returnResult is set to false (the default), an exception is thrown
52 * in case of a failure. null is returned otherwise.
54 * If $returnResult is true, the result of the evaluation is returned as
55 * a boolean value instead: true in case of success, false in case of a
58 * @param mixed $other Value or object to evaluate.
59 * @param string $description Additional information about the test
60 * @param bool $returnResult Whether to return a result or throw an exception
64 * @throws PHPUnit_Framework_ExpectationFailedException
66 public function evaluate($other, $description = '', $returnResult = false)
70 foreach ($other as $item) {
71 if (!$this->constraint->evaluate($item, '', true)) {
82 $this->fail($other, $description);
87 * Returns a string representation of the constraint.
91 public function toString()
93 return 'contains only values of type "' . $this->type . '"';