$serviceBuilder
$serviceBuilder :
Base testcase class for all Guzzle testcases.
getServer() : \Guzzle\Tests\Http\Server
Get the global server object used throughout the unit tests of Guzzle
setServiceBuilder(\Guzzle\Service\Builder\ServiceBuilderInterface $builder)
Set the service builder to use for tests
\Guzzle\Service\Builder\ServiceBuilderInterface | $builder | Service builder |
getServiceBuilder() : \Guzzle\Service\Builder\ServiceBuilder
Get a service builder object that can be used throughout the service tests
getWildcardObserver(\Guzzle\Common\HasDispatcherInterface $hasDispatcher) : \Guzzle\Tests\Mock\MockObserver
Get a wildcard observer for an event dispatcher
\Guzzle\Common\HasDispatcherInterface | $hasDispatcher |
setMockBasePath(string $path) : \Guzzle\Tests\GuzzleTestCase
Set the mock response base path
string | $path | Path to mock response folder |
addMockedRequest(\Guzzle\Http\Message\RequestInterface $request) : self
Mark a request as being mocked
\Guzzle\Http\Message\RequestInterface | $request |
getMockResponse(string $path) : \Guzzle\Http\Message\Response
Get a mock response for a client by mock file name
string | $path | Relative path to the mock response file |
setMockResponse(\Guzzle\Http\Client $client, string $paths) : \Guzzle\Plugin\Mock\MockPlugin
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.
\Guzzle\Http\Client | $client | Client object to modify |
string | $paths | Path to files within the Mock folder of the service |
returns the created mock plugin
compareHeaders(array $filteredHeaders, array $actualHeaders) : array|boolean
Compare HTTP headers and use special markup to filter values A header prefixed with '!' means it must not exist A header prefixed with '_' means it must be ignored A header value of '*' means anything after the * will be ignored
array | $filteredHeaders | Array of special headers |
array | $actualHeaders | Array of headers to check against |
Returns an array of the differences or FALSE if none
hasSubscriber(\Guzzle\Common\HasDispatcherInterface $dispatcher, \Symfony\Component\EventDispatcher\EventSubscriberInterface $subscriber) : boolean
Check if an event dispatcher has a subscriber
\Guzzle\Common\HasDispatcherInterface | $dispatcher | |
\Symfony\Component\EventDispatcher\EventSubscriberInterface | $subscriber |
<?php
namespace Guzzle\Tests\Service\Resource;
use Guzzle\Service\Resource\ResourceIterator;
use Guzzle\Tests\Service\Mock\Model\MockCommandIterator;
/**
* @group server
* @covers Guzzle\Service\Resource\ResourceIterator
*/
class ResourceIteratorTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testDescribesEvents()
{
$this->assertInternalType('array', ResourceIterator::getAllEvents());
}
public function testConstructorConfiguresDefaults()
{
$ri = $this->getMockForAbstractClass('Guzzle\\Service\\Resource\\ResourceIterator', array(
$this->getServiceBuilder()->get('mock')->getCommand('iterable_command'),
array(
'limit' => 10,
'page_size' => 3
)
), 'MockIterator');
$this->assertEquals(false, $ri->getNextToken());
$this->assertEquals(false, $ri->current());
}
public function testSendsRequestsForNextSetOfResources()
{
// Queue up an array of responses for iterating
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 41\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"j\"] }"
));
// Create a new resource iterator using the IterableCommand mock
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array(
'page_size' => 3
));
// Ensure that no requests have been sent yet
$this->assertEquals(0, count($this->getServer()->getReceivedRequests(false)));
//$this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $ri->toArray());
$ri->toArray();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals(3, count($requests));
$this->assertEquals(3, $requests[0]->getQuery()->get('page_size'));
$this->assertEquals(3, $requests[1]->getQuery()->get('page_size'));
$this->assertEquals(3, $requests[2]->getQuery()->get('page_size'));
// Reset and resend
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 41\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"j\"] }",
));
$d = array();
foreach ($ri as $data) {
$d[] = $data;
}
$this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $d);
}
public function testCalculatesPageSize()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"g\", \"h\", \"i\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"j\", \"resources\": [\"j\", \"k\"] }"
));
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'), array(
'page_size' => 3,
'limit' => 7
));
$this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i', 'j'), $ri->toArray());
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals(3, count($requests));
$this->assertEquals(3, $requests[0]->getQuery()->get('page_size'));
$this->assertEquals(3, $requests[1]->getQuery()->get('page_size'));
$this->assertEquals(1, $requests[2]->getQuery()->get('page_size'));
}
public function testUseAsArray()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\nContent-Length: 52\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"g\", \"h\", \"i\"] }"
));
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'));
// Ensure that the key is never < 0
$this->assertEquals(0, $ri->key());
$this->assertEquals(0, count($ri));
// Ensure that the iterator can be used as KVP array
$data = array();
foreach ($ri as $key => $value) {
$data[$key] = $value;
}
// Ensure that the iterate is countable
$this->assertEquals(6, count($ri));
$this->assertEquals(array('d', 'e', 'f', 'g', 'h', 'i'), $data);
}
public function testBailsWhenSendReturnsNoResults()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"g\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [] }"
));
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'));
// Ensure that the iterator can be used as KVP array
$data = $ri->toArray();
// Ensure that the iterate is countable
$this->assertEquals(3, count($ri));
$this->assertEquals(array('d', 'e', 'f'), $data);
$this->assertEquals(2, $ri->getRequestCount());
}
public function testHoldsDataOptions()
{
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'));
$this->assertNull($ri->get('foo'));
$this->assertSame($ri, $ri->set('foo', 'bar'));
$this->assertEquals('bar', $ri->get('foo'));
}
public function testSettingLimitOrPageSizeClearsData()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }",
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }"
));
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'));
$ri->toArray();
$this->assertNotEmpty($this->readAttribute($ri, 'resources'));
$ri->setLimit(10);
$this->assertEmpty($this->readAttribute($ri, 'resources'));
$ri->toArray();
$this->assertNotEmpty($this->readAttribute($ri, 'resources'));
$ri->setPageSize(10);
$this->assertEmpty($this->readAttribute($ri, 'resources'));
}
public function testWorksWithCustomAppendIterator()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\n\r\n{ \"next_token\": \"\", \"resources\": [\"d\", \"e\", \"f\"] }"
));
$ri = new MockCommandIterator($this->getServiceBuilder()->get('mock')->getCommand('iterable_command'));
$a = new \Guzzle\Iterator\AppendIterator();
$a->append($ri);
$results = iterator_to_array($a, false);
$this->assertEquals(4, $ri->calledNext);
}
}