PageRenderTime 84ms CodeModel.GetById 10ms app.highlight 65ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/functional/WindowsAzure/Queue/QueueServiceFunctionalTest.php

http://github.com/WindowsAzure/azure-sdk-for-php
PHP | 1362 lines | 975 code | 160 blank | 227 comment | 224 complexity | 3d772836af988ed5af0a195cd8fe316a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/**
   4 * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
   5 * you may not use this file except in compliance with the License.
   6 * You may obtain a copy of the License at
   7 * http://www.apache.org/licenses/LICENSE-2.0
   8 *
   9 * Unless required by applicable law or agreed to in writing, software
  10 * distributed under the License is distributed on an "AS IS" BASIS,
  11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 * See the License for the specific language governing permissions and
  13 * limitations under the License.
  14 * 
  15 * PHP version 5
  16 *
  17 * @category  Microsoft
  18 * @package   Tests\Functional\WindowsAzure\Queue
  19 * @author    Azure PHP SDK <azurephpsdk@microsoft.com>
  20 * @copyright 2012 Microsoft Corporation
  21 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
  22 * @link      https://github.com/windowsazure/azure-sdk-for-php
  23 */
  24
  25namespace Tests\Functional\WindowsAzure\Queue;
  26
  27use \HTTP_Request2_LogicException;
  28use WindowsAzure\Common\ServiceException;
  29use WindowsAzure\Common\Configuration;
  30use WindowsAzure\Common\Models\Logging;
  31use WindowsAzure\Common\Models\Metrics;
  32use WindowsAzure\Common\Models\RetentionPolicy;
  33use WindowsAzure\Common\Models\ServiceProperties;
  34use WindowsAzure\Queue\Models\CreateMessageOptions;
  35use WindowsAzure\Queue\Models\CreateQueueOptions;
  36use WindowsAzure\Queue\Models\ListMessagesOptions;
  37use WindowsAzure\Queue\Models\ListQueuesOptions;
  38use WindowsAzure\Queue\Models\PeekMessagesOptions;
  39use WindowsAzure\Queue\Models\QueueServiceOptions;
  40
  41class QueueServiceFunctionalTest extends FunctionalTestBase {
  42    /**
  43    * @covers WindowsAzure\Queue\QueueRestProxy::getServiceProperties
  44    * @covers WindowsAzure\Queue\QueueRestProxy::setServiceProperties
  45    */
  46    public function testGetServicePropertiesNoOptions() {
  47        $serviceProperties = QueueServiceFunctionalTestData::getDefaultServiceProperties();
  48       
  49        $shouldReturn = false;
  50        try {
  51            $this->restProxy->setServiceProperties($serviceProperties);
  52            $this->assertFalse(Configuration::isEmulated(), 'Should succeed when not running in emulator');
  53        } catch (ServiceException $e) {
  54            // Expect failure in emulator, as v1.6 doesn't support this method
  55            if (Configuration::isEmulated()) {
  56                $this->assertEquals(400, $e->getCode(), 'getCode');
  57                $shouldReturn = true;
  58            } else {
  59                throw $e;
  60            }
  61        }
  62        if($shouldReturn) {
  63            return;
  64        }
  65
  66        $this->getServicePropertiesWorker(null);
  67    }
  68
  69    /**
  70    * @covers WindowsAzure\Queue\QueueRestProxy::getServiceProperties
  71    * @covers WindowsAzure\Queue\QueueRestProxy::setServiceProperties
  72    */
  73    public function testGetServiceProperties() {
  74        $serviceProperties = QueueServiceFunctionalTestData::getDefaultServiceProperties();
  75
  76        $shouldReturn = false;
  77        try {
  78            $this->restProxy->setServiceProperties($serviceProperties);
  79            $this->assertFalse(Configuration::isEmulated(), 'Should succeed when not running in emulator');
  80        } catch (ServiceException $e) {
  81            // Expect failure in emulator, as v1.6 doesn't support this method
  82            if (Configuration::isEmulated()) {
  83                $this->assertEquals(400, $e->getCode(), 'getCode');
  84                $shouldReturn = true;
  85            } else {
  86                throw $e;
  87            }
  88        }
  89        if($shouldReturn) {
  90            return;
  91        }
  92
  93        // Now look at the combos.
  94        $interestingTimeouts = QueueServiceFunctionalTestData::getInterestingTimeoutValues();
  95        foreach($interestingTimeouts as $timeout)  {
  96            $options = new QueueServiceOptions();
  97            $options->setTimeout($timeout);
  98            $this->getServicePropertiesWorker($options);
  99        }
 100    }
 101
 102    private function getServicePropertiesWorker($options) {
 103        self::println( 'Trying $options: ' . self::tmptostring($options));
 104        $effOptions = (is_null($options) ? new QueueServiceOptions() : $options);
 105        try {
 106            $ret = (is_null($options) ? $this->restProxy->getServiceProperties() : $this->restProxy->getServiceProperties($effOptions));
 107
 108            if (!is_null($effOptions->getTimeout()) && $effOptions->getTimeout() < 1) {
 109                $this->True('Expect negative timeouts in $options to throw', false);
 110            } else {
 111                $this->assertFalse(Configuration::isEmulated(), 'Should succeed when not running in emulator');
 112            }
 113            $this->verifyServicePropertiesWorker($ret, null);
 114        }
 115        catch (ServiceException $e) {
 116            if (Configuration::isEmulated()) {
 117                if (!is_null($options->getTimeout()) && $options->getTimeout() < 0) {
 118                    $this->assertEquals(500, $e->getCode(), 'getCode');
 119                } else {
 120                    // Expect failure in emulator, as v1.6 doesn't support this method
 121                    $this->assertEquals(400, $e->getCode(), 'getCode');
 122                }
 123            } else {
 124                if (is_null($effOptions->getTimeout()) || $effOptions->getTimeout() >= 1) {
 125                    $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 126                }
 127                else {
 128                    $this->assertEquals(500, $e->getCode(), 'getCode');
 129                }
 130            }
 131        }
 132    }
 133
 134    private function verifyServicePropertiesWorker($ret, $serviceProperties) {
 135        if (is_null($serviceProperties)) {
 136            $serviceProperties = QueueServiceFunctionalTestData::getDefaultServiceProperties();
 137        }
 138
 139        $sp = $ret->getValue();
 140        $this->assertNotNull($sp, 'getValue should be non-null');
 141
 142        $l = $sp->getLogging();
 143        $this->assertNotNull($l, 'getValue()->getLogging() should be non-null');
 144        $this->assertEquals($serviceProperties->getLogging()->getVersion(), $l->getVersion(), 'getValue()->getLogging()->getVersion');
 145        $this->assertEquals($serviceProperties->getLogging()->getDelete(), $l->getDelete(), 'getValue()->getLogging()->getDelete');
 146        $this->assertEquals($serviceProperties->getLogging()->getRead(), $l->getRead(), 'getValue()->getLogging()->getRead');
 147        $this->assertEquals($serviceProperties->getLogging()->getWrite(), $l->getWrite(), 'getValue()->getLogging()->getWrite');
 148
 149        $r = $l->getRetentionPolicy();
 150        $this->assertNotNull($r, 'getValue()->getLogging()->getRetentionPolicy should be non-null');
 151        $this->assertEquals($serviceProperties->getLogging()->getRetentionPolicy()->getDays(), $r->getDays(), 'getValue()->getLogging()->getRetentionPolicy()->getDays');
 152
 153        $m = $sp->getMetrics();
 154        $this->assertNotNull($m, 'getValue()->getMetrics() should be non-null');
 155        $this->assertEquals($serviceProperties->getMetrics()->getVersion(), $m->getVersion(), 'getValue()->getMetrics()->getVersion');
 156        $this->assertEquals($serviceProperties->getMetrics()->getEnabled(), $m->getEnabled(), 'getValue()->getMetrics()->getEnabled');
 157        $this->assertEquals($serviceProperties->getMetrics()->getIncludeAPIs(), $m->getIncludeAPIs(), 'getValue()->getMetrics()->getIncludeAPIs');
 158
 159        $r = $m->getRetentionPolicy();
 160        $this->assertNotNull($r, 'getValue()->getMetrics()->getRetentionPolicy should be non-null');
 161        $this->assertEquals($serviceProperties->getMetrics()->getRetentionPolicy()->getDays(), $r->getDays(), 'getValue()->getMetrics()->getRetentionPolicy()->getDays');
 162    }
 163
 164    /**
 165    * @covers WindowsAzure\Queue\QueueRestProxy::getServiceProperties
 166    * @covers WindowsAzure\Queue\QueueRestProxy::setServiceProperties
 167    */
 168    public function testSetServicePropertiesNoOptions() {
 169        $serviceProperties = QueueServiceFunctionalTestData::getDefaultServiceProperties();
 170        $this->setServicePropertiesWorker($serviceProperties, null);
 171    }
 172
 173    /**
 174    * @covers WindowsAzure\Queue\QueueRestProxy::getServiceProperties
 175    * @covers WindowsAzure\Queue\QueueRestProxy::setServiceProperties
 176    */
 177    public function testSetServiceProperties() {
 178        $interestingServiceProperties = QueueServiceFunctionalTestData::getInterestingServiceProperties();
 179        foreach($interestingServiceProperties as $serviceProperties)  {
 180            $interestingTimeouts = QueueServiceFunctionalTestData::getInterestingTimeoutValues();
 181            foreach($interestingTimeouts as $timeout)  {
 182                $options = new QueueServiceOptions();
 183                $options->setTimeout($timeout);
 184                $this->setServicePropertiesWorker($serviceProperties, $options);
 185            }
 186        }
 187
 188        if (!Configuration::isEmulated()) {
 189            $this->restProxy->setServiceProperties($interestingServiceProperties[0]);
 190        }
 191    }
 192
 193    private function setServicePropertiesWorker($serviceProperties, $options) {
 194        try {
 195            if (is_null($options)) {
 196                $this->restProxy->setServiceProperties($serviceProperties);
 197            } else {
 198                $this->restProxy->setServiceProperties($serviceProperties, $options);
 199            }
 200
 201            if (is_null($options)) {
 202                $options = new QueueServiceOptions();
 203            }
 204
 205            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 206                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 207            } else {
 208                $this->assertFalse(Configuration::isEmulated(), 'Should succeed when not running in emulator');
 209            }
 210
 211            $ret = (is_null($options) ? $this->restProxy->getServiceProperties() : $this->restProxy->getServiceProperties($options));
 212            $this->verifyServicePropertiesWorker($ret, $serviceProperties);
 213        } catch (ServiceException $e) {
 214            if (is_null($options)) {
 215                $options = new QueueServiceOptions();
 216            }
 217
 218            if (Configuration::isEmulated()) {
 219                if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 220                    $this->assertEquals(500, $e->getCode(), 'getCode');
 221                } else {
 222                    $this->assertEquals(400, $e->getCode(), 'getCode');
 223                }
 224            } else {
 225                if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 226                    $this->assertEquals(500, $e->getCode(), 'getCode');
 227                } else {
 228                    $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 229                }
 230            }
 231        }
 232    }
 233
 234    /**
 235    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 236    */
 237    public function testListQueuesNoOptions() {
 238        $this->listQueuesWorker(null);
 239    }
 240
 241    /**
 242    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 243    */
 244    public function testListQueues() {
 245        $interestingListQueuesOptions = QueueServiceFunctionalTestData::getInterestingListQueuesOptions();
 246        foreach($interestingListQueuesOptions as $options)  {
 247            $this->listQueuesWorker($options);
 248        }
 249    }
 250
 251    private function listQueuesWorker($options) {
 252        $finished = false;
 253        while (!$finished) {
 254            try {
 255                $ret = (is_null($options) ? $this->restProxy->listQueues() : $this->restProxy->listQueues($options));
 256
 257                if (is_null($options)) {
 258                    $options = new ListQueuesOptions();
 259                }
 260
 261                if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 262                    $this->assertTrue(false, 'Expect negative timeouts ' . $options->getTimeout() . ' in $options to throw');
 263                }
 264                $this->verifyListQueuesWorker($ret, $options);
 265
 266                if (strlen($ret->getNextMarker()) == 0) {
 267                    self::println('Done with this loop');
 268                    $finished = true;
 269                }
 270                else {
 271                    self::println('Cycling to get the next marker: ' . $ret->getNextMarker());
 272                    $options->setMarker($ret->getNextMarker());
 273                }
 274            }
 275            catch (ServiceException $e) {
 276                $finished = true;
 277                if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
 278                    $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 279                }
 280                else {
 281                    $this->assertEquals(500, $e->getCode(), 'getCode');
 282                }
 283            }
 284        }
 285    }
 286
 287    private function verifyListQueuesWorker($ret, $options) {
 288        // Uncomment when fixed
 289        // https://github.com/WindowsAzure/azure-sdk-for-php/issues/98
 290        //$this->assertEquals($accountName, $ret->getAccountName(), 'getAccountName');
 291
 292        $this->assertEquals($options->getMarker(), $ret->getMarker(), 'getMarker');
 293        $this->assertEquals($options->getMaxResults(), $ret->getMaxResults(), 'getMaxResults');
 294        $this->assertEquals($options->getPrefix(), $ret->getPrefix(), 'getPrefix');
 295
 296        $this->assertNotNull($ret->getQueues(), 'getQueues');
 297         
 298        if ($options->getMaxResults() == 0) {
 299            $this->assertNull($ret->getNextMarker(), 'When MaxResults is 0, expect getNextMarker (' . $ret->getNextMarker() . ')to be null');
 300
 301            if (!is_null($options->getPrefix()) && $options->getPrefix() == QueueServiceFunctionalTestData::$nonExistQueuePrefix) {
 302                $this->assertEquals(0, count($ret->getQueues()), 'when MaxResults=0 and Prefix=(\'' . $options->getPrefix() . '\'), then Queues->length');
 303            }
 304            else if (!is_null($options->getPrefix()) && $options->getPrefix() == QueueServiceFunctionalTestData::$testUniqueId) {
 305                $this->assertEquals(count(QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES), count($ret->getQueues()), 'when MaxResults=0 and Prefix=(\'' . $options->getPrefix() . '\'), then count Queues');
 306            }
 307            else {
 308                // Don't know how many there should be
 309            }
 310        }
 311        else if (strlen($ret->getNextMarker()) == 0) {
 312            $this->assertTrue(count($ret ->getQueues()) <= $options->getMaxResults(), 'when NextMarker (\'' . $ret->getNextMarker() . '\')==\'\', Queues->length (' . count($ret->getQueues()) . ') should be <= MaxResults (' . $options->getMaxResults() . ')');
 313
 314            if (!is_null($options->getPrefix()) && $options->getPrefix() == QueueServiceFunctionalTestData::$nonExistQueuePrefix) {
 315                $this->assertEquals(0, count($ret->getQueues()), 'when no next marker and Prefix=(\'' . $options->getPrefix() . '\'), then Queues->length');
 316            }
 317            else if (!is_null($options->getPrefix()) && $options->getPrefix() == QueueServiceFunctionalTestData::$testUniqueId) {
 318                // Need to futz with the mod because you are allowed to get MaxResults items returned.
 319                $this->assertEquals(count(QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES) % $options->getMaxResults(), count($ret ->getQueues()) % $options->getMaxResults(), 'when no next marker and Prefix=(\'' . $options->getPrefix() . '\'), then Queues->length');
 320            }
 321            else {
 322                // Don't know how many there should be
 323            }
 324        }
 325        else {
 326            $this->assertEquals(count($ret ->getQueues()), $options->getMaxResults(),
 327                    'when NextMarker (' . $ret->getNextMarker() .
 328                    ')!=\'\', Queues->length (' . count($ret->getQueues()) . 
 329                    ') should be == MaxResults (' . $options->getMaxResults() . ')');
 330
 331            if (!is_null($options->getPrefix()) && $options->getPrefix() == (QueueServiceFunctionalTestData::$nonExistQueuePrefix)) {
 332                $this->assertTrue(false, 'when a next marker and Prefix=(\'' . $options->getPrefix() . '\'), impossible');
 333            }
 334        }
 335    }
 336
 337    /**
 338    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 339    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 340    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 341    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 342    */
 343    public function testCreateQueueNoOptions() {
 344        $this->createQueueWorker(null);
 345    }
 346
 347    /**
 348    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 349    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 350    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 351    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 352    */
 353    public function testCreateQueue() {
 354        $interestingCreateQueueOptions = QueueServiceFunctionalTestData::getInterestingCreateQueueOptions();
 355        foreach($interestingCreateQueueOptions as $options)  {
 356            $this->createQueueWorker($options);
 357        }
 358    }
 359
 360    private function createQueueWorker($options) {
 361        self::println( 'Trying $options: ' . self::tmptostring($options));
 362        $queue = QueueServiceFunctionalTestData::getInterestingQueueName();
 363        $created = false;
 364
 365        try {
 366            if (is_null($options)) {
 367                $this->restProxy->createQueue($queue);
 368            }
 369            else {
 370                $this->restProxy->createQueue($queue, $options);
 371            }
 372            $created = true;
 373
 374            if (is_null($options)) {
 375                $options = new CreateQueueOptions();
 376            }
 377
 378            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 379                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 380            }
 381
 382            // Now check that the queue was created correctly.
 383
 384            // Make sure that the list of all applicable queues is correctly updated.
 385            $opts = new ListQueuesOptions();
 386            $opts->setPrefix(QueueServiceFunctionalTestData::$testUniqueId);
 387            $qs = $this->restProxy->listQueues($opts);
 388            $this->assertEquals(count($qs->getQueues()), (count(QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES) + 1), 'After adding one, with Prefix=(\'' . QueueServiceFunctionalTestData::$testUniqueId . '\'), then Queues->length');
 389
 390            // Check the metadata on the queue
 391            $ret = $this->restProxy->getQueueMetadata($queue);
 392            $this->verifyCreateQueueWorker($ret, $options);
 393            $this->restProxy->deleteQueue($queue);
 394            $created = false;
 395        }
 396        catch (ServiceException $e) {
 397            if (is_null($options)) {
 398                $options = new CreateQueueOptions();
 399            }
 400            if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
 401                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 402            }
 403            else {
 404                $this->assertEquals(500, $e->getCode(), 'getCode');
 405            }
 406        }
 407        if ($created) {
 408            $this->restProxy->deleteQueue($queue);
 409        }
 410    }
 411
 412    private function verifyCreateQueueWorker($ret, $options) {
 413        self::println( 'Trying $options: ' . self::tmptostring($options) . 
 414                ' and ret ' . self::tmptostring($ret));
 415        if (is_null($options)) {
 416            $options = QueueServiceFunctionalTestData::getInterestingCreateQueueOptions();
 417            $options = $options[0];
 418        }
 419
 420        if (is_null($options->getMetadata())) {
 421            $this->assertNotNull($ret->getMetadata(), 'queue Metadata');
 422            $this->assertEquals(0, count($ret->getMetadata()), 'queue Metadata count');
 423        }
 424        else {
 425            $this->assertNotNull($ret->getMetadata(), 'queue Metadata');
 426            $this->assertEquals(count($options->getMetadata()), count($ret->getMetadata()), 'Metadata');
 427            $om = $options->getMetadata();
 428            $rm = $ret->getMetadata();
 429            foreach(array_keys($options->getMetadata()) as $key)  {
 430                $this->assertEquals($om[$key], $rm[$key], 'Metadata(' . $key . ')');
 431            }
 432        }
 433    }
 434
 435    /**
 436    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 437    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 438    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 439    */
 440    public function testDeleteQueueNoOptions() {
 441        $this->deleteQueueWorker(null);
 442    }
 443
 444    /**
 445    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 446    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 447    * @covers WindowsAzure\Queue\QueueRestProxy::listQueues
 448    */
 449    public function testDeleteQueue() {
 450        $interestingTimeouts = QueueServiceFunctionalTestData::getInterestingTimeoutValues();
 451        foreach($interestingTimeouts as $timeout)  {
 452            $options = new QueueServiceOptions();
 453            $options->setTimeout($timeout);
 454            $this->deleteQueueWorker($options);
 455        }
 456    }
 457
 458    private function deleteQueueWorker($options) {
 459        self::println( 'Trying $options: ' . self::tmptostring($options));
 460        $queue = QueueServiceFunctionalTestData::getInterestingQueueName();
 461
 462        // Make sure there is something to delete.
 463        $this->restProxy->createQueue($queue);
 464
 465        // Make sure that the list of all applicable queues is correctly updated.
 466        $opts = new ListQueuesOptions();
 467        $opts->setPrefix(QueueServiceFunctionalTestData::$testUniqueId);
 468        $qs = $this->restProxy->listQueues($opts);
 469        $this->assertEquals(count($qs->getQueues()), (count(QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES) + 1), 'After adding one, with Prefix=(\'' . QueueServiceFunctionalTestData::$testUniqueId . '\'), then Queues->length');
 470
 471        $deleted = false;
 472        try {
 473            if (is_null($options)) {
 474                $this->restProxy->deleteQueue($queue);
 475            }
 476            else {
 477                $this->restProxy->deleteQueue($queue, $options);
 478            }
 479
 480            $deleted = true;
 481
 482            if (is_null($options)) {
 483                $options = new QueueServiceOptions();
 484            }
 485
 486            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 487                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 488            }
 489
 490            // Make sure that the list of all applicable queues is correctly updated.
 491            $opts = new ListQueuesOptions();
 492            $opts->setPrefix(QueueServiceFunctionalTestData::$testUniqueId);
 493            $qs = $this->restProxy->listQueues($opts);
 494            $this->assertEquals(count($qs->getQueues()), count(QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES), 'After adding then deleting one, with Prefix=(\'' . QueueServiceFunctionalTestData::$testUniqueId . '\'), then Queues->length');
 495
 496            // Nothing else interesting to check for the options.
 497        }
 498        catch (ServiceException $e) {
 499            if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
 500                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 501            }
 502            else {
 503                $this->assertEquals(500, $e->getCode(), 'getCode');
 504            }
 505        }
 506        if (!$deleted) {
 507            // Try again. If it doesn't work, not much else to try.
 508            $this->restProxy->deleteQueue($queue);
 509        }
 510    }
 511
 512    // TODO: Negative tests, like accessing a non-existant queue, or recreating an existing queue?
 513
 514    /**
 515    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 516    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 517    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 518    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 519    * @covers WindowsAzure\Queue\QueueRestProxy::setQueueMetadata
 520    */
 521    public function testGetQueueMetadataNoOptions() {
 522        $interestingMetadata = QueueServiceFunctionalTestData::getNiceMetadata();
 523        foreach ($interestingMetadata as $metadata) {
 524            $this->getQueueMetadataWorker(null, $metadata);
 525        }
 526    }
 527
 528    /**
 529    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 530    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 531    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 532    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 533    * @covers WindowsAzure\Queue\QueueRestProxy::setQueueMetadata
 534    */
 535    public function testGetQueueMetadata() {
 536        $interestingTimeouts = QueueServiceFunctionalTestData::getInterestingTimeoutValues();
 537        $interestingMetadata = QueueServiceFunctionalTestData::getNiceMetadata();
 538
 539        foreach($interestingTimeouts as $timeout)  {
 540            foreach ($interestingMetadata as $metadata) {
 541                $options = new QueueServiceOptions();
 542                $options->setTimeout($timeout);
 543                $this->getQueueMetadataWorker($options, $metadata);
 544            }
 545        }
 546    }
 547
 548    private function getQueueMetadataWorker($options, $metadata) {
 549        self::println( 'Trying $options: ' . self::tmptostring($options) . 
 550                ' and $metadata: ' . self::tmptostring($metadata));
 551        $queue = QueueServiceFunctionalTestData::getInterestingQueueName();
 552
 553        // Make sure there is something to test
 554        $this->restProxy->createQueue($queue);
 555
 556        // Put some messages to verify getApproximateMessageCount 
 557        if (!is_null($metadata)) {
 558            for ($i = 0; $i < count($metadata); $i++) {
 559                $this->restProxy->createMessage($queue, 'message ' . $i);
 560            }
 561
 562            // And put in some metadata
 563            $this->restProxy->setQueueMetadata($queue, $metadata);
 564        }
 565
 566        try {
 567            $res = (is_null($options) ? $this->restProxy->getQueueMetadata($queue) : $this->restProxy->getQueueMetadata( $queue, $options));
 568
 569            if (is_null($options)) {
 570                $options = new QueueServiceOptions();
 571            }
 572
 573            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 574                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 575            }
 576
 577            $this->verifyGetSetQueueMetadataWorker($res, $metadata);
 578        }
 579        catch (ServiceException $e) {
 580            if (is_null($options->getTimeout()) || $options->getTimeout() >= 1) {
 581                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 582            }
 583            else {
 584                $this->assertEquals(500, $e->getCode(), 'getCode');
 585            }
 586        }
 587        // Clean up->
 588        $this->restProxy->deleteQueue($queue);
 589    }
 590
 591    private function verifyGetSetQueueMetadataWorker($ret, $metadata) {
 592        $this->assertNotNull($ret->getMetadata(), 'queue Metadata');
 593        if (is_null($metadata)) {
 594            $this->assertEquals(0, count($ret->getMetadata()), 'Metadata');
 595            $this->assertEquals(0, $ret->getApproximateMessageCount(), 'getApproximateMessageCount');
 596        }
 597        else {
 598            $this->assertEquals(count($metadata), count($ret->getMetadata()), 'Metadata');
 599            $rm =$ret->getMetadata();
 600            foreach(array_keys($metadata) as $key)  {
 601                $this->assertEquals($metadata[$key], $rm[$key], 'Metadata(' . $key . ')');
 602            }
 603
 604            // Hard to test "approximate", so just verify that it is in the expected range
 605            $this->assertTrue(
 606                    (0 <= $ret->getApproximateMessageCount()) && ($ret->getApproximateMessageCount() <= count($metadata)),
 607                    '0 <= getApproximateMessageCount (' . $ret->getApproximateMessageCount() . ') <= $metadata count (' . count($metadata) . ')');
 608        }
 609    }
 610
 611    /**
 612    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 613    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 614    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 615    * @covers WindowsAzure\Queue\QueueRestProxy::setQueueMetadata
 616    */
 617    public function testSetQueueMetadataNoOptions() {
 618        $interestingMetadata = QueueServiceFunctionalTestData::getInterestingMetadata();
 619        foreach ($interestingMetadata as $metadata) {
 620            if (is_null($metadata)) {
 621                // This is tested above.
 622                continue;
 623            }
 624            $this->setQueueMetadataWorker(null, $metadata);
 625        }
 626    }
 627
 628    /**
 629    * @covers WindowsAzure\Queue\QueueRestProxy::createQueue
 630    * @covers WindowsAzure\Queue\QueueRestProxy::deleteQueue
 631    * @covers WindowsAzure\Queue\QueueRestProxy::getQueueMetadata
 632    * @covers WindowsAzure\Queue\QueueRestProxy::setQueueMetadata
 633    */
 634    public function testSetQueueMetadata() {
 635        $interestingTimeouts = QueueServiceFunctionalTestData::getInterestingTimeoutValues();
 636        $interestingMetadata = QueueServiceFunctionalTestData::getInterestingMetadata();
 637
 638        foreach($interestingTimeouts as $timeout)  {
 639            foreach ($interestingMetadata as $metadata) {
 640                if (is_null($metadata)) {
 641                    // This is tested above.
 642                    continue;
 643                }
 644                $options = new QueueServiceOptions();
 645                $options->setTimeout($timeout);
 646                $this->setQueueMetadataWorker($options, $metadata);
 647            }
 648        }
 649    }
 650
 651    private function setQueueMetadataWorker($options, $metadata) {
 652        self::println( 'Trying $options: ' . self::tmptostring($options) . 
 653                ' and $metadata: ' . self::tmptostring($metadata));
 654        $queue = QueueServiceFunctionalTestData::getInterestingQueueName();
 655
 656        // Make sure there is something to test
 657        $this->restProxy->createQueue($queue);
 658
 659        try {
 660          try {
 661            // And put in some metadata
 662            if (is_null($options)) {
 663                $this->restProxy->setQueueMetadata($queue, $metadata);
 664            }
 665            else {
 666                $this->restProxy->setQueueMetadata($queue, $metadata, $options);
 667            }
 668
 669            if (is_null($options)) {
 670                $options = new QueueServiceOptions();
 671            }
 672
 673            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 674                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 675            }
 676
 677            $res = $this->restProxy->getQueueMetadata($queue);
 678            $this->verifyGetSetQueueMetadataWorker($res, $metadata);
 679          }
 680          catch (HTTP_Request2_LogicException $le) {
 681            $keypart = array_keys($metadata);
 682            $keypart = $keypart[0];
 683            if (!is_null($metadata) && count($metadata) > 0 && (substr($keypart, 0, 1) == '<')) {
 684                // Trying to pass bad metadata
 685            }            
 686            else {
 687                throw $le;
 688            }
 689         }
 690        }
 691        catch (ServiceException $e) {
 692            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 693                $this->assertEquals(500, $e->getCode(), 'getCode');
 694            } else {
 695                throw $e;
 696            }
 697        }
 698        // Clean up.
 699        $this->restProxy->deleteQueue($queue);
 700    }
 701
 702    /**
 703    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 704    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 705    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 706    */
 707    public function testCreateMessageEmpty() {
 708        $this->createMessageWorker('', QueueServiceFunctionalTestData::getSimpleCreateMessageOptions());
 709    }
 710
 711    /**
 712    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 713    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 714    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 715    */
 716    public function testCreateMessageUnicodeMessage() {
 717        $this->createMessageWorker('Some unicode: ' .
 718                chr(0xEB) . chr(0x8B) . chr(0xA4) . // \uB2E4 in UTF-8
 719                chr(0xEB) . chr(0xA5) . chr(0xB4) . // \uB974 in UTF-8
 720                chr(0xEB) . chr(0x8B) . chr(0xA4) . // \uB2E4 in UTF-8
 721                chr(0xEB) . chr(0x8A) . chr(0x94) . // \uB294 in UTF-8
 722                chr(0xD8) . chr(0xA5) .             // \u0625 in UTF-8
 723                ' ' . 
 724                chr(0xD9) . chr(0x8A) .             // \u064A in UTF-8
 725                chr(0xD8) . chr(0xAF) .             // \u062F in UTF-8
 726                chr(0xD9) . chr(0x8A) .             // \u064A in UTF-8
 727                chr(0xD9) . chr(0x88),              // \u0648 in UTF-8
 728                QueueServiceFunctionalTestData::getSimpleCreateMessageOptions());
 729        }
 730
 731    /**
 732    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 733    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 734    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 735    */
 736    public function testCreateMessageXmlMessage() {
 737        $this->createMessageWorker('Some HTML: <this><is></a>', QueueServiceFunctionalTestData::getSimpleCreateMessageOptions());
 738    }
 739
 740    /**
 741    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 742    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 743    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 744    */
 745    public function testCreateMessageWithSmallTTL() {
 746        $queue = QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES;
 747        $queue = $queue[0];
 748        $this->restProxy->clearMessages($queue);
 749        $messageText = QueueServiceFunctionalTestData::getSimpleMessageText();
 750
 751        $options = new CreateMessageOptions();
 752        $options->setVisibilityTimeoutInSeconds(2);
 753        $options->setTimeToLiveInSeconds('4');
 754
 755        $this->restProxy->createMessage($queue, $messageText, $options);
 756
 757        $lmr = $this->restProxy->listMessages($queue);
 758
 759        // No messages, because it is not visible for 2 seconds.
 760        $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 761        sleep(6);
 762        // Try again, passed the VisibilityTimeout has passed, but also the 4 second TTL has passed.
 763        $lmr = $this->restProxy->listMessages($queue);
 764
 765        $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 766
 767        $this->restProxy->clearMessages($queue);
 768    }
 769
 770    /**
 771    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 772    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 773    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 774    */
 775    public function testCreateMessage() {
 776        $interestingTimes = array( null, -1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, 1000 );
 777        foreach($interestingTimes as $timeToLiveInSeconds)  {
 778            foreach($interestingTimes as $visibilityTimeoutInSeconds)  {
 779                $timeout = null;
 780                $options = new CreateMessageOptions();
 781                $options->setTimeout($timeout);
 782
 783                $options->setTimeToLiveInSeconds($timeToLiveInSeconds);
 784                $options->setVisibilityTimeoutInSeconds($visibilityTimeoutInSeconds . '');
 785                $this->createMessageWorker(QueueServiceFunctionalTestData::getSimpleMessageText(), $options);
 786            }
 787        }
 788
 789        foreach($interestingTimes as $timeout)  {
 790            $timeToLiveInSeconds = 1000;
 791            $visibilityTimeoutInSeconds = QueueServiceFunctionalTestData::INTERESTING_TTL;
 792            $options = new CreateMessageOptions();
 793            $options->setTimeout($timeout);
 794
 795            $options->setTimeToLiveInSeconds($timeToLiveInSeconds . '');
 796            $options->setVisibilityTimeoutInSeconds($visibilityTimeoutInSeconds);
 797            $this->createMessageWorker(QueueServiceFunctionalTestData::getSimpleMessageText(), $options);
 798        }
 799    }
 800
 801    private function createMessageWorker($messageText, $options) {
 802        self::println( 'Trying $options: ' . self::tmptostring($options));
 803        $queue = QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES;
 804        $queue = $queue[0];
 805        $this->restProxy->clearMessages($queue);
 806
 807        try {
 808            if (is_null($options)) {
 809                $this->restProxy->createMessage($queue, $messageText);
 810            }
 811            else {
 812                $this->restProxy->createMessage($queue, $messageText, $options);
 813            }
 814
 815            if (is_null($options)) {
 816                $options = new CreateMessageOptions();
 817            }
 818
 819            if (!is_null($options->getVisibilityTimeoutInSeconds()) && $options->getVisibilityTimeoutInSeconds() < 0) {
 820                $this->assertTrue(false, 'Expect negative getVisibilityTimeoutInSeconds in $options to throw');
 821            }
 822            else if (!is_null($options->getTimeToLiveInSeconds()) && $options->getTimeToLiveInSeconds() <= 0) {
 823                $this->assertTrue(false, 'Expect negative getVisibilityTimeoutInSeconds in $options to throw');
 824            }
 825            else if (!is_null($options->getVisibilityTimeoutInSeconds()) &&
 826                    !is_null($options->getTimeToLiveInSeconds()) &&
 827                    $options->getVisibilityTimeoutInSeconds() > 0 &&
 828                    $options->getTimeToLiveInSeconds() <= $options->getVisibilityTimeoutInSeconds()) {
 829                $this->assertTrue(false, 'Expect getTimeToLiveInSeconds() <= getVisibilityTimeoutInSeconds in $options to throw');
 830            }
 831            else if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 832                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 833            }
 834
 835            // Check that the message matches
 836            $lmr = $this->restProxy->listMessages($queue);
 837            if (!is_null($options->getVisibilityTimeoutInSeconds()) && $options->getVisibilityTimeoutInSeconds() > 0) {
 838                $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 839                sleep(QueueServiceFunctionalTestData::INTERESTING_TTL);
 840                // Try again, not that the 4 second visibility has passed
 841                $lmr = $this->restProxy->listMessages($queue);
 842                if ($options->getVisibilityTimeoutInSeconds() > QueueServiceFunctionalTestData::INTERESTING_TTL) {
 843                    $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 844                }
 845                else {
 846                    $this->assertEquals(1, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 847                    $qm = $lmr->getQueueMessages();
 848                    $qm = $qm[0];
 849                    $this->assertEquals($messageText, $qm->getMessageText(), '$qm->getMessageText');
 850                }
 851            }
 852            else {
 853                $this->assertEquals(1, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 854                $qm = $lmr->getQueueMessages();
 855                $qm = $qm[0];
 856                $this->assertEquals($messageText, $qm->getMessageText(), '$qm->getMessageText');
 857            }
 858
 859        }
 860        catch (ServiceException $e) {
 861            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 862                $this->assertEquals(500, $e->getCode(), 'getCode');
 863            }
 864            else if (!is_null($options->getVisibilityTimeoutInSeconds()) && $options->getVisibilityTimeoutInSeconds() < 0) {
 865                // Trying to pass bad metadata
 866                $this->assertEquals(400, $e->getCode(), 'getCode');
 867            }
 868            else if (!is_null($options->getTimeToLiveInSeconds()) && $options->getTimeToLiveInSeconds() <= 0) {
 869                $this->assertEquals(400, $e->getCode(), 'getCode');
 870            }
 871            else if (!is_null($options->getVisibilityTimeoutInSeconds()) && !is_null($options->getTimeToLiveInSeconds()) && $options->getVisibilityTimeoutInSeconds() > 0 && $options->getTimeToLiveInSeconds() <= $options->getVisibilityTimeoutInSeconds()) {
 872                $this->assertEquals(400, $e->getCode(), 'getCode');
 873            }
 874            else {
 875                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 876            }
 877        }
 878        $this->restProxy->clearMessages($queue);
 879    }
 880
 881    /**
 882    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 883    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 884    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 885    * @covers WindowsAzure\Queue\QueueRestProxy::updateMessage
 886    */
 887    public function testUpdateMessageNoOptions() {
 888        $interestingVisibilityTimes = array(-1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, QueueServiceFunctionalTestData::INTERESTING_TTL * 2);
 889
 890        $startingMessage = new CreateMessageOptions();
 891        $startingMessage->setTimeout(QueueServiceFunctionalTestData::INTERESTING_TTL);
 892        $startingMessage->setTimeToLiveInSeconds(QueueServiceFunctionalTestData::INTERESTING_TTL * 1.5);
 893
 894        foreach($interestingVisibilityTimes as $visibilityTimeoutInSeconds)  {
 895            $this->updateMessageWorker(QueueServiceFunctionalTestData::getSimpleMessageText(), $startingMessage, $visibilityTimeoutInSeconds, null);
 896        }
 897    }
 898
 899    /**
 900    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
 901    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
 902    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
 903    * @covers WindowsAzure\Queue\QueueRestProxy::updateMessage
 904    */
 905    public function testUpdateMessage() {
 906        $interestingTimes = array(null, -1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, 1000);
 907        
 908        $interestingVisibilityTimes = array(-1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, QueueServiceFunctionalTestData::INTERESTING_TTL * 2);
 909
 910        $startingMessage = new CreateMessageOptions();        
 911        $startingMessage->setTimeout( QueueServiceFunctionalTestData::INTERESTING_TTL);
 912        $startingMessage->setTimeToLiveInSeconds(QueueServiceFunctionalTestData::INTERESTING_TTL * 1.5);
 913
 914        foreach($interestingTimes as $timeout)  {
 915            foreach($interestingVisibilityTimes as $visibilityTimeoutInSeconds)  {
 916                $options = new QueueServiceOptions();
 917                $options->setTimeout($timeout);
 918                $this->updateMessageWorker(QueueServiceFunctionalTestData::getSimpleMessageText(), $startingMessage, $visibilityTimeoutInSeconds, $options);
 919            }
 920        }
 921    }
 922
 923    private function updateMessageWorker($messageText, $startingMessage, $visibilityTimeoutInSeconds, $options) {
 924        self::println( 'Trying $options: ' . self::tmptostring($options) . 
 925                ' and $visibilityTimeoutInSeconds: ' . self::tmptostring($visibilityTimeoutInSeconds));
 926        $queue = QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES;
 927        $queue = $queue[0];
 928        $this->restProxy->clearMessages($queue);
 929
 930        $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText(), $startingMessage);
 931        $lmr = $this->restProxy->listMessages($queue);
 932        $m = $lmr->getQueueMessages();
 933        $m = $m[0];
 934
 935        try {
 936            if (is_null($options)) {
 937                $this->restProxy->updateMessage($queue, $m->getMessageId(), $m->getPopReceipt(), $messageText, $visibilityTimeoutInSeconds);
 938            }
 939            else {
 940                $this->restProxy->updateMessage($queue, $m->getMessageId(), $m->getPopReceipt(), $messageText, $visibilityTimeoutInSeconds, $options);
 941            }
 942
 943            if (is_null($options)) {
 944                $options = new CreateMessageOptions();
 945            }
 946
 947            if ($visibilityTimeoutInSeconds < 0) {
 948                $this->assertTrue(false, 'Expect negative getVisibilityTimeoutInSeconds in $options to throw');
 949            }
 950            else if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 951                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
 952            }
 953
 954            // Check that the message matches
 955            $lmr = $this->restProxy->listMessages($queue);
 956            if ($visibilityTimeoutInSeconds > 0) {
 957                $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 958                sleep(QueueServiceFunctionalTestData::INTERESTING_TTL);
 959                // Try again, not that the 4 second visibility has passed
 960                $lmr = $this->restProxy->listMessages($queue);
 961                if ($visibilityTimeoutInSeconds > QueueServiceFunctionalTestData::INTERESTING_TTL) {
 962                    $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 963                }
 964                else {
 965                    $this->assertEquals(1, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 966                    $qm = $lmr->getQueueMessages();
 967                    $qm = $qm[0];
 968                    $this->assertEquals($messageText, $qm->getMessageText(), '$qm->getMessageText');
 969                }
 970            }
 971            else {
 972                $this->assertEquals(1, count($lmr->getQueueMessages()), 'getQueueMessages() count');
 973                $qm = $lmr->getQueueMessages();
 974                $qm = $qm[0];
 975                $this->assertEquals($messageText, $qm->getMessageText(), '$qm->getMessageText');
 976            }
 977
 978        }
 979        catch (ServiceException $e) {
 980            if (is_null($options)) {
 981                $options = new CreateMessageOptions();
 982            }
 983
 984            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
 985                $this->assertEquals(500, $e->getCode(), 'getCode');
 986            }
 987            else if ($visibilityTimeoutInSeconds < 0) {
 988                // Trying to pass bad metadata
 989                $this->assertEquals(400, $e->getCode(), 'getCode');
 990            }
 991            else {
 992                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
 993            }
 994        }
 995        $this->restProxy->clearMessages($queue);
 996    }
 997
 998    /**
 999    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
1000    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
1001    * @covers WindowsAzure\Queue\QueueRestProxy::deleteMessage
1002    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
1003    */
1004    public function testDeleteMessageNoOptions() {
1005        $this->deleteMessageWorker(null);
1006    }
1007
1008    /**
1009    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
1010    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
1011    * @covers WindowsAzure\Queue\QueueRestProxy::deleteMessage
1012    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
1013    */
1014    public function testDeleteMessage() {
1015        $interestingTimes = array(null, -1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, 1000);
1016        foreach($interestingTimes as $timeout)  {
1017            $options = new QueueServiceOptions();
1018            $options->setTimeout($timeout);
1019            $this->deleteMessageWorker($options);
1020        }
1021    }
1022
1023    private function deleteMessageWorker($options) {
1024        self::println( 'Trying $options: ' . self::tmptostring($options));
1025        $queue = QueueServiceFunctionalTestData::$TEST_QUEUE_NAMES;
1026        $queue = $queue[0];
1027        $this->restProxy->clearMessages($queue);
1028
1029        $this->restProxy->createMessage($queue, 'test');
1030        $opts = new ListMessagesOptions();
1031        $opts->setVisibilityTimeoutInSeconds(QueueServiceFunctionalTestData::INTERESTING_TTL);
1032        $lmr = $this->restProxy->listMessages($queue, $opts);
1033        $m = $lmr->getQueueMessages();
1034        $m = $m[0];
1035
1036        try {
1037            if (is_null($options)) {
1038                $this->restProxy->deleteMessage($queue, $m->getMessageId(), $m->getPopReceipt());
1039            }
1040            else {
1041                $this->restProxy->deleteMessage($queue, $m->getMessageId(), $m->getPopReceipt(), $options);
1042            }
1043
1044            if (is_null($options)) {
1045                $options = new CreateMessageOptions();
1046            }
1047            else if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
1048                $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
1049            }
1050
1051            // Check that the message matches
1052            $lmr = $this->restProxy->listMessages($queue);
1053            $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
1054
1055            // Wait until the popped message should be visible again.
1056            sleep(QueueServiceFunctionalTestData::INTERESTING_TTL + 1);
1057            // Try again, to make sure the message really is gone.
1058            $lmr = $this->restProxy->listMessages($queue);
1059            $this->assertEquals(0, count($lmr->getQueueMessages()), 'getQueueMessages() count');
1060        }
1061        catch (ServiceException $e) {
1062            if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
1063                $this->assertEquals(500, $e->getCode(), 'getCode');
1064            }
1065            else  {
1066                $this->assertNull($e, 'Expect positive timeouts in $options to be fine');
1067            }
1068        }
1069        $this->restProxy->clearMessages($queue);
1070    }
1071
1072    /**
1073    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
1074    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
1075    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
1076    * @covers WindowsAzure\Queue\QueueRestProxy::peekMessages
1077    */
1078    public function testListMessagesNoOptions() {
1079        $this->listMessagesWorker(new ListMessagesOptions());
1080    }
1081
1082    /**
1083    * @covers WindowsAzure\Queue\QueueRestProxy::clearMessages
1084    * @covers WindowsAzure\Queue\QueueRestProxy::createMessage
1085    * @covers WindowsAzure\Queue\QueueRestProxy::listMessages
1086    * @covers WindowsAzure\Queue\QueueRestProxy::peekMessages
1087    */
1088    public function testListMessages() {
1089        $interestingTimes = array(null, -1, 0, QueueServiceFunctionalTestData::INTERESTING_TTL, 1000);
1090        $interestingNums = array(null, -1, 0, 2, 10, 1000);
1091        foreach($interestingNums as $numberOfMessages)  {
1092            foreach($interestingTimes as $visibilityTimeoutInSeconds)  {

Large files files are truncated, but you can click here to view the full file