MessageTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /*
  23. * The adapter test class provides a universal test class for all of the
  24. * abstract methods.
  25. *
  26. * All methods marked not supported are explictly checked for for throwing
  27. * an exception.
  28. */
  29. /** Zend_Queue */
  30. require_once 'Zend/Queue.php';
  31. /** Zend_Queue */
  32. require_once 'Zend/Queue/Message.php';
  33. /** Zend_Queue_Adapter_Array */
  34. require_once 'Zend/Queue/Adapter/Array.php';
  35. /** Zend_Queue_Adapter_Null */
  36. require_once 'Zend/Queue/Adapter/Null.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Queue
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Queue
  44. */
  45. class Zend_Queue_MessageTest extends PHPUnit_Framework_TestCase
  46. {
  47. protected function setUp()
  48. {
  49. // Test Zend_Config
  50. $this->options = array(
  51. 'name' => 'queue1',
  52. 'params' => array(),
  53. );
  54. $this->queue = new Zend_Queue('array', $this->options);
  55. $this->data = array(
  56. 'id' => 123,
  57. 'handle' => 567,
  58. 'body' => 'Hello world' // This is my 2524'th time writing that.
  59. );
  60. $this->options = array(
  61. 'queue' => $this->queue,
  62. 'data' => $this->data,
  63. );
  64. $this->message = new Zend_Queue_Message($this->options);
  65. }
  66. protected function tearDown()
  67. {
  68. }
  69. public function testConstruct()
  70. {
  71. try {
  72. $message = new Zend_Queue_Message($this->options);
  73. $this->assertTrue(true);
  74. } catch (Exception $e) {
  75. $this->fail('should have gotten a valid object');
  76. }
  77. // parameter verification
  78. try {
  79. $config2 = $this->options;
  80. $config2['queue'] = 'weee';
  81. $message = new Zend_Queue_Message($config2);
  82. $this->fail('should have thrown an exception bad queue var');
  83. } catch (Exception $e) {
  84. $this->assertTrue(true);
  85. }
  86. try {
  87. $config2 = $this->options;
  88. $config2['data'] = 'weee';
  89. $message = new Zend_Queue_Message($config2);
  90. $this->fail('should have thrown an exception bad queue var');
  91. } catch (Exception $e) {
  92. $this->assertTrue(true);
  93. }
  94. }
  95. public function testMagic()
  96. {
  97. $this->assertEquals(123, $this->message->__get('id'));
  98. $this->assertEquals(123, $this->message->id);
  99. $this->assertEquals('Hello world', $this->message->body);
  100. $this->message->__set('id', 'abc');
  101. $this->assertEquals('abc', $this->message->__get('id'));
  102. $this->assertTrue($this->message->__isset('id'));
  103. try {
  104. $this->message->__get('hello world');
  105. $this->fail('key is NOT in variable, should have thrown an exception');
  106. } catch (Exception $e) {
  107. $this->assertTrue(true);
  108. }
  109. try {
  110. $this->message->__set('hello world', 'good bye');
  111. $this->fail('key is NOT in variable, should have thrown an exception');
  112. } catch (Exception $e) {
  113. $this->assertTrue(true);
  114. }
  115. try {
  116. $message = new Zend_Queue_Message($this->options);
  117. $this->assertTrue(true);
  118. } catch (Exception $e) {
  119. $this->fail('should have gotten a valid object');
  120. }
  121. // parameter verification
  122. try {
  123. $config2 = $this->options;
  124. $config2['queue'] = 'weee';
  125. $message = new Zend_Queue_Message($config2);
  126. $this->fail('should have thrown an exception bad queue var');
  127. } catch (Exception $e) {
  128. $this->assertTrue(true);
  129. }
  130. try {
  131. $config2 = $this->options;
  132. $config2['data'] = 'weee';
  133. $message = new Zend_Queue_Message($config2);
  134. $this->fail('should have thrown an exception bad queue var');
  135. } catch (Exception $e) {
  136. $this->assertTrue(true);
  137. }
  138. }
  139. public function test_set_getQueue()
  140. {
  141. $this->assertTrue($this->message->getQueue() instanceof Zend_Queue);
  142. $class = $this->message->getQueueClass();
  143. $this->assertEquals('Zend_Queue', $class);
  144. $this->assertTrue($this->message->setQueue($this->message->getQueue()));
  145. // parameter verification
  146. try {
  147. $null = new Zend_Queue('Null', array());
  148. $this->message->setQueue($null);
  149. $this->fail('invalid class passed to setQueue()');
  150. } catch (Exception $e) {
  151. $this->assertTrue(true);
  152. }
  153. }
  154. public function test_array()
  155. {
  156. $array = $this->message->toArray();
  157. $this->assertTrue(is_array($array));
  158. $array['id'] = 'hello';
  159. $this->message->setFromArray($array);
  160. $this->assertEquals('hello', $this->message->id);
  161. }
  162. public function test_magic()
  163. {
  164. $this->assertTrue(is_array($this->message->__sleep()));
  165. $message = serialize($this->message);
  166. $woken = unserialize($message);
  167. $this->assertEquals($this->message->body, $woken->body);
  168. }
  169. }