Skip to content

Commit

Permalink
Merge pull request #42 from troydavisson/matcher-fix
Browse files Browse the repository at this point in the history
Fix for request matcher missing query string differences
  • Loading branch information
adri committed Feb 19, 2014
2 parents 2a9c3fc + e222920 commit f1be864
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -123,6 +123,7 @@ phpunit ./tests

## Changelog

* 2014-02-19 Release 1.0.7: Adds query request matcher.
* 2014-01-12 Release 1.0.6: Updates dependencies.
* 2013-10-13 Release 1.0.5: Fixed SOAP support, refactorings.
* 2013-07-22 Release 1.0.4: Updates dependencies.
Expand Down
13 changes: 7 additions & 6 deletions src/VCR/Configuration.php
Expand Up @@ -28,12 +28,13 @@ class Configuration
// All are enabled by default
private $enabledRequestMatchers;
private $availableRequestMatchers = array(
'method' => array('VCR\RequestMatcher', 'matchMethod'),
'url' => array('VCR\RequestMatcher', 'matchUrl'),
'host' => array('VCR\RequestMatcher', 'matchHost'),
'headers' => array('VCR\RequestMatcher', 'matchHeaders'),
'body' => array('VCR\RequestMatcher', 'matchBody'),
'post_fields' => array('VCR\RequestMatcher', 'matchPostFields'),
'method' => array('VCR\RequestMatcher', 'matchMethod'),
'url' => array('VCR\RequestMatcher', 'matchUrl'),
'host' => array('VCR\RequestMatcher', 'matchHost'),
'headers' => array('VCR\RequestMatcher', 'matchHeaders'),
'body' => array('VCR\RequestMatcher', 'matchBody'),
'post_fields' => array('VCR\RequestMatcher', 'matchPostFields'),
'query_string' => array('VCR\RequestMatcher', 'matchQueryString'),
);
private $whiteList = array();
private $blackList = array('src/VCR/LibraryHooks/', 'src/VCR/Util/Soap/SoapClient');
Expand Down
7 changes: 7 additions & 0 deletions src/VCR/RequestMatcher.php
Expand Up @@ -61,4 +61,11 @@ public static function matchPostFields(Request $first, Request $second)
return true;
}

public static function matchQueryString(Request $first, Request $second)
{
if (null !== $first->getQuery(true) && $first->getQuery(true) != $second->getQuery(true)) {
return false;
}
return true;
}
}
102 changes: 102 additions & 0 deletions tests/VCR/RequestMatcherTest.php
@@ -0,0 +1,102 @@
<?php

namespace VCR;

class RequestMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testMatchingMethod()
{
$first = new Request('GET', 'http://example.com', array());
$second = new Request('GET', 'http://example.com', array());

$this->assertTrue(RequestMatcher::matchMethod($first, $second));

$first = new Request('GET', 'http://example.com', array());
$second = new Request('POST', 'http://example.com', array());

$this->assertFalse(RequestMatcher::matchMethod($first, $second));
}

public function testMatchingUrl()
{
$first = new Request('GET', 'http://example.com/common/path', array());
$second = new Request('GET', 'http://example.com/common/path', array());

$this->assertTrue(RequestMatcher::matchUrl($first, $second));

$first = new Request('GET', 'http://example.com/first/path', array());
$second = new Request('GET', 'http://example.com/second/path', array());

$this->assertFalse(RequestMatcher::matchUrl($first, $second));
}

public function testMatchingHost()
{
$first = new Request('GET', 'http://example.com/common/path', array());
$second = new Request('GET', 'http://example.com/common/path', array());

$this->assertTrue(RequestMatcher::matchHost($first, $second));

$first = new Request('GET', 'http://example.com/first/path', array());
$second = new Request('GET', 'http://elpmaxe.com/second/path', array());

$this->assertFalse(RequestMatcher::matchHost($first, $second));
}

public function testMatchingHeaders()
{
$first = new Request('GET', 'http://example.com', array(
'Accept' => 'Everything',
));
$second = new Request('GET', 'http://example.com', array(
'Accept' => 'Everything',
));

$this->assertTrue(RequestMatcher::matchHeaders($first, $second));

$first = new Request('GET', 'http://example.com', array(
'Accept' => 'Everything',
));
$second = new Request('GET', 'http://example.com', array(
'Accept' => 'Nothing',
));

$this->assertFalse(RequestMatcher::matchHeaders($first, $second));
}

public function testMatchingPostFields()
{
$mock = array(
'method' => 'POST',
'url' => 'http://example.com',
'headers' => array(),
'post_fields' => array(
'field1' => 'value1',
'field2' => 'value2',
)
);

$first = Request::fromArray($mock);
$second = Request::fromArray($mock);

$this->assertTrue(RequestMatcher::matchPostFields($first, $second));

$mock['post_fields']['field2'] = 'changedvalue2';
$third = Request::fromArray($mock);

$this->assertFalse(RequestMatcher::matchPostFields($first, $third));
}

public function testMatchingQueryString()
{
$first = new Request('GET', 'http://example.com/search?query=test', array());
$second = new Request('GET', 'http://example.com/search?query=test', array());

$this->assertTrue(RequestMatcher::matchQueryString($first, $second));

$first = new Request('GET', 'http://example.com/search?query=first', array());
$second = new Request('GET', 'http://example.com/search?query=second', array());

$this->assertFalse(RequestMatcher::matchQueryString($first, $second));
}
}

0 comments on commit f1be864

Please sign in to comment.