/** * Test getting messages until the queue runs out. When no messages left in the queue, * getNextMessage will return null, indicating no additional messages. * Iterable should end when no additional messages left in the queue. */ public function testIterateExhaustMessages() { $body = '<test xmlns="http://api.gsicommerce.com/schema/checkout/1.0" timestamp="2000-01-01T00:00:00-00:00"/>'; $this->api->expects($this->any())->method('getNextMessage')->will($this->onConsecutiveCalls(new AMQPMessage($body, ['type' => 'Test']), new AMQPMessage($body, ['type' => 'Test']))); $processed = 0; while ($this->iterator->valid()) { $processed++; $this->iterator->next(); } $this->assertSame(2, $processed); }
/** * Test if there is a payload at the current index. Called after self::rewind * and self::next while iterating over the messages. * @return bool */ public function valid() { // don't process more than the max messages if ($this->currentKey >= $this->maxMessages) { return false; } // if a payload already exists for the current key, we'll be able to return that if (isset($this->messages[$this->currentKey])) { return true; } // if no payload exists for the current key yet, try to get the next one $message = $this->api->getNextMessage(); if ($message) { $this->messages[$this->currentKey] = $message; return true; } // if no message exists and no new messages could be received, // end the iterable return false; }