PageRenderTime 64ms CodeModel.GetById 20ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 1ms

/codes-php/phpjakarta/tests/functional/WindowsAzure/Queue/QueueServiceFunctionalTest.php

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

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