Event.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Event\Test\Unit;
  36. use Hoa\Event as LUT;
  37. use Hoa\Event\Event as SUT;
  38. use Hoa\Test;
  39. /**
  40. * Class \Hoa\Event\Test\Unit\Event.
  41. *
  42. * Test suite of the event class.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Event extends Test\Unit\Suite
  48. {
  49. public function case_multiton()
  50. {
  51. $this
  52. ->given($eventId = 'hoa://Event/Test')
  53. ->when($result = SUT::getEvent($eventId))
  54. ->then
  55. ->object($result)
  56. ->isInstanceOf('Hoa\Event\Event')
  57. ->object(SUT::getEvent($eventId))
  58. ->isIdenticalTo($result);
  59. }
  60. public function case_register_source_instance()
  61. {
  62. $this
  63. ->given(
  64. $eventId = 'hoa://Event/Test',
  65. $source = new \Mock\Hoa\Event\Source()
  66. )
  67. ->when($result = SUT::register($eventId, $source))
  68. ->then
  69. ->variable($result)
  70. ->isNull()
  71. ->boolean(SUT::eventExists($eventId))
  72. ->isTrue();
  73. }
  74. public function case_register_source_name()
  75. {
  76. $this
  77. ->given(
  78. $eventId = 'hoa://Event/Test',
  79. $source = 'Mock\Hoa\Event\Source'
  80. )
  81. ->when($result = SUT::register($eventId, $source))
  82. ->then
  83. ->variable($result)
  84. ->isNull()
  85. ->boolean(SUT::eventExists($eventId))
  86. ->isTrue();
  87. }
  88. public function case_register_redeclare()
  89. {
  90. $this
  91. ->given(
  92. $eventId = 'hoa://Event/Test',
  93. $source = new \Mock\Hoa\Event\Source(),
  94. SUT::register($eventId, $source)
  95. )
  96. ->exception(function () use ($eventId, $source) {
  97. SUT::register($eventId, $source);
  98. })
  99. ->isInstanceOf('Hoa\Event\Exception');
  100. }
  101. public function case_register_not_a_source_instance()
  102. {
  103. $this
  104. ->given(
  105. $eventId = 'hoa://Event/Test',
  106. $source = new \StdClass()
  107. )
  108. ->exception(function () use ($eventId, $source) {
  109. $result = SUT::register($eventId, $source);
  110. })
  111. ->isInstanceOf('Hoa\Event\Exception');
  112. }
  113. public function case_register_not_a_source_name()
  114. {
  115. $this
  116. ->given(
  117. $eventId = 'hoa://Event/Test',
  118. $source = 'StdClass'
  119. )
  120. ->exception(function () use ($eventId, $source) {
  121. $result = SUT::register($eventId, $source);
  122. })
  123. ->isInstanceOf('Hoa\Event\Exception');
  124. }
  125. public function case_unregister()
  126. {
  127. $this
  128. ->given(
  129. $eventId = 'hoa://Event/Test',
  130. $source = new \Mock\Hoa\Event\Source(),
  131. SUT::register($eventId, $source)
  132. )
  133. ->when($result = SUT::unregister($eventId))
  134. ->then
  135. ->boolean(SUT::eventExists($eventId))
  136. ->isFalse();
  137. }
  138. public function case_unregister_hard()
  139. {
  140. $this
  141. ->given(
  142. $eventId = 'hoa://Event/Test',
  143. $source = new \Mock\Hoa\Event\Source(),
  144. SUT::register($eventId, $source),
  145. $event = SUT::getEvent($eventId)
  146. )
  147. ->when($result = SUT::unregister($eventId, true))
  148. ->then
  149. ->boolean(SUT::eventExists($eventId))
  150. ->isFalse()
  151. ->object(SUT::getEvent($eventId))
  152. ->isNotIdenticalTo($event);
  153. }
  154. public function case_unregister_not_registered()
  155. {
  156. $this
  157. ->given($eventId = 'hoa://Event/Test')
  158. ->when($result = SUT::unregister($eventId))
  159. ->then
  160. ->variable($result)
  161. ->isNull();
  162. }
  163. public function case_attach()
  164. {
  165. $this
  166. ->given(
  167. $event = SUT::getEvent('hoa://Event/Test'),
  168. $callable = function () { }
  169. )
  170. ->when($result = $event->attach($callable))
  171. ->then
  172. ->object($result)
  173. ->isIdenticalTo($event)
  174. ->boolean($event->isListened())
  175. ->isTrue();
  176. }
  177. public function case_detach()
  178. {
  179. $this
  180. ->given(
  181. $event = SUT::getEvent('hoa://Event/Test'),
  182. $callable = function () { },
  183. $event->attach($callable)
  184. )
  185. ->when($result = $event->detach($callable))
  186. ->then
  187. ->object($result)
  188. ->isIdenticalTo($event)
  189. ->boolean($event->isListened())
  190. ->isFalse();
  191. }
  192. public function case_detach_unattached()
  193. {
  194. $this
  195. ->given(
  196. $event = SUT::getEvent('hoa://Event/Test'),
  197. $callable = function () { }
  198. )
  199. ->when($result = $event->detach($callable))
  200. ->then
  201. ->object($result)
  202. ->isIdenticalTo($event)
  203. ->boolean($event->isListened())
  204. ->isFalse();
  205. }
  206. public function case_is_listened()
  207. {
  208. $this
  209. ->given($event = SUT::getEvent('hoa://Event/Test'))
  210. ->when($result = $event->isListened())
  211. ->then
  212. ->boolean($event->isListened())
  213. ->isFalse();
  214. }
  215. public function case_notify()
  216. {
  217. $self = $this;
  218. $this
  219. ->given(
  220. $eventId = 'hoa://Event/Test',
  221. $source = new \Mock\Hoa\Event\Source(),
  222. $bucket = new LUT\Bucket(),
  223. SUT::register($eventId, $source),
  224. SUT::getEvent($eventId)->attach(
  225. function (LUT\Bucket $receivedBucket) use ($self, $source, $bucket, &$called) {
  226. $called = true;
  227. $this
  228. ->object($receivedBucket)
  229. ->isIdenticalTo($bucket)
  230. ->object($receivedBucket->getSource())
  231. ->isIdenticalTo($source);
  232. }
  233. )
  234. )
  235. ->when($result = SUT::notify($eventId, $source, $bucket))
  236. ->then
  237. ->variable($result)
  238. ->isNull()
  239. ->boolean($called)
  240. ->isTrue();
  241. }
  242. public function case_notify_unregistered_event_id()
  243. {
  244. $this
  245. ->given(
  246. $eventId = 'hoa://Event/Test',
  247. $source = new \Mock\Hoa\Event\Source(),
  248. $data = new LUT\Bucket()
  249. )
  250. ->exception(function () use ($eventId, $source, $data) {
  251. SUT::notify($eventId, $source, $data);
  252. })
  253. ->isInstanceOf('Hoa\Event\Exception');
  254. }
  255. public function case_event_exists()
  256. {
  257. $this
  258. ->given(
  259. $eventId = 'hoa://Event/Test',
  260. $source = new \Mock\Hoa\Event\Source(),
  261. SUT::register($eventId, $source)
  262. )
  263. ->when($result = SUT::eventExists($eventId))
  264. ->then
  265. ->boolean($result)
  266. ->isTrue();
  267. }
  268. public function case_event_not_exists()
  269. {
  270. $this
  271. ->given($eventId = 'hoa://Event/Test')
  272. ->when($result = SUT::eventExists($eventId))
  273. ->then
  274. ->boolean($result)
  275. ->isFalse();
  276. }
  277. }