Вход Регистрация
Файл: vkolhoze.com/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/History/HistoryPluginTest.php
Строк: 155
<?php

namespace GuzzleTestsPluginHistory;

use 
GuzzleHttpClient;
use 
GuzzleHttpMessageRequest;
use 
GuzzleHttpMessageResponse;
use 
GuzzlePluginHistoryHistoryPlugin;
use 
GuzzlePluginMockMockPlugin;

/**
 * @covers GuzzlePluginHistoryHistoryPlugin
 */
class HistoryPluginTest extends GuzzleTestsGuzzleTestCase
{
    
/**
     * Adds multiple requests to a plugin
     *
     * @param HistoryPlugin $h Plugin
     * @param int $num Number of requests to add
     *
     * @return array
     */
    
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($h1);
        
$this->assertEquals(1count($h));
        
$i $h->getIterator();
        
$this->assertEquals(1count($i));
        
$this->assertEquals($requests[0], $i[0]);
    }

    
/**
     * @depends testAddsRequests
     */
    
public function testMaintainsLimit()
    {
        
$h = new HistoryPlugin();
        
$h->setLimit(2);
        
$requests $this->addRequests($h3);
        
$this->assertEquals(2count($h));
        
$i 0;
        foreach (
$h as $request) {
            if (
$i 0) {
                
$this->assertSame($requests[$i], $request);
            }
        }
    }

    public function 
testReturnsLastRequest()
    {
        
$h = new HistoryPlugin();
        
$requests $this->addRequests($h5);
        
$this->assertSame(end($requests), $h->getLastRequest());
    }

    public function 
testReturnsLastResponse()
    {
        
$h = new HistoryPlugin();
        
$requests $this->addRequests($h5);
        
$this->assertSame(end($requests)->getResponse(), $h->getLastResponse());
    }

    public function 
testClearsHistory()
    {
        
$h = new HistoryPlugin();
        
$requests $this->addRequests($h5);
        
$this->assertEquals(5count($h));
        
$h->clear();
        
$this->assertEquals(0count($h));
    }

    
/**
     * @depends testAddsRequests
     */
    
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(3count($h));
        
$this->assertEquals(3count($mock->getReceivedRequests()));

        
$h str_replace("r"''$h);
        
$this->assertContains("> GET / HTTP/1.1nHost: 127.0.0.1nUser-Agent:"$h);
        
$this->assertContains("< HTTP/1.1 301 Moved PermanentlynLocation: /redirect1"$h);
        
$this->assertContains("< HTTP/1.1 307 Temporary RedirectnLocation: /redirect2"$h);
        
$this->assertContains("< HTTP/1.1 200 OKnContent-Length: 2nnHI"$h);
    }
}
Онлайн: 0
Реклама