Open Journal Systems  3.3.0
GuzzleTestCase.php
1 <?php
2 
3 namespace Guzzle\Tests;
4 
16 use RuntimeException;
18 
22 abstract class GuzzleTestCase extends \PHPUnit_Framework_TestCase
23 {
24  protected static $mockBasePath;
25  public static $serviceBuilder;
26  public static $server;
27 
28  private $requests = array();
29  public $mockObserver;
30 
36  public static function getServer()
37  {
38  if (!self::$server) {
39  self::$server = new Server();
40  if (self::$server->isRunning()) {
41  self::$server->flush();
42  } else {
43  self::$server->start();
44  }
45  }
46 
47  return self::$server;
48  }
49 
55  public static function setServiceBuilder(ServiceBuilderInterface $builder)
56  {
57  self::$serviceBuilder = $builder;
58  }
59 
65  public static function getServiceBuilder()
66  {
67  if (!self::$serviceBuilder) {
68  throw new RuntimeException('No service builder has been set via setServiceBuilder()');
69  }
70 
71  return self::$serviceBuilder;
72  }
73 
82  protected function hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber)
83  {
84  $class = get_class($subscriber);
85  $all = array_keys(call_user_func(array($class, 'getSubscribedEvents')));
86 
87  foreach ($all as $i => $event) {
88  foreach ($dispatcher->getEventDispatcher()->getListeners($event) as $e) {
89  if ($e[0] === $subscriber) {
90  unset($all[$i]);
91  break;
92  }
93  }
94  }
95 
96  return count($all) == 0;
97  }
98 
106  public function getWildcardObserver(HasDispatcherInterface $hasDispatcher)
107  {
108  $class = get_class($hasDispatcher);
109  $o = new MockObserver();
110  $events = call_user_func(array($class, 'getAllEvents'));
111  foreach ($events as $event) {
112  $hasDispatcher->getEventDispatcher()->addListener($event, array($o, 'update'));
113  }
114 
115  return $o;
116  }
117 
125  public static function setMockBasePath($path)
126  {
127  self::$mockBasePath = $path;
128  }
129 
137  public function addMockedRequest(RequestInterface $request)
138  {
139  $this->requests[] = $request;
140 
141  return $this;
142  }
143 
149  public function getMockedRequests()
150  {
151  return $this->requests;
152  }
153 
161  public function getMockResponse($path)
162  {
163  return $path instanceof Response
164  ? $path
165  : MockPlugin::getMockFile(self::$mockBasePath . DIRECTORY_SEPARATOR . $path);
166  }
167 
181  public function setMockResponse(Client $client, $paths)
182  {
183  $this->requests = array();
184  $that = $this;
185  $mock = new MockPlugin(null, true);
186  $client->getEventDispatcher()->removeSubscriber($mock);
187  $mock->getEventDispatcher()->addListener('mock.request', function(Event $event) use ($that) {
188  $that->addMockedRequest($event['request']);
189  });
190 
191  if ($paths instanceof Response) {
192  // A single response instance has been specified, create an array with that instance
193  // as the only element for the following loop to work as expected
194  $paths = array($paths);
195  }
196 
197  foreach ((array) $paths as $path) {
198  $mock->addResponse($this->getMockResponse($path));
199  }
200 
201  $client->getEventDispatcher()->addSubscriber($mock);
202 
203  return $mock;
204  }
205 
217  public function compareHeaders($filteredHeaders, $actualHeaders)
218  {
219  $comparison = new HeaderComparison();
220 
221  return $comparison->compare($filteredHeaders, $actualHeaders);
222  }
223 
231  public function assertContainsIns($needle, $haystack, $message = null)
232  {
233  $this->assertContains(strtolower($needle), strtolower($haystack), $message);
234  }
235 }
Guzzle\Tests\GuzzleTestCase\getServiceBuilder
static getServiceBuilder()
Definition: GuzzleTestCase.php:65
Guzzle\Tests\GuzzleTestCase\compareHeaders
compareHeaders($filteredHeaders, $actualHeaders)
Definition: GuzzleTestCase.php:217
Guzzle\Tests\GuzzleTestCase\setServiceBuilder
static setServiceBuilder(ServiceBuilderInterface $builder)
Definition: GuzzleTestCase.php:55
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Tests\GuzzleTestCase\$mockBasePath
static $mockBasePath
Definition: GuzzleTestCase.php:24
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\GuzzleTestCase\getMockResponse
getMockResponse($path)
Definition: GuzzleTestCase.php:161
Guzzle\Tests\GuzzleTestCase\setMockBasePath
static setMockBasePath($path)
Definition: GuzzleTestCase.php:125
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Tests\GuzzleTestCase\getMockedRequests
getMockedRequests()
Definition: GuzzleTestCase.php:149
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\GuzzleTestCase\$serviceBuilder
static $serviceBuilder
Definition: GuzzleTestCase.php:25
Guzzle\Plugin\Mock\MockPlugin\getMockFile
static getMockFile($path)
Definition: MockPlugin.php:82
Guzzle\Tests\GuzzleTestCase\$mockObserver
$mockObserver
Definition: GuzzleTestCase.php:29
Guzzle\Common\HasDispatcherInterface
Definition: HasDispatcherInterface.php:11
Guzzle\Tests\GuzzleTestCase\hasSubscriber
hasSubscriber(HasDispatcherInterface $dispatcher, EventSubscriberInterface $subscriber)
Definition: GuzzleTestCase.php:82
Guzzle\Tests\Http\Message\HeaderComparison
Definition: HeaderComparison.php:11
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Tests\GuzzleTestCase\addMockedRequest
addMockedRequest(RequestInterface $request)
Definition: GuzzleTestCase.php:137
Guzzle\Tests\Http\Server
Definition: Server.php:27
Guzzle\Common\HasDispatcherInterface\getEventDispatcher
getEventDispatcher()
Guzzle\Tests\GuzzleTestCase\$server
static $server
Definition: GuzzleTestCase.php:26
Guzzle\Tests\Mock\MockObserver
Definition: MockObserver.php:8
Guzzle\Tests\GuzzleTestCase\assertContainsIns
assertContainsIns($needle, $haystack, $message=null)
Definition: GuzzleTestCase.php:231
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Plugin\Mock\MockPlugin
Definition: MockPlugin.php:17
Guzzle\Service\Builder\ServiceBuilder
Definition: ServiceBuilder.php:16
Guzzle\Tests
Guzzle\Service\Builder\ServiceBuilderInterface
Definition: ServiceBuilderInterface.php:13
Guzzle\Tests\GuzzleTestCase\getServer
static getServer()
Definition: GuzzleTestCase.php:36
Guzzle\Tests\GuzzleTestCase\setMockResponse
setMockResponse(Client $client, $paths)
Definition: GuzzleTestCase.php:181
Guzzle\Tests\GuzzleTestCase\getWildcardObserver
getWildcardObserver(HasDispatcherInterface $hasDispatcher)
Definition: GuzzleTestCase.php:106