Open Journal Systems  3.3.0
BatchTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Batch;
4 
7 
12 {
13  private function getMockTransfer()
14  {
15  return $this->getMock('Guzzle\Batch\BatchTransferInterface');
16  }
17 
18  private function getMockDivisor()
19  {
20  return $this->getMock('Guzzle\Batch\BatchDivisorInterface');
21  }
22 
23  public function testAddsItemsToQueue()
24  {
25  $batch = new Batch($this->getMockTransfer(), $this->getMockDivisor());
26  $this->assertSame($batch, $batch->add('foo'));
27  $this->assertEquals(1, count($batch));
28  }
29 
30  public function testFlushReturnsItems()
31  {
32  $transfer = $this->getMockTransfer();
33  $transfer->expects($this->exactly(2))
34  ->method('transfer');
35 
36  $divisor = $this->getMockDivisor();
37  $divisor->expects($this->once())
38  ->method('createBatches')
39  ->will($this->returnValue(array(array('foo', 'baz'), array('bar'))));
40 
41  $batch = new Batch($transfer, $divisor);
42 
43  $batch->add('foo')->add('baz')->add('bar');
44  $items = $batch->flush();
45 
46  $this->assertEquals(array('foo', 'baz', 'bar'), $items);
47  }
48 
50  {
51  $called = 0;
52  $originalException = new \Exception('Foo!');
53 
54  $transfer = $this->getMockTransfer();
55  $transfer->expects($this->exactly(2))
56  ->method('transfer')
57  ->will($this->returnCallback(function () use (&$called, $originalException) {
58  if (++$called == 2) {
59  throw $originalException;
60  }
61  }));
62 
63  $divisor = $this->getMockDivisor();
64  $batch = new Batch($transfer, $divisor);
65 
66  // PHPunit clones objects before passing them to a callback.
67  // Horrible hack to get around this!
68  $queue = $this->readAttribute($batch, 'queue');
69 
70  $divisor->expects($this->once())
71  ->method('createBatches')
72  ->will($this->returnCallback(function ($batch) use ($queue) {
73  foreach ($queue as $item) {
74  $items[] = $item;
75  }
76  return array_chunk($items, 2);
77  }));
78 
79  $batch->add('foo')->add('baz')->add('bar')->add('bee')->add('boo');
80  $this->assertFalse($batch->isEmpty());
81 
82  try {
83  $items = $batch->flush();
84  $this->fail('Expected exception');
85  } catch (BatchTransferException $e) {
86  $this->assertEquals($originalException, $e->getPrevious());
87  $this->assertEquals(array('bar', 'bee'), array_values($e->getBatch()));
88  $this->assertEquals(1, count($batch));
89  }
90  }
91 }
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Batch\BatchTest\testFlushReturnsItems
testFlushReturnsItems()
Definition: BatchTest.php:30
Guzzle\Batch\Exception\BatchTransferException\getBatch
getBatch()
Definition: BatchTransferException.php:68
Guzzle\Tests\Batch\BatchTest\testThrowsExceptionContainingTheFailedBatch
testThrowsExceptionContainingTheFailedBatch()
Definition: BatchTest.php:49
Guzzle\Tests\Batch\BatchTest\testAddsItemsToQueue
testAddsItemsToQueue()
Definition: BatchTest.php:23
Guzzle\Tests\Batch
Definition: AbstractBatchDecoratorTest.php:3
Guzzle\Batch\Exception\BatchTransferException
Definition: BatchTransferException.php:12
Guzzle\Batch\Batch
Definition: Batch.php:15
Guzzle\Tests\Batch\BatchTest
Definition: BatchTest.php:11