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
|
<?php
namespace GuzzleHttp\Tests\Adapter\Curl;
use GuzzleHttp\Adapter\Curl\BatchContext;
use GuzzleHttp\Adapter\Transaction;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Request;
/**
* @covers GuzzleHttp\Adapter\Curl\BatchContext
*/
class BatchContextTest extends \PHPUnit_Framework_TestCase
{
public function testProvidesGetters()
{
$m = curl_multi_init();
$b = new BatchContext($m, true);
$this->assertTrue($b->throwsExceptions());
$this->assertSame($m, $b->getMultiHandle());
$this->assertFalse($b->hasPending());
curl_multi_close($m);
}
public function testValidatesTransactionsAreNotAddedTwice()
{
$m = curl_multi_init();
$b = new BatchContext($m, true);
$h = curl_init();
$t = new Transaction(
new Client(),
new Request('GET', 'http://httbin.org')
);
$b->addTransaction($t, $h);
try {
$b->addTransaction($t, $h);
$this->fail('Did not throw');
} catch (\RuntimeException $e) {
curl_close($h);
curl_multi_close($m);
}
}
public function testManagesHandles()
{
$m = curl_multi_init();
$b = new BatchContext($m, true);
$h = curl_init();
$t = new Transaction(
new Client(),
new Request('GET', 'http://httbin.org')
);
$b->addTransaction($t, $h);
$this->assertTrue($b->isActive());
$this->assertSame($t, $b->findTransaction($h));
$b->removeTransaction($t);
$this->assertFalse($b->isActive());
try {
$this->assertEquals([], $b->findTransaction($h));
$this->fail('Did not throw');
} catch (\RuntimeException $e) {}
curl_multi_close($m);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Transaction not registered
*/
public function testThrowsWhenRemovingNonExistentTransaction()
{
$b = new BatchContext('foo', false);
$t = new Transaction(
new Client(),
new Request('GET', 'http://httbin.org')
);
$b->removeTransaction($t);
}
public function testReturnsPendingAsIteratorTypeObject()
{
$t1 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
$t2 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
$t3 = new Transaction(new Client(), new Request('GET', 'http://t.com'));
$iter = new \ArrayIterator([$t1, $t2, $t3]);
$b = new BatchContext('foo', false, $iter);
$this->assertTrue($b->hasPending());
$this->assertSame($t1, $b->nextPending());
$this->assertTrue($b->hasPending());
$this->assertSame($t2, $b->nextPending());
$this->assertTrue($b->hasPending());
$this->assertSame($t3, $b->nextPending());
$this->assertFalse($b->hasPending());
$this->assertNull($b->nextPending());
}
public function testCanCloseAll()
{
$m = curl_multi_init();
$b = new BatchContext($m, true);
$h = curl_init();
$t = new Transaction(
new Client(),
new Request('GET', 'http://httbin.org')
);
$b->addTransaction($t, $h);
$b->removeAll();
$this->assertFalse($b->isActive());
$this->assertEquals(0, count($this->readAttribute($b, 'handles')));
curl_multi_close($m);
}
}
|