/**
  * @covers MicrosoftAzure\Storage\Queue\Models\ListMessagesOptions::setVisibilityTimeoutInSeconds
  */
 public function testSetVisibilityTimeoutInSeconds()
 {
     // Setup
     $listMessagesOptions = new ListMessagesOptions();
     $expected = 1000;
     // Test
     $listMessagesOptions->setVisibilityTimeoutInSeconds($expected);
     // Assert
     $actual = $listMessagesOptions->getVisibilityTimeoutInSeconds();
     $this->assertEquals($expected, $actual);
 }
 public function testCheckListMessagesOptions()
 {
     $options = new ListMessagesOptions();
     $this->assertNull($options->getTimeout(), 'Default ListMessagesOptions->getTimeout');
     $this->assertNull($options->getNumberOfMessages(), 'Default ListMessagesOptions->getNumberOfMessages');
     $this->assertNull($options->getVisibilityTimeoutInSeconds(), 'Default ListMessagesOptions->getVisibilityTimeoutInSeconds');
     $options->setTimeout(self::INT_MAX_VALUE);
     $options->setNumberOfMessages(0);
     $options->setVisibilityTimeoutInSeconds(self::INT_MIN_VALUE);
     $this->assertEquals(self::INT_MAX_VALUE, $options->getTimeout(), 'Set ListMessagesOptions->getTimeout');
     $this->assertEquals(0, $options->getNumberOfMessages(), 'Set ListMessagesOptions->getNumberOfMessages');
     $this->assertEquals(self::INT_MIN_VALUE, $options->getVisibilityTimeoutInSeconds(), 'Set ListMessagesOptions->getVisibilityTimeoutInSeconds');
 }
 /**
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::clearMessages
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::createMessage
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::listMessages
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::peekMessages
  */
 private function listMessagesWorker($options)
 {
     self::println('Trying $options: ' . self::tmptostring($options));
     $queue = QueueServiceFunctionalTestData::$testQueueNames;
     $queue = $queue[0];
     $this->restProxy->clearMessages($queue);
     // Put three messages into the queue.
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     // Default is 1 message
     $effectiveNumOfMessages = is_null($options) || is_null($options->getNumberOfMessages()) ? 1 : $options->getNumberOfMessages();
     $effectiveNumOfMessages = $effectiveNumOfMessages < 0 ? 0 : $effectiveNumOfMessages;
     // Default is 30 seconds
     $effectiveVisTimeout = is_null($options) || is_null($options->getVisibilityTimeoutInSeconds()) ? 30 : $options->getVisibilityTimeoutInSeconds();
     $effectiveVisTimeout = $effectiveVisTimeout < 0 ? 0 : $effectiveVisTimeout;
     $expectedNumMessagesFirst = $effectiveNumOfMessages > 3 ? 3 : $effectiveNumOfMessages;
     $expectedNumMessagesSecond = $effectiveVisTimeout <= 2 ? 3 : 3 - $effectiveNumOfMessages;
     $expectedNumMessagesSecond = $expectedNumMessagesSecond < 0 ? 0 : $expectedNumMessagesSecond;
     try {
         $res = is_null($options) ? $this->restProxy->listMessages($queue) : $this->restProxy->listMessages($queue, $options);
         if (is_null($options)) {
             $options = new ListMessagesOptions();
         }
         if (!is_null($options->getVisibilityTimeoutInSeconds()) && $options->getVisibilityTimeoutInSeconds() < 1) {
             $this->assertTrue(false, 'Expect non-positive getVisibilityTimeoutInSeconds in $options to throw');
         } else {
             if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) {
                 $this->assertTrue(false, 'Expect  getNumberOfMessages < 1 or 32 < numMessages in $options to throw');
             } else {
                 if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
                     $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
                 }
             }
         }
         $this->assertEquals($expectedNumMessagesFirst, count($res->getQueueMessages()), 'list getQueueMessages() count');
         $opts = new PeekMessagesOptions();
         $opts->setNumberOfMessages(32);
         $pres = $this->restProxy->peekMessages($queue, $opts);
         $this->assertEquals(3 - $expectedNumMessagesFirst, count($pres->getQueueMessages()), 'peek getQueueMessages() count');
         // The visibilityTimeoutInSeconds controls when the requested messages will be visible again.
         // Wait 2.5 seconds to see when the messages are visible again.
         sleep(2.5);
         $opts = new ListMessagesOptions();
         $opts->setNumberOfMessages(32);
         $res2 = $this->restProxy->listMessages($queue, $opts);
         $this->assertEquals($expectedNumMessagesSecond, count($res2->getQueueMessages()), 'list getQueueMessages() count');
         $opts = new PeekMessagesOptions();
         $opts->setNumberOfMessages(32);
         $pres2 = $this->restProxy->peekMessages($queue, $opts);
         $this->assertEquals(0, count($pres2->getQueueMessages()), 'peek getQueueMessages() count');
         // TODO: These might get screwy if the timing gets off. Might need to use times spaces farther apart.
     } catch (ServiceException $e) {
         if (is_null($options)) {
             $options = new ListMessagesOptions();
         }
         if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
             $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
         } else {
             if (!is_null($options->getVisibilityTimeoutInSeconds()) && $options->getVisibilityTimeoutInSeconds() < 1) {
                 $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
             } else {
                 if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) {
                     $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
                 } else {
                     throw $e;
                 }
             }
         }
     }
     $this->restProxy->clearMessages($queue);
 }
 /**
  * Lists all messages in the queue.
  * 
  * @param string              $queueName The queue name.
  * @param ListMessagesOptions $options   The optional parameters.
  * 
  * @return MicrosoftAzure\Storage\Common\Models\ListMessagesResult
  */
 public function listMessages($queueName, $options = null)
 {
     Validate::isString($queueName, 'queueName');
     Validate::notNullOrEmpty($queueName, 'queueName');
     $method = Resources::HTTP_GET;
     $headers = array();
     $queryParams = array();
     $postParams = array();
     $path = $queueName . '/messages';
     $statusCode = Resources::STATUS_OK;
     if (is_null($options)) {
         $options = new ListMessagesOptions();
     }
     $messagesCount = $options->getNumberOfMessages();
     $visibility = $options->getVisibilityTimeoutInSeconds();
     $timeout = $options->getTimeout();
     $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $timeout);
     $this->addOptionalQueryParam($queryParams, Resources::QP_NUM_OF_MESSAGES, $messagesCount);
     $this->addOptionalQueryParam($queryParams, Resources::QP_VISIBILITY_TIMEOUT, $visibility);
     $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode);
     $parsed = $this->dataSerializer->unserialize($response->getBody());
     return ListMessagesResult::create($parsed);
 }