~ballot/wordpress/openstack-objectstorage-breaking-insight

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/tests/Adapter/Curl/BatchContextTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace GuzzleHttp\Tests\Adapter\Curl;
 
4
 
 
5
use GuzzleHttp\Adapter\Curl\BatchContext;
 
6
use GuzzleHttp\Adapter\Transaction;
 
7
use GuzzleHttp\Client;
 
8
use GuzzleHttp\Message\Request;
 
9
 
 
10
/**
 
11
 * @covers GuzzleHttp\Adapter\Curl\BatchContext
 
12
 */
 
13
class BatchContextTest extends \PHPUnit_Framework_TestCase
 
14
{
 
15
    public function testProvidesGetters()
 
16
    {
 
17
        $m = curl_multi_init();
 
18
        $b = new BatchContext($m, true);
 
19
        $this->assertTrue($b->throwsExceptions());
 
20
        $this->assertSame($m, $b->getMultiHandle());
 
21
        $this->assertFalse($b->hasPending());
 
22
        curl_multi_close($m);
 
23
    }
 
24
 
 
25
    public function testValidatesTransactionsAreNotAddedTwice()
 
26
    {
 
27
        $m = curl_multi_init();
 
28
        $b = new BatchContext($m, true);
 
29
        $h = curl_init();
 
30
        $t = new Transaction(
 
31
            new Client(),
 
32
            new Request('GET', 'http://httbin.org')
 
33
        );
 
34
        $b->addTransaction($t, $h);
 
35
        try {
 
36
            $b->addTransaction($t, $h);
 
37
            $this->fail('Did not throw');
 
38
        } catch (\RuntimeException $e) {
 
39
            curl_close($h);
 
40
            curl_multi_close($m);
 
41
        }
 
42
    }
 
43
 
 
44
    public function testManagesHandles()
 
45
    {
 
46
        $m = curl_multi_init();
 
47
        $b = new BatchContext($m, true);
 
48
        $h = curl_init();
 
49
        $t = new Transaction(
 
50
            new Client(),
 
51
            new Request('GET', 'http://httbin.org')
 
52
        );
 
53
        $b->addTransaction($t, $h);
 
54
        $this->assertTrue($b->isActive());
 
55
        $this->assertSame($t, $b->findTransaction($h));
 
56
        $b->removeTransaction($t);
 
57
        $this->assertFalse($b->isActive());
 
58
        try {
 
59
            $this->assertEquals([], $b->findTransaction($h));
 
60
            $this->fail('Did not throw');
 
61
        } catch (\RuntimeException $e) {}
 
62
        curl_multi_close($m);
 
63
    }
 
64
 
 
65
    /**
 
66
     * @expectedException \RuntimeException
 
67
     * @expectedExceptionMessage Transaction not registered
 
68
     */
 
69
    public function testThrowsWhenRemovingNonExistentTransaction()
 
70
    {
 
71
        $b = new BatchContext('foo', false);
 
72
        $t = new Transaction(
 
73
            new Client(),
 
74
            new Request('GET', 'http://httbin.org')
 
75
        );
 
76
        $b->removeTransaction($t);
 
77
    }
 
78
 
 
79
    public function testReturnsPendingAsIteratorTypeObject()
 
80
    {
 
81
        $t1 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
 
82
        $t2 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
 
83
        $t3 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
 
84
        $iter = new \ArrayIterator([$t1, $t2, $t3]);
 
85
        $b = new BatchContext('foo', false, $iter);
 
86
        $this->assertTrue($b->hasPending());
 
87
        $this->assertSame($t1, $b->nextPending());
 
88
        $this->assertTrue($b->hasPending());
 
89
        $this->assertSame($t2, $b->nextPending());
 
90
        $this->assertTrue($b->hasPending());
 
91
        $this->assertSame($t3, $b->nextPending());
 
92
        $this->assertFalse($b->hasPending());
 
93
        $this->assertNull($b->nextPending());
 
94
    }
 
95
 
 
96
    public function testCanCloseAll()
 
97
    {
 
98
        $m = curl_multi_init();
 
99
        $b = new BatchContext($m, true);
 
100
        $h = curl_init();
 
101
        $t = new Transaction(
 
102
            new Client(),
 
103
            new Request('GET', 'http://httbin.org')
 
104
        );
 
105
        $b->addTransaction($t, $h);
 
106
        $b->removeAll();
 
107
        $this->assertFalse($b->isActive());
 
108
        $this->assertEquals(0, count($this->readAttribute($b, 'handles')));
 
109
        curl_multi_close($m);
 
110
    }
 
111
}