2011-02-27 22:32:13 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Guzzle\Tests;
|
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
use Guzzle\Common\HasDispatcherInterface;
|
2012-01-16 11:14:41 -06:00
|
|
|
use Guzzle\Common\Event;
|
2011-02-27 22:32:13 -06:00
|
|
|
use Guzzle\Common\Log\Adapter\ZendLogAdapter;
|
|
|
|
use Guzzle\Http\Message\Response;
|
|
|
|
use Guzzle\Http\Message\RequestInterface;
|
2012-01-14 13:57:05 -06:00
|
|
|
use Guzzle\Http\Plugin\MockPlugin;
|
2011-02-27 22:32:13 -06:00
|
|
|
use Guzzle\Service\Client;
|
2011-03-09 17:09:40 -06:00
|
|
|
use Guzzle\Service\ServiceBuilder;
|
2012-01-14 13:57:05 -06:00
|
|
|
use Guzzle\Tests\Mock\MockObserver;
|
2011-11-10 15:18:35 -06:00
|
|
|
use Guzzle\Tests\Http\Server;
|
2011-11-01 22:30:39 -05:00
|
|
|
use RuntimeException;
|
2012-01-14 13:57:05 -06:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2011-02-27 22:32:13 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base testcase class for all Guzzle testcases.
|
|
|
|
*/
|
|
|
|
abstract class GuzzleTestCase extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-11-10 15:18:35 -06:00
|
|
|
protected static $mockBasePath;
|
2011-02-27 22:32:13 -06:00
|
|
|
public static $serviceBuilder;
|
|
|
|
public static $server;
|
2011-11-10 15:18:35 -06:00
|
|
|
|
|
|
|
private $requests = array();
|
2011-03-08 10:04:48 -06:00
|
|
|
public $mockObserver;
|
2011-02-27 22:32:13 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the global server object used throughout the unit tests of Guzzle
|
|
|
|
*
|
|
|
|
* @return Server
|
|
|
|
*/
|
|
|
|
public function getServer()
|
|
|
|
{
|
|
|
|
if (!self::$server) {
|
2012-01-14 13:57:05 -06:00
|
|
|
try {
|
2012-01-16 11:14:41 -06:00
|
|
|
self::$server = new Server();
|
|
|
|
if (self::$server->isRunning()) {
|
|
|
|
self::$server->flush();
|
|
|
|
} else {
|
|
|
|
self::$server->start();
|
|
|
|
}
|
2012-01-14 13:57:05 -06:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
fwrite(STDERR, $e->getMessage());
|
|
|
|
}
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-01 22:30:39 -05:00
|
|
|
* Set the service builder to use for tests
|
2011-02-27 22:32:13 -06:00
|
|
|
*
|
2011-11-01 22:30:39 -05:00
|
|
|
* @param ServiceBuilder $builder Service builder
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
2011-11-01 22:30:39 -05:00
|
|
|
public static function setServiceBuilder(ServiceBuilder $builder)
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
2011-11-01 22:30:39 -05:00
|
|
|
self::$serviceBuilder = $builder;
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a service builder object that can be used throughout the service tests
|
|
|
|
*
|
|
|
|
* @return ServiceBuilder
|
|
|
|
*/
|
|
|
|
public function getServiceBuilder()
|
|
|
|
{
|
|
|
|
if (!self::$serviceBuilder) {
|
2011-11-01 22:30:39 -05:00
|
|
|
throw new RuntimeException('No service builder has been set via setServiceBuilder()');
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$serviceBuilder;
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
/**
|
|
|
|
* Check if an event dispatcher has a subscriber
|
2012-01-16 11:14:41 -06:00
|
|
|
*
|
2012-01-14 13:57:05 -06:00
|
|
|
* @param HasDispatcherInterface $dispatcher
|
|
|
|
* @param EventSubscriberInterface $subscriber
|
2012-01-16 11:14:41 -06:00
|
|
|
*
|
2012-01-14 13:57:05 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber)
|
|
|
|
{
|
|
|
|
$class = get_class($subscriber);
|
|
|
|
$all = array_keys(call_user_func(array($class, 'getSubscribedEvents')));
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
foreach ($all as $i => $event) {
|
|
|
|
foreach ($dispatcher->getEventDispatcher()->getListeners($event) as $e) {
|
|
|
|
if ($e[0] === $subscriber) {
|
|
|
|
unset($all[$i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
return count($all) == 0;
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
/**
|
|
|
|
* Get a wildcard observer for an event dispatcher
|
2012-01-16 11:14:41 -06:00
|
|
|
*
|
2012-01-14 13:57:05 -06:00
|
|
|
* @param HasEventDispatcherInterface $hasEvent
|
2012-01-16 11:14:41 -06:00
|
|
|
*
|
|
|
|
* @return MockObserver
|
2012-01-14 13:57:05 -06:00
|
|
|
*/
|
|
|
|
public function getWildcardObserver(HasDispatcherInterface $hasDispatcher)
|
|
|
|
{
|
|
|
|
$class = get_class($hasDispatcher);
|
|
|
|
$o = new MockObserver();
|
|
|
|
$events = call_user_func(array($class, 'getAllEvents'));
|
|
|
|
foreach ($events as $event) {
|
|
|
|
$hasDispatcher->getEventDispatcher()->addListener($event, array($o, 'update'));
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
return $o;
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
2011-11-10 15:18:35 -06:00
|
|
|
/**
|
|
|
|
* Set the mock response base path
|
|
|
|
*
|
|
|
|
* @param string $path Path to mock response folder
|
|
|
|
*
|
|
|
|
* @return GuzzleTestCase
|
|
|
|
*/
|
|
|
|
public static function setMockBasePath($path)
|
|
|
|
{
|
|
|
|
self::$mockBasePath = $path;
|
|
|
|
}
|
|
|
|
|
2011-02-27 22:32:13 -06:00
|
|
|
/**
|
|
|
|
* Mark a request as being mocked
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
*/
|
|
|
|
public function addMockedRequest(RequestInterface $request)
|
|
|
|
{
|
|
|
|
$this->requests[] = $request;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the mocked requests
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMockedRequests()
|
|
|
|
{
|
|
|
|
return $this->requests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a mock response for a client by mock file name
|
|
|
|
*
|
2011-11-10 15:18:35 -06:00
|
|
|
* @param string $path Relative path to the mock response file
|
2011-02-27 22:32:13 -06:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2011-11-10 15:18:35 -06:00
|
|
|
public function getMockResponse($path)
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
2011-11-10 15:18:35 -06:00
|
|
|
return MockPlugin::getMockFile(self::$mockBasePath . DIRECTORY_SEPARATOR . $path);
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a mock response from a mock file on the next client request.
|
|
|
|
*
|
|
|
|
* This method assumes that mock response files are located under the
|
|
|
|
* Command/Mock/ directory of the Service being tested
|
|
|
|
* (e.g. Unfuddle/Command/Mock/). A mock response is added to the next
|
|
|
|
* request sent by the client.
|
|
|
|
*
|
|
|
|
* @param Client $client Client object to modify
|
2011-11-10 15:18:35 -06:00
|
|
|
* @param string $paths Path to files within the Mock folder of the service
|
2011-02-27 22:32:13 -06:00
|
|
|
*/
|
2011-11-10 15:18:35 -06:00
|
|
|
public function setMockResponse(Client $client, $paths)
|
2011-02-27 22:32:13 -06:00
|
|
|
{
|
|
|
|
$this->requests = array();
|
|
|
|
$that = $this;
|
2012-01-14 13:57:05 -06:00
|
|
|
$mock = new MockPlugin(null, true);
|
2012-01-16 11:14:41 -06:00
|
|
|
$mock->getEventDispatcher()->addListener('mock.request', function(Event $event) use ($that) {
|
|
|
|
$that->addMockedRequest($event['request']);
|
2011-11-10 15:18:35 -06:00
|
|
|
});
|
2011-02-27 22:32:13 -06:00
|
|
|
|
2011-11-10 15:18:35 -06:00
|
|
|
foreach ((array) $paths as $path) {
|
|
|
|
$mock->addResponse($this->getMockResponse($path));
|
|
|
|
}
|
2012-01-16 11:14:41 -06:00
|
|
|
|
|
|
|
$client->getEventDispatcher()->addSubscriber($mock);
|
2011-02-27 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if an array of HTTP headers matches another array of HTTP headers
|
|
|
|
* while taking * into account as a wildcard for header values
|
|
|
|
*
|
|
|
|
* @param array $actual Actual HTTP header array
|
|
|
|
* @param array $expected Expected HTTP headers (allows wildcard values)
|
|
|
|
*
|
|
|
|
* @return array|false Returns an array of the differences or FALSE if none
|
|
|
|
*/
|
|
|
|
public function compareHttpHeaders(array $expected, array $actual)
|
|
|
|
{
|
|
|
|
$differences = array();
|
|
|
|
|
|
|
|
foreach ($actual as $key => $value) {
|
|
|
|
if (!isset($expected[$key]) || ($expected[$key] != '*' && $actual[$key] != $expected[$key])) {
|
|
|
|
$differences[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return empty($differences) ? false : $differences;
|
|
|
|
}
|
|
|
|
}
|