Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added AssertRequestsContains and AssertRequestsNotContains - closes #4

  • Loading branch information...
commit 6303b2aa3392467bafde2bef1a550957c6b4fcee 1 parent 2c7cf58
@acoulton authored
Showing with 148 additions and 0 deletions.
  1. +60 −0 classes/mimic/unittest/testcase.php
  2. +88 −0 tests/mimic/TestCaseTest.php
View
60 classes/mimic/unittest/testcase.php
@@ -100,4 +100,64 @@ public function assertMimicLastRequestBody($expected)
$this->assertEquals($expected, $this->mimic->last_request()->body());
}
+ /**
+ * Searches the request history for a request to the given URL and (optionally)
+ * with the specified method, returning TRUE or FALSE.
+ *
+ * @param string $url
+ * @param string $method
+ * @return boolean
+ */
+ protected function _search_request_history($url, $method)
+ {
+ foreach ($this->mimic->request_history() as $request)
+ {
+ if (($method !== NULL) AND ($method !== $request->method()))
+ {
+ continue;
+ }
+
+ if ($request->uri() === $url)
+ {
+ return TRUE;
+ }
+ }
+
+ // Nothing found
+ return FALSE;
+
+ }
+
+ /**
+ * Verify that a request (optionally with a specified method) was made to the
+ * given URL at some point in execution - for requests where the sequence doesn't
+ * matter, this is obviously less brittle than asserting a specific request.
+ *
+ * @param string $url
+ * @param string $method
+ */
+ public function assertMimicRequestsContains($url, $method = NULL)
+ {
+ if ( ! $this->_search_request_history($url, $method))
+ {
+ $this->fail("Expected $method request to $url and none was made");
+ }
+ }
+
+ /**
+ * Verify that a request (optionally with a specified method) was not made to
+ * the given URL at any point in execution - for requests where the sequence doesn't
+ * matter, this is obviously less brittle than asserting a specific request.
+ *
+ * @param string $url
+ * @param string $method
+ */
+ public function assertMimicRequestsNotContains($url, $method = NULL)
+ {
+ if ($this->_search_request_history($url, $method))
+ {
+ $this->fail("A $method request was made to $url");
+ }
+ }
+
}
View
88 tests/mimic/TestCaseTest.php
@@ -88,6 +88,15 @@ protected function _test_assertion($testcase, $method, $args, $should_pass)
}
return;
}
+ catch (PHPUnit_Framework_AssertionFailedError $e)
+ {
+ // Check that this is expected to fail
+ if ($should_pass)
+ {
+ $this->fail('Unexpected assertion failure with message '.$e->getMessage());
+ }
+ return;
+ }
// Check that it is expected to pass
if ( ! $should_pass)
@@ -282,6 +291,85 @@ public function test_should_assert_last_request_body($mock_body, $expected, $sho
$this->_test_assertion($testcase, 'assertMimicLastRequestBody', array($expected), $should_pass);
}
+ public function provider_should_assert_or_not_requests_contains()
+ {
+ $requests = array(
+ array('method'=>'GET', 'uri' => 'http://foo.bar.com/foo'),
+ array('method'=>'POST', 'uri' => 'http://foo.bar.com/foo'),
+ array('method'=>'GET', 'uri' => 'http://foo.bar.com/bar'),
+ );
+ return array(
+ array($requests, 'http://foo.bar.com/foo', 'POST', TRUE),
+ array($requests, 'http://foo.bar.com/foo', NULL, TRUE),
+ array($requests, 'http://foo.bar.com/foo', 'PUT', FALSE),
+ array($requests, 'http://foo.bar.com/bar', 'GET', TRUE),
+ );
+ }
+
+ /**
+ * Helper method, as the test strap for assertMimicRequestsContains and
+ * assertMimicRequestsNotContains is very similar.
+ *
+ * @param string $method
+ * @param array $mock_requests
+ * @param string $test_url
+ * @param string $test_method
+ * @param string $should_pass
+ */
+ protected function _test_should_or_not_contain($method, $mock_requests, $test_url, $test_method, $should_pass)
+ {
+ // Build an array of mock requests for the stack
+ $request_history = array();
+ foreach ($mock_requests as $mock_request)
+ {
+ $request = $this->_mock_request();
+
+ // Request::uri() will only be called if the method matches the search
+ $request->expects($this->any())
+ ->method('uri')
+ ->will($this->returnValue($mock_request['uri']));
+
+ // Request::method() will only be called if matching on it
+ $request->expects($this->any())
+ ->method('method')
+ ->will($this->returnValue($mock_request['method']));
+ $request_history[] = $request;
+ }
+
+ // Mock the testcase
+ $testcase = $this->_testcase_with_mock_mimic('request_history', $request_history);
+
+ // Call and test the assertion
+ $this->_test_assertion($testcase, $method, array($test_url, $test_method), $should_pass);
+ }
+
+ /**
+ * @dataProvider provider_should_assert_or_not_requests_contains
+ * @param array $mock_requests
+ * @param string $test_url
+ * @param string $test_method
+ * @param boolean $should_contain Whether the stack should contain the request
+ */
+ public function test_should_assert_requests_contains($mock_requests, $test_url, $test_method, $should_contain)
+ {
+ $this->_test_should_or_not_contain('assertMimicRequestsContains',
+ $mock_requests, $test_url, $test_method, $should_contain);
+ }
+
+ /**
+ * @dataProvider provider_should_assert_or_not_requests_contains
+ * @param array $mock_requests
+ * @param string $test_url
+ * @param string $test_method
+ * @param boolean $should_contain Whether the stack should contain the request
+ */
+ public function test_should_assert_requests_not_contains($mock_requests, $test_url, $test_method, $should_contain)
+ {
+ $should_pass = ! $should_contain;
+ $this->_test_should_or_not_contain('assertMimicRequestsNotContains',
+ $mock_requests, $test_url, $test_method, $should_pass);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.