Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
760 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Mimic for PHP | ||
#(c) 2012 Andrew Coulton | ||
# Changelog | ||
|
||
## v0.6 | ||
|
||
### New features | ||
* Use .md extension for userguide links so that they can be read on github | ||
(requires latest version of Kohana 3.2 to browse in the userguide module) | ||
* Provides a base unittest testcase class for common assertions and requirements (see issue #4) | ||
* | ||
|
||
### Bugfixes | ||
* Explicitly sets Request content-length header if not present - fixes #5 | ||
* Only requires vfsStream to run Mimic's own unit tests - previously caused an | ||
error if trying to run other testcases in the project without vfsStream installed. | ||
|
||
## v0.5 | ||
|
||
* First version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Base class for test cases that use Mimic to test interaction with external web | ||
* services, handling set up and tear down of the Mimic request client and providing | ||
* suitable assertions as required. | ||
* | ||
* @package Mimic | ||
* @category TestInterface | ||
* @author Andrew Coulton | ||
* @copyright (c) 2011 Ingenerator | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
abstract class Mimic_Unittest_Testcase extends Unittest_TestCase | ||
{ | ||
/** | ||
* The current Mimic instance | ||
* @var Mimic | ||
*/ | ||
public $mimic = NULL; | ||
|
||
/** | ||
* If set, will load a default scenario in setUp | ||
* @var string | ||
*/ | ||
protected $_mimic_default_scenario = NULL; | ||
|
||
/** | ||
* Stores a reference to the Mimic instance, and resets requests for the | ||
* next test execution | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->mimic = Mimic::instance(); | ||
$this->mimic->reset_requests(); | ||
if ($this->_mimic_default_scenario) | ||
{ | ||
$this->mimic->load_scenario($this->_mimic_default_scenario); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that an expected number of requests were made | ||
* @param integer $expected | ||
*/ | ||
public function assertMimicRequestCount($expected) | ||
{ | ||
$this->assertEquals($expected, $this->mimic->request_count()); | ||
} | ||
|
||
/** | ||
* Asserts that the URL of the most recent request is equal to an expected value | ||
* @param string $expected | ||
*/ | ||
public function assertMimicLastRequestURL($expected) | ||
{ | ||
$this->assertEquals($expected, $this->mimic->last_request()->uri()); | ||
} | ||
|
||
/** | ||
* Asserts that the HTTP request method of the most recent request is equal to | ||
* an expected value | ||
* | ||
* @param string $expected | ||
*/ | ||
public function assertMimicLastRequestMethod($expected) | ||
{ | ||
$this->assertEquals($expected, $this->mimic->last_request()->method()); | ||
} | ||
|
||
/** | ||
* Asserts that the most recent request included a header with the given value | ||
* | ||
* @param string $header The header name | ||
* @param string $expected The expected value | ||
*/ | ||
public function assertMimicLastRequestHeader($header, $expected) | ||
{ | ||
$this->assertEquals($expected, $this->mimic->last_request()->headers($header)); | ||
} | ||
|
||
/** | ||
* Asserts that the most recent request included an expected $_GET parameter | ||
* | ||
* @param string $key | ||
* @param string $expected | ||
*/ | ||
public function assertMimicLastRequestQuery($key, $expected) | ||
{ | ||
$this->assertEquals($expected, $this->mimic->last_request()->query($key)); | ||
} | ||
|
||
/** | ||
* Asserts that the request body of the most recent request was as expected | ||
* @param string $expected | ||
*/ | ||
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"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
## [Mimic]() | ||
- [Recording](recording) | ||
- [Matching Requests](matching) | ||
- [Customising Responses](customising) | ||
- [Testing with Mimic](testing) | ||
- [Updating definitions](updating) | ||
- [Configuration](config) | ||
- [Response Formatters](formatters) | ||
- [Recording](recording.md) | ||
- [Matching Requests](matching.md) | ||
- [Customising Responses](customising.md) | ||
- [Testing with Mimic](testing.md) | ||
- [Updating definitions](updating.md) | ||
- [Configuration](config.md) | ||
- [Response Formatters](formatters.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.