MessageTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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-2009 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. /** PHPUnit Test Case */
  30. require_once 'PHPUnit/Framework/TestCase.php';
  31. /** TestHelp.php */
  32. require_once dirname(__FILE__) . '/../../TestHelper.php';
  33. /** Zend_Queue */
  34. require_once 'Zend/Queue.php';
  35. /** Zend_Queue */
  36. require_once 'Zend/Queue/Message.php';
  37. /** Zend_Queue_Adapter_Array */
  38. require_once 'Zend/Queue/Adapter/Array.php';
  39. /** Zend_Queue_Adapter_Null */
  40. require_once 'Zend/Queue/Adapter/Null.php';
  41. /**
  42. * @category Zend
  43. * @package Zend_Queue
  44. * @subpackage UnitTests
  45. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. * @group Zend_Queue
  48. */
  49. class Zend_Queue_MessageTest extends PHPUnit_Framework_TestCase
  50. {
  51. protected function setUp()
  52. {
  53. // Test Zend_Config
  54. $this->options = array(
  55. 'name' => 'queue1',
  56. 'params' => array(),
  57. );
  58. $this->queue = new Zend_Queue('array', $this->options);
  59. $this->data = array(
  60. 'id' => 123,
  61. 'handle' => 567,
  62. 'body' => 'Hello world' // This is my 2524'th time writing that.
  63. );
  64. $this->options = array(
  65. 'queue' => $this->queue,
  66. 'data' => $this->data,
  67. );
  68. $this->message = new Zend_Queue_Message($this->options);
  69. }
  70. protected function tearDown()
  71. {
  72. }
  73. public function testConstruct()
  74. {
  75. try {
  76. $message = new Zend_Queue_Message($this->options);
  77. $this->assertTrue(true);
  78. } catch (Exception $e) {
  79. $this->fail('should have gotten a valid object');
  80. }
  81. // parameter verification
  82. try {
  83. $config2 = $this->options;
  84. $config2['queue'] = 'weee';
  85. $message = new Zend_Queue_Message($config2);
  86. $this->fail('should have thrown an exception bad queue var');
  87. } catch (Exception $e) {
  88. $this->assertTrue(true);
  89. }
  90. try {
  91. $config2 = $this->options;
  92. $config2['data'] = 'weee';
  93. $message = new Zend_Queue_Message($config2);
  94. $this->fail('should have thrown an exception bad queue var');
  95. } catch (Exception $e) {
  96. $this->assertTrue(true);
  97. }
  98. }
  99. public function testMagic()
  100. {
  101. $this->assertEquals(123, $this->message->__get('id'));
  102. $this->assertEquals(123, $this->message->id);
  103. $this->assertEquals('Hello world', $this->message->body);
  104. $this->message->__set('id', 'abc');
  105. $this->assertEquals('abc', $this->message->__get('id'));
  106. $this->assertTrue($this->message->__isset('id'));
  107. try {
  108. $this->message->__get('hello world');
  109. $this->fail('key is NOT in variable, should have thrown an exception');
  110. } catch (Exception $e) {
  111. $this->assertTrue(true);
  112. }
  113. try {
  114. $this->message->__set('hello world', 'good bye');
  115. $this->fail('key is NOT in variable, should have thrown an exception');
  116. } catch (Exception $e) {
  117. $this->assertTrue(true);
  118. }
  119. try {
  120. $message = new Zend_Queue_Message($this->options);
  121. $this->assertTrue(true);
  122. } catch (Exception $e) {
  123. $this->fail('should have gotten a valid object');
  124. }
  125. // parameter verification
  126. try {
  127. $config2 = $this->options;
  128. $config2['queue'] = 'weee';
  129. $message = new Zend_Queue_Message($config2);
  130. $this->fail('should have thrown an exception bad queue var');
  131. } catch (Exception $e) {
  132. $this->assertTrue(true);
  133. }
  134. try {
  135. $config2 = $this->options;
  136. $config2['data'] = 'weee';
  137. $message = new Zend_Queue_Message($config2);
  138. $this->fail('should have thrown an exception bad queue var');
  139. } catch (Exception $e) {
  140. $this->assertTrue(true);
  141. }
  142. }
  143. public function test_set_getQueue()
  144. {
  145. $this->assertTrue($this->message->getQueue() instanceof Zend_Queue);
  146. $class = $this->message->getQueueClass();
  147. $this->assertEquals('Zend_Queue', $class);
  148. $this->assertTrue($this->message->setQueue($this->message->getQueue()));
  149. // parameter verification
  150. try {
  151. $null = new Zend_Queue('Null', array());
  152. $this->message->setQueue($null);
  153. $this->fail('invalid class passed to setQueue()');
  154. } catch (Exception $e) {
  155. $this->assertTrue(true);
  156. }
  157. }
  158. public function test_array()
  159. {
  160. $array = $this->message->toArray();
  161. $this->assertTrue(is_array($array));
  162. $array['id'] = 'hello';
  163. $this->message->setFromArray($array);
  164. $this->assertEquals('hello', $this->message->id);
  165. }
  166. public function test_magic()
  167. {
  168. $this->assertTrue(is_array($this->message->__sleep()));
  169. $message = serialize($this->message);
  170. $woken = unserialize($message);
  171. $this->assertEquals($this->message->body, $woken->body);
  172. }
  173. }