/Phly_PubSub/tests/Phly/PubSub/ProviderTest.php

https://github.com/emjliano/phly · PHP · 159 lines · 114 code · 20 blank · 25 comment · 4 complexity · a8c50b0078fe11f641cb72b4cf6e100a MD5 · raw file

  1. <?php
  2. /**
  3. * Phly - PHp LibrarY
  4. *
  5. * @category Phly
  6. * @package Phly_PubSub
  7. * @subpackage Test
  8. * @copyright Copyright (C) 2008 - Present, Matthew Weier O'Phinney
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  10. * @license New BSD {@link http://mwop.net/license}
  11. */
  12. // Call Phly_PubSub_ProviderTest::main() if this source file is executed directly.
  13. if (!defined("PHPUnit_MAIN_METHOD")) {
  14. define("PHPUnit_MAIN_METHOD", "Phly_PubSub_ProviderTest::main");
  15. }
  16. /**
  17. * Test helper
  18. */
  19. require_once dirname(__FILE__) . '/../../TestHelper.php';
  20. /**
  21. * Phly_PubSub
  22. */
  23. require_once 'Phly/PubSub/Provider.php';
  24. /**
  25. * @category Phly
  26. * @package Phly_PubSub
  27. * @subpackage Test
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
  30. */
  31. class Phly_PubSub_ProviderTest extends PHPUnit_Framework_TestCase
  32. {
  33. public static function main()
  34. {
  35. $suite = new PHPUnit_Framework_TestSuite("Phly_PubSub_ProviderTest");
  36. $result = PHPUnit_TextUI_TestRunner::run($suite);
  37. }
  38. public function setUp()
  39. {
  40. if (isset($this->message)) {
  41. unset($this->message);
  42. }
  43. $this->provider = new Phly_PubSub_Provider;
  44. }
  45. public function tearDown()
  46. {
  47. }
  48. public function testSubscribeShouldReturnHandle()
  49. {
  50. $handle = $this->provider->subscribe('test', $this, __METHOD__);
  51. $this->assertTrue($handle instanceof Phly_PubSub_Handle);
  52. }
  53. public function testSubscribeShouldAddHandleToTopic()
  54. {
  55. $handle = $this->provider->subscribe('test', $this, __METHOD__);
  56. $handles = $this->provider->getSubscribedHandles('test');
  57. $this->assertEquals(1, count($handles));
  58. $this->assertContains($handle, $handles);
  59. }
  60. public function testSubscribeShouldAddTopicIfItDoesNotExist()
  61. {
  62. $topics = $this->provider->getTopics();
  63. $this->assertTrue(empty($topics), var_export($topics, 1));
  64. $handle = $this->provider->subscribe('test', $this, __METHOD__);
  65. $topics = $this->provider->getTopics();
  66. $this->assertFalse(empty($topics));
  67. $this->assertContains('test', $topics);
  68. }
  69. public function testUnsubscribeShouldRemoveHandleFromTopic()
  70. {
  71. $handle = $this->provider->subscribe('test', $this, __METHOD__);
  72. $handles = $this->provider->getSubscribedHandles('test');
  73. $this->assertContains($handle, $handles);
  74. $this->provider->unsubscribe($handle);
  75. $handles = $this->provider->getSubscribedHandles('test');
  76. $this->assertNotContains($handle, $handles);
  77. }
  78. public function testUnsubscribeShouldReturnFalseIfTopicDoesNotExist()
  79. {
  80. $handle = $this->provider->subscribe('test', $this, __METHOD__);
  81. $this->provider->clearHandles('test');
  82. $this->assertFalse($this->provider->unsubscribe($handle));
  83. }
  84. public function testUnsubscribeShouldReturnFalseIfHandleDoesNotExist()
  85. {
  86. $handle1 = $this->provider->subscribe('test', $this, __METHOD__);
  87. $this->provider->clearHandles('test');
  88. $handle2 = $this->provider->subscribe('test', $this, 'handleTestTopic');
  89. $this->assertFalse($this->provider->unsubscribe($handle1));
  90. }
  91. public function testRetrievingSubscribedHandlesShouldReturnEmptyArrayWhenTopicDoesNotExist()
  92. {
  93. $handles = $this->provider->getSubscribedHandles('test');
  94. $this->assertTrue(empty($handles));
  95. }
  96. public function testPublishShouldNotifySubscribedHandlers()
  97. {
  98. $handle = $this->provider->subscribe('test', $this, 'handleTestTopic');
  99. $this->provider->publish('test', 'test message');
  100. $this->assertEquals('test message', $this->message);
  101. }
  102. public function testPublishShouldReturnTheReturnValueOfTheLastInvokedSubscriber()
  103. {
  104. $this->provider->subscribe('string.transform', 'trim');
  105. $this->provider->subscribe('string.transform', 'str_rot13');
  106. $value = $this->provider->publish('string.transform', ' foo ');
  107. $this->assertEquals(str_rot13(' foo '), $value);
  108. }
  109. public function testPublishUntilShouldReturnAsSoonAsCallbackReturnsTrue()
  110. {
  111. $this->provider->subscribe('foo.bar', 'strpos');
  112. $this->provider->subscribe('foo.bar', 'strstr');
  113. $value = $this->provider->publishUntil(
  114. array($this, 'evaluateStringCallback'),
  115. 'foo.bar',
  116. 'foo', 'f'
  117. );
  118. $this->assertSame(0, $value);
  119. }
  120. public function testFilterShouldPassReturnValueOfEachSubscriberToNextSubscriber()
  121. {
  122. $this->provider->subscribe('string.transform', 'trim');
  123. $this->provider->subscribe('string.transform', 'str_rot13');
  124. $value = $this->provider->filter('string.transform', ' foo ');
  125. $this->assertEquals(str_rot13('foo'), $value);
  126. }
  127. public function handleTestTopic($message)
  128. {
  129. $this->message = $message;
  130. }
  131. public function evaluateStringCallback($value)
  132. {
  133. return (!$value);
  134. }
  135. }
  136. // Call Phly_PubSub_ProviderTest::main() if this source file is executed directly.
  137. if (PHPUnit_MAIN_METHOD == "Phly_PubSub_ProviderTest::main") {
  138. Phly_PubSub_ProviderTest::main();
  139. }