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