1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-06-02 12:04:57 +02:00
guzzle/tests/Guzzle/Tests/GuzzleTestCase.php
Michael Dowling 4c46e77015 Guzzle 2.0
Adopting composer for dependency management
Updating LICENSE, travis build file, making better use of git ignores, and remove unused build target
Removing @author tags.  Use the commit history for a changelog.
Moving files from build folder to /
Adding min build target to product a Guzzle only phar with no autoloader
[Common] Accepting ZF1 or ZF2 cache in ZendCacheAdapter
[Common] Optimizing Stream wrapper and EntityBody abstractions.
[Common] [Http] Migrating from Guzzle event system to the Symfony2 event dispatcher
[Common] Moved Inflector and Inspector to Service namespace
[Http] Simplifying Guzzle\Guzzle curl detection
[Http] Removing Guzzle\Http\Pool and now using Guzzle\Http\Curl\CurlMulti
[Http] The helper methods from Guzzle\Http\Message\RequestFactory have been removed to prevent confusion and encourage developers to use Guzzle\Http\Client to create requests.
[Http] Clients can now send one or more requests in an array using the send() method, so the batch() method was removed.
[Http] Updating curl multi to allow blocking calls while sending other transfers
[Http] Making the Request::hasHeader method more intuitive.  Guzzle\Http\Message\AbstractMessage::hasHeader() now returns true if the header is found using exact matching.  If the header is found using a regex or case-insensitive match, then it will return the name of the found header.
[Http] Removing content-type guessing from EntityBody based on file extension and solely using finfo.
[Http] Adding basic auth plugin
[Http] Cleaning up CookieJar and CurlMulti
[Http] Removing custom rawurlencode from QueryString because PHP 5.3 now properly deals with tilde characters.
[Http] Minor optimization to parsing messages in RequestFactory
[Http] Adding Guzzle\Http\Client for developers that don't need commands or service descriptions
[Http] Making it easier to set a global User-Agent header for a Guzzle\Http\Client
[Http] Fixing the discrepancies between the ClientInterface and Guzzle\Http\Client
[Http] Adding the ability to set and retrieve tokenized headers from Requests and Responses
[Service] Ditching NIH filters and using the Symfony2 validator
[Service] Moving most service building logic to the ServiceBuilder::factory method so that it is easier to build custom config readers.
[Service] Allowing deep nested command inheritance.
[Service] Cleaning up Inflector caching.
[Service] Getting rid of concept of can_batch because everything is now sent in parallel.
[Service] Adding a JSON description builder.
[Service] Cleaning up ResourceIteratorApplyBatched.
[Service] Removing caching stuff from ServiceBuilder because the data being cached is extremely fast to generate.
[Service] Added a method to serialize the ServiceDescription in case a ServiceDescription needs to be cached in an application.
[Service] Making description builders use static methods.
[Service] Adding support to include other description files for XML and JSON description builders.
[Service] Adding support for filters to ApiCommands
[Service] Using {{}} instead of $. to reference other services as a dependency for another service
2012-01-14 13:57:05 -06:00

216 lines
6.0 KiB
PHP

<?php
namespace Guzzle\Tests;
use Guzzle\Common\HasDispatcherInterface;
use Guzzle\Common\Log\Adapter\ZendLogAdapter;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Plugin\MockPlugin;
use Guzzle\Service\Client;
use Guzzle\Service\ServiceBuilder;
use Guzzle\Tests\Mock\MockObserver;
use Guzzle\Tests\Http\Server;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Base testcase class for all Guzzle testcases.
*/
abstract class GuzzleTestCase extends \PHPUnit_Framework_TestCase
{
protected static $mockBasePath;
public static $serviceBuilder;
public static $server;
private $requests = array();
public $mockObserver;
/**
* Get the global server object used throughout the unit tests of Guzzle
*
* @return Server
*/
public function getServer()
{
if (!self::$server) {
try {
self::$server = new Server();
if (self::$server->isRunning()) {
self::$server->flush();
} else {
self::$server->start();
}
} catch (\Exception $e) {
fwrite(STDERR, $e->getMessage());
}
}
return self::$server;
}
/**
* Set the service builder to use for tests
*
* @param ServiceBuilder $builder Service builder
*/
public static function setServiceBuilder(ServiceBuilder $builder)
{
self::$serviceBuilder = $builder;
}
/**
* Get a service builder object that can be used throughout the service tests
*
* @return ServiceBuilder
*/
public function getServiceBuilder()
{
if (!self::$serviceBuilder) {
throw new RuntimeException('No service builder has been set via setServiceBuilder()');
}
return self::$serviceBuilder;
}
/**
* Check if an event dispatcher has a subscriber
*
* @param HasDispatcherInterface $dispatcher
* @param EventSubscriberInterface $subscriber
*
* @return bool
*/
protected function hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber)
{
$class = get_class($subscriber);
$all = array_keys(call_user_func(array($class, 'getSubscribedEvents')));
foreach ($all as $i => $event) {
foreach ($dispatcher->getEventDispatcher()->getListeners($event) as $e) {
if ($e[0] === $subscriber) {
unset($all[$i]);
break;
}
}
}
return count($all) == 0;
}
/**
* Get a wildcard observer for an event dispatcher
*
* @param HasEventDispatcherInterface $hasEvent
*
* @return MockObserver
*/
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'));
}
return $o;
}
/**
* Set the mock response base path
*
* @param string $path Path to mock response folder
*
* @return GuzzleTestCase
*/
public static function setMockBasePath($path)
{
self::$mockBasePath = $path;
}
/**
* 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
*
* @param string $path Relative path to the mock response file
*
* @return Response
*/
public function getMockResponse($path)
{
return MockPlugin::getMockFile(self::$mockBasePath . DIRECTORY_SEPARATOR . $path);
}
/**
* 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
* @param string $paths Path to files within the Mock folder of the service
*/
public function setMockResponse(Client $client, $paths)
{
$this->requests = array();
$that = $this;
$mock = new MockPlugin(null, true);
$mock->getEventManager()->attach(function($subject, $event, $context) use ($that) {
if ($event == 'mock.request') {
$that->addMockedRequest($context);
}
});
foreach ((array) $paths as $path) {
$mock->addResponse($this->getMockResponse($path));
}
$client->getEventManager()->attach($mock, 9999);
}
/**
* 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;
}
}