1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141:
<?php
namespace Guzzle\Tests\Plugin\History;
use Guzzle\Http\Client;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\History\HistoryPlugin;
use Guzzle\Plugin\Mock\MockPlugin;
class HistoryPluginTest extends \Guzzle\Tests\GuzzleTestCase
{
protected function addRequests(HistoryPlugin $h, $num)
{
$requests = array();
$client = new Client('http://127.0.0.1/');
for ($i = 0; $i < $num; $i++) {
$requests[$i] = $client->get();
$requests[$i]->setResponse(new Response(200), true);
$requests[$i]->send();
$h->add($requests[$i]);
}
return $requests;
}
public function testDescribesSubscribedEvents()
{
$this->assertInternalType('array', HistoryPlugin::getSubscribedEvents());
}
public function testMaintainsLimitValue()
{
$h = new HistoryPlugin();
$this->assertSame($h, $h->setLimit(10));
$this->assertEquals(10, $h->getLimit());
}
public function testAddsRequests()
{
$h = new HistoryPlugin();
$requests = $this->addRequests($h, 1);
$this->assertEquals(1, count($h));
$i = $h->getIterator();
$this->assertEquals(1, count($i));
$this->assertEquals($requests[0], $i[0]);
}
public function testMaintainsLimit()
{
$h = new HistoryPlugin();
$h->setLimit(2);
$requests = $this->addRequests($h, 3);
$this->assertEquals(2, count($h));
$i = 0;
foreach ($h as $request) {
if ($i > 0) {
$this->assertSame($requests[$i], $request);
}
}
}
public function testReturnsLastRequest()
{
$h = new HistoryPlugin();
$requests = $this->addRequests($h, 5);
$this->assertSame(end($requests), $h->getLastRequest());
}
public function testReturnsLastResponse()
{
$h = new HistoryPlugin();
$requests = $this->addRequests($h, 5);
$this->assertSame(end($requests)->getResponse(), $h->getLastResponse());
}
public function testClearsHistory()
{
$h = new HistoryPlugin();
$requests = $this->addRequests($h, 5);
$this->assertEquals(5, count($h));
$h->clear();
$this->assertEquals(0, count($h));
}
public function testUpdatesAddRequests()
{
$h = new HistoryPlugin();
$client = new Client('http://127.0.0.1/');
$client->getEventDispatcher()->addSubscriber($h);
$request = $client->get();
$request->setResponse(new Response(200), true);
$request->send();
$this->assertSame($request, $h->getLastRequest());
}
public function testCanCastToString()
{
$client = new Client('http://127.0.0.1/');
$h = new HistoryPlugin();
$client->getEventDispatcher()->addSubscriber($h);
$mock = new MockPlugin(array(
new Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)),
new Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)),
new Response(200, array('Content-Length' => '2'), 'HI')
));
$client->getEventDispatcher()->addSubscriber($mock);
$request = $client->get();
$request->send();
$this->assertEquals(3, count($h));
$this->assertEquals(3, count($mock->getReceivedRequests()));
$h = str_replace("\r", '', $h);
$this->assertContains("> GET / HTTP/1.1\nHost: 127.0.0.1\nUser-Agent:", $h);
$this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h);
$this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h);
$this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h);
}
}