Browse Source

Add CakeRequest::isAll().

Provides a way to test multiple request types at the same time. And
assert that all pass. This replaces longer conditionals with a terser
syntax.

Refs #3714
mark_story 13 years ago
parent
commit
ce04d6afa1
2 changed files with 32 additions and 0 deletions
  1. 16 0
      lib/Cake/Network/CakeRequest.php
  2. 16 0
      lib/Cake/Test/Case/Network/CakeRequestTest.php

+ 16 - 0
lib/Cake/Network/CakeRequest.php

@@ -510,6 +510,22 @@ class CakeRequest implements ArrayAccess {
 	}
 
 /**
+ * Check that a request matches all the given types.
+ *
+ * Allows you to test multiple types and union the results.
+ * See CakeRequest::is() for how to add additional types and the
+ * built-in types.
+ *
+ * @param array $types The types to check.
+ * @return boolean Success.
+ * @see CakeRequest::is()
+ */
+	public function isAll(array $types) {
+		$result = array_filter(array_map(array($this, 'is'), $types));
+		return count($result) === count($types);
+	}
+
+/**
  * Add a new detector to the list of detectors that a request can use.
  * There are several different formats and types of detectors that can be set.
  *

+ 16 - 0
lib/Cake/Test/Case/Network/CakeRequestTest.php

@@ -742,6 +742,22 @@ class CakeRequestTest extends CakeTestCase {
 	}
 
 /**
+ * Test isAll()
+ *
+ * @return void
+ */
+	public function testIsAll() {
+		$request = new CakeRequest('some/path');
+
+		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
+		$_SERVER['REQUEST_METHOD'] = 'GET';
+
+		$this->assertTrue($request->isAll(array('ajax', 'get')));
+		$this->assertFalse($request->isAll(array('post', 'get')));
+		$this->assertFalse($request->isAll(array('ajax', 'post')));
+	}
+
+/**
  * test the method() method.
  *
  * @return void