Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Merge [770].

  • Loading branch information...
commit e9f96603bf59c2c837ff9659c04a792e5d9a1937 1 parent e00ffb1
@sebastianbergmann authored
View
106 PHPUnit/Framework/Assert.php
@@ -223,6 +223,98 @@ public static function assertAttributeNotContains($needle, $haystackAttributeNam
}
/**
+ * Asserts that a haystack contains only values of a given type.
+ *
+ * @param string $type
+ * @param mixed $haystack
+ * @param string $message
+ * @access public
+ * @static
+ * @since Method available since Release 3.1.4
+ */
+ public static function assertContainsOnly($type, $haystack, $message = '')
+ {
+ if (!(is_array($haystack) ||
+ is_object($haystack) && $haystack instanceof Iterator)) {
+ throw new InvalidArgumentException;
+ }
+
+ self::assertThat(
+ $haystack,
+ new PHPUnit_Framework_Constraint_TraversableContainsOnly($type),
+ $message
+ );
+ }
+
+ /**
+ * Asserts that a haystack that is stored in a static attribute of a class
+ * or an attribute of an object contains only values of a given type.
+ *
+ * @param string $type
+ * @param string $haystackAttributeName
+ * @param mixed $haystackClassOrObject
+ * @param string $message
+ * @access public
+ * @static
+ * @since Method available since Release 3.1.4
+ */
+ public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $message = '')
+ {
+ self::assertContainsOnly(
+ $type,
+ self::readAttribute($haystackClassOrObject, $haystackAttributeName),
+ $message
+ );
+ }
+
+ /**
+ * Asserts that a haystack does not contain only values of a given type.
+ *
+ * @param string $type
+ * @param mixed $haystack
+ * @param string $message
+ * @access public
+ * @static
+ * @since Method available since Release 3.1.4
+ */
+ public static function assertNotContainsOnly($type, $haystack, $message = '')
+ {
+ if (!(is_array($haystack) ||
+ is_object($haystack) && $haystack instanceof Iterator)) {
+ throw new InvalidArgumentException;
+ }
+
+ self::assertThat(
+ $haystack,
+ new PHPUnit_Framework_Constraint_Not(
+ new PHPUnit_Framework_Constraint_TraversableContainsOnly($type)
+ ),
+ $message
+ );
+ }
+
+ /**
+ * Asserts that a haystack that is stored in a static attribute of a class
+ * or an attribute of an object does not contain only values of a given type.
+ *
+ * @param string $type
+ * @param string $haystackAttributeName
+ * @param mixed $haystackClassOrObject
+ * @param string $message
+ * @access public
+ * @static
+ * @since Method available since Release 3.1.4
+ */
+ public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $message = '')
+ {
+ self::assertNotContainsOnly(
+ $type,
+ self::readAttribute($haystackClassOrObject, $haystackAttributeName),
+ $message
+ );
+ }
+
+ /**
* Asserts that two variables are equal.
*
* @param mixed $expected
@@ -1103,6 +1195,20 @@ public static function contains($value)
/**
*
*
+ * @param string $type
+ * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
+ * @access public
+ * @since Method available since Release 3.1.4
+ * @static
+ */
+ public static function containsOnly($type)
+ {
+ return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
+ }
+
+ /**
+ *
+ *
* @param mixed $key
* @return PHPUnit_Framework_Constraint_ArrayHasKey
* @access public
View
1  PHPUnit/Framework/Constraint.php
@@ -160,5 +160,6 @@ public static function negate($string)
require_once 'PHPUnit/Framework/Constraint/PCREMatch.php';
require_once 'PHPUnit/Framework/Constraint/StringContains.php';
require_once 'PHPUnit/Framework/Constraint/TraversableContains.php';
+require_once 'PHPUnit/Framework/Constraint/TraversableContainsOnly.php';
require_once 'PHPUnit/Framework/Constraint/Xor.php';
?>
View
106 PHPUnit/Framework/Constraint/TraversableContainsOnly.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2007, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version SVN: $Id$
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.1.4
+ */
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Util/Type.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ * Constraint that asserts that the Traversable it is applied to contains
+ * only values of a given type.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: @package_version@
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.1.4
+ */
+class PHPUnit_Framework_Constraint_TraversableContainsOnly extends PHPUnit_Framework_Constraint
+{
+ private $constraint;
+ private $type;
+
+ public function __construct($type)
+ {
+ $this->constraint = new PHPUnit_Framework_Constraint_IsType($type);
+ $this->type = $type;
+ }
+
+ /**
+ * Evaluates the constraint for parameter $other. Returns TRUE if the
+ * constraint is met, FALSE otherwise.
+ *
+ * @param mixed $other Value or object to evaluate.
+ * @return bool
+ */
+ public function evaluate($other)
+ {
+ foreach ($other as $item) {
+ if (!$this->constraint->evaluate($item)) {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+ }
+
+ /**
+ * Returns a string representation of the constraint.
+ *
+ * @return string
+ * @access public
+ */
+ public function toString()
+ {
+ return 'contains only values of type "' . $this->type . '"';
+ }
+}
+?>
View
30 PHPUnit/Tests/Framework/AssertTest.php
@@ -267,6 +267,36 @@ public function testAssertStringNotContainsString()
$this->fail();
}
+ public function testAssertArrayContainsOnlyIntegers()
+ {
+ $this->assertContainsOnly('integer', array(1, 2, 3));
+
+ try {
+ $this->assertContainsOnly('integer', array("1", 2, 3));
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
+ public function testAssertArrayNotContainsOnlyIntegers()
+ {
+ $this->assertNotContainsOnly('integer', array("1", 2, 3));
+
+ try {
+ $this->assertNotContainsOnly('integer', array(1, 2, 3));
+ }
+
+ catch (PHPUnit_Framework_AssertionFailedError $e) {
+ return;
+ }
+
+ $this->fail();
+ }
+
public function testAssertEqualsArray()
{
$this->assertEquals(array('a', 'b' => array(1, 2)), array('a', 'b' => array(1, 2)));
View
3  package.xml
@@ -138,6 +138,9 @@
<file baseinstalldir="/" name="TraversableContains.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="/" name="TraversableContainsOnly.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="/" name="Xor.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Please sign in to comment.
Something went wrong with that request. Please try again.