Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 173 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ChannelHistoryTest | |
0.00% |
0 / 172 |
|
0.00% |
0 / 9 |
992 | |
0.00% |
0 / 1 |
| setUpBeforeClass | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| tearDownAfterClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| testPublishEventsAndCheckOrderForwards | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| testPublishEventsAndCheckOrderBackwards | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| testDefaults | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| testPublishEventsGetLimitedHistoryAndCheckOrderForwards | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
30 | |||
| testPublishEventsGetLimitedHistoryAndCheckOrderBackwards | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
30 | |||
| testPublishEventsAndCheckExpectedHistoryInTimeSlicesForwards | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
| testPublishEventsAndCheckExpectedHistoryInTimeSlicesBackwards | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | namespace tests; |
| 3 | use Ably\AblyRest; |
| 4 | use Ably\Channel; |
| 5 | use Ably\Models\Message; |
| 6 | |
| 7 | require_once __DIR__ . '/factories/TestApp.php'; |
| 8 | |
| 9 | class ChannelHistoryTest extends \PHPUnit_Framework_TestCase { |
| 10 | |
| 11 | protected static $testApp; |
| 12 | protected static $defaultOptions; |
| 13 | protected static $ably; |
| 14 | |
| 15 | public static function setUpBeforeClass() { |
| 16 | self::$testApp = new TestApp(); |
| 17 | self::$defaultOptions = self::$testApp->getOptions(); |
| 18 | self::$ably = new AblyRest( array_merge( self::$defaultOptions, [ |
| 19 | 'key' => self::$testApp->getAppKeyDefault()->string, |
| 20 | ] ) ); |
| 21 | } |
| 22 | |
| 23 | public static function tearDownAfterClass() { |
| 24 | self::$testApp->release(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Publish events and check expected order (forwards) |
| 29 | */ |
| 30 | public function testPublishEventsAndCheckOrderForwards() { |
| 31 | // publish some messages |
| 32 | $history1 = self::$ably->channel('persisted:history1'); |
| 33 | |
| 34 | $msgsToSend = []; |
| 35 | for ( $i = 0; $i < 50; $i++ ) { |
| 36 | $msg = new Message(); |
| 37 | $msg->name = 'history'.$i; |
| 38 | $msg->data = (string) $i; |
| 39 | $msgsToSend[] = $msg; |
| 40 | } |
| 41 | $history1->publish( $msgsToSend ); |
| 42 | |
| 43 | // get the history for this channel |
| 44 | $messages = $history1->history( ['direction' => 'forwards'] ); |
| 45 | $this->assertEquals( 50, count($messages->items), 'Expected 50 messages' ); |
| 46 | |
| 47 | // verify message order |
| 48 | $actual_message_history = []; |
| 49 | foreach ($messages->items as $msg) { |
| 50 | $actual_message_history[] = (int) $msg->data; |
| 51 | } |
| 52 | $expected_message_history = range(0, 49); |
| 53 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in forward order'); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Publish events and check expected order (backwards) |
| 58 | */ |
| 59 | public function testPublishEventsAndCheckOrderBackwards() { |
| 60 | // publish some messages |
| 61 | $history2 = self::$ably->channel('persisted:history2'); |
| 62 | |
| 63 | $msgsToSend = []; |
| 64 | for ( $i = 0; $i < 50; $i++ ) { |
| 65 | $msg = new Message(); |
| 66 | $msg->name = 'history'.$i; |
| 67 | $msg->data = (string) $i; |
| 68 | $msgsToSend[] = $msg; |
| 69 | } |
| 70 | $history2->publish( $msgsToSend ); |
| 71 | |
| 72 | $messages = $history2->history( ['direction' => 'backwards'] ); |
| 73 | $this->assertEquals( 50, count($messages->items), 'Expected 50 messages' ); |
| 74 | |
| 75 | // verify message order |
| 76 | $actual_message_history = []; |
| 77 | foreach ($messages->items as $msg) { |
| 78 | $actual_message_history[] = (int) $msg->data; |
| 79 | } |
| 80 | $expected_message_history = range(49, 0, -1); |
| 81 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in backward order'); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Test default limit (100 messages) and default order (backwards) |
| 86 | */ |
| 87 | public function testDefaults() { |
| 88 | $channel = self::$ably->channel('persisted:history_def_limit'); |
| 89 | |
| 90 | $msgsToSend = []; |
| 91 | for ( $i = 0; $i < 101; $i++ ) { |
| 92 | $msg = new Message(); |
| 93 | $msg->data = (string) $i; |
| 94 | $msgsToSend[] = $msg; |
| 95 | } |
| 96 | $channel->publish( $msgsToSend ); |
| 97 | |
| 98 | $messages = $channel->history(); |
| 99 | $this->assertEquals( 100, count( $messages->items ), 'Expected 100 messages' ); |
| 100 | |
| 101 | // verify message order |
| 102 | $actual_message_history = []; |
| 103 | foreach ($messages->items as $msg) { |
| 104 | $actual_message_history[] = (int) $msg->data; |
| 105 | } |
| 106 | $expected_message_history = range(100, 1, -1); |
| 107 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in backward order'); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Publish events, get limited history, check expected order (forwards) and pagination |
| 112 | */ |
| 113 | public function testPublishEventsGetLimitedHistoryAndCheckOrderForwards() { |
| 114 | // publish some messages |
| 115 | $history3 = self::$ably->channel( 'persisted:history3' ); |
| 116 | |
| 117 | $msgsToSend = []; |
| 118 | for ( $i = 0; $i < 50; $i++ ) { |
| 119 | $msg = new Message(); |
| 120 | $msg->name = 'history'.$i; |
| 121 | $msg->data = (string) $i; |
| 122 | $msgsToSend[] = $msg; |
| 123 | } |
| 124 | $history3->publish( $msgsToSend ); |
| 125 | |
| 126 | $messages = $history3->history( ['direction' => 'forwards', 'limit' => 25] ); |
| 127 | $this->assertEquals( 25, count($messages->items), 'Expected 25 messages' ); |
| 128 | |
| 129 | // verify message order |
| 130 | $actual_message_history = []; |
| 131 | foreach ($messages->items as $msg) { |
| 132 | $actual_message_history[] = (int) $msg->data; |
| 133 | } |
| 134 | $expected_message_history = range(0, 24); |
| 135 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in forward order'); |
| 136 | |
| 137 | // check pagination |
| 138 | $this->assertTrue( $messages->isPaginated(), 'Expected messages to be paginated' ); |
| 139 | $this->assertTrue( $messages->hasFirst(), 'Expected to have first page' ); |
| 140 | $this->assertTrue( $messages->hasNext(), 'Expected to have next page' ); |
| 141 | $this->assertFalse( $messages->isLast(), 'Expected not to be the last page' ); |
| 142 | |
| 143 | // next page |
| 144 | $messages2 = $messages->next(); |
| 145 | $this->assertEquals( 25, count($messages2->items), 'Expected 25 messages on 2nd page' ); |
| 146 | |
| 147 | $actual_message_history2 = []; |
| 148 | foreach ($messages2->items as $msg) { |
| 149 | $actual_message_history2[] = (int) $msg->data; |
| 150 | } |
| 151 | $expected_message_history2 = range(25, 49); |
| 152 | $this->assertEquals( $expected_message_history2, $actual_message_history2, 'Expect messages in forward order on 2nd page'); |
| 153 | |
| 154 | $this->assertTrue( $messages2->isPaginated(), 'Expected messages to be paginated' ); |
| 155 | $this->assertTrue( $messages2->hasFirst(), 'Expected to have first page' ); |
| 156 | $this->assertFalse( $messages2->hasNext(), 'Expected not to have next page' ); |
| 157 | $this->assertTrue( $messages2->isLast(), 'Expected to be the last page' ); |
| 158 | $this->assertNull( $messages2->next(), 'Expected the 3rd page to be null' ); |
| 159 | |
| 160 | // get the first page from the 2nd page |
| 161 | $messages1 = $messages2->first(); |
| 162 | $this->assertEquals( 25, count($messages1->items), 'Expected 25 messages on the 1st page' ); |
| 163 | |
| 164 | $actual_message_history1 = []; |
| 165 | foreach ($messages1->items as $msg) { |
| 166 | $actual_message_history1[] = (int) $msg->data; |
| 167 | } |
| 168 | $this->assertEquals( $expected_message_history, $actual_message_history1, 'Expect messages to match the first page'); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Publish events, get limited history, check expected order (backwards) and pagination |
| 174 | */ |
| 175 | public function testPublishEventsGetLimitedHistoryAndCheckOrderBackwards() { |
| 176 | // publish some messages |
| 177 | $history4 = self::$ably->channel('persisted:history4'); |
| 178 | |
| 179 | $msgsToSend = []; |
| 180 | for ( $i = 0; $i < 50; $i++ ) { |
| 181 | $msg = new Message(); |
| 182 | $msg->name = 'history'.$i; |
| 183 | $msg->data = (string) $i; |
| 184 | $msgsToSend[] = $msg; |
| 185 | } |
| 186 | $history4->publish( $msgsToSend ); |
| 187 | |
| 188 | $messages = $history4->history( ['direction' => 'backwards', 'limit' => 25] ); |
| 189 | $this->assertEquals( 25, count($messages->items), 'Expected 25 messages' ); |
| 190 | |
| 191 | // verify message order |
| 192 | $actual_message_history = []; |
| 193 | foreach ($messages->items as $msg) { |
| 194 | $actual_message_history[] = (int) $msg->data; |
| 195 | } |
| 196 | $expected_message_history = range(49, 25, -1); |
| 197 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in backward order'); |
| 198 | |
| 199 | // check pagination |
| 200 | $this->assertTrue( $messages->isPaginated(), 'Expected messages to be paginated' ); |
| 201 | $this->assertTrue( $messages->hasFirst(), 'Expected to have first page' ); |
| 202 | $this->assertTrue( $messages->hasNext(), 'Expected to have next page' ); |
| 203 | $this->assertFalse( $messages->isLast(), 'Expected not to be the last page' ); |
| 204 | |
| 205 | // next page |
| 206 | $messages2 = $messages->next(); |
| 207 | $this->assertEquals( 25, count($messages2->items), 'Expected 25 messages on 2nd page' ); |
| 208 | |
| 209 | $actual_message_history2 = []; |
| 210 | foreach ($messages2->items as $msg) { |
| 211 | $actual_message_history2[] = (int) $msg->data; |
| 212 | } |
| 213 | $expected_message_history2 = range(24, 0, -1); |
| 214 | $this->assertEquals( $expected_message_history2, $actual_message_history2, 'Expect messages in backward order on 2nd page'); |
| 215 | |
| 216 | $this->assertTrue( $messages2->isPaginated(), 'Expected messages to be paginated' ); |
| 217 | $this->assertTrue( $messages2->hasFirst(), 'Expected to have first page' ); |
| 218 | $this->assertFalse( $messages2->hasNext(), 'Expected not to have next page' ); |
| 219 | $this->assertTrue( $messages2->isLast(), 'Expected to be the last page' ); |
| 220 | $this->assertNull( $messages2->next(), 'Expected the 3rd page to be null' ); |
| 221 | |
| 222 | // get the first page from the 2nd page |
| 223 | $messages1 = $messages2->first(); |
| 224 | $this->assertEquals( 25, count($messages1->items), 'Expected 25 messages on the 1st page' ); |
| 225 | |
| 226 | $actual_message_history1 = []; |
| 227 | foreach ($messages1->items as $msg) { |
| 228 | $actual_message_history1[] = (int) $msg->data; |
| 229 | } |
| 230 | $this->assertEquals( $expected_message_history, $actual_message_history1, 'Expect messages to match the first page'); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Publish events and check expected history based on time slice (forwards) |
| 235 | */ |
| 236 | public function testPublishEventsAndCheckExpectedHistoryInTimeSlicesForwards() { |
| 237 | $interval_start = $interval_end = 0; |
| 238 | |
| 239 | // first publish some messages |
| 240 | $history5 = self::$ably->channel('persisted:history5'); |
| 241 | |
| 242 | // send batches of messages with short inter-message delay |
| 243 | for ($i=0; $i<2; $i++) { |
| 244 | $history5->publish( 'history'.$i, (string) $i ); |
| 245 | usleep(100000); // sleep for 0.1 of a second |
| 246 | } |
| 247 | $interval_start = self::$ably->time(); |
| 248 | for ($i=2; $i<4; $i++) { |
| 249 | $history5->publish( 'history'.$i, (string) $i ); |
| 250 | usleep(100000); // sleep for 0.1 of a second |
| 251 | } |
| 252 | $interval_end = self::$ably->time(); |
| 253 | for ($i=4; $i<6; $i++) { |
| 254 | $history5->publish( 'history'.$i, (string) $i ); |
| 255 | usleep(100000); // sleep for 0.1 of a second |
| 256 | } |
| 257 | |
| 258 | // get the history for this channel |
| 259 | $messages = $history5->history([ |
| 260 | 'direction' => 'forwards', |
| 261 | 'start' => $interval_start, |
| 262 | 'end' => $interval_end, |
| 263 | ]); |
| 264 | $this->assertEquals( 2, count($messages->items), 'Expected 2 messages' ); |
| 265 | |
| 266 | // verify message order |
| 267 | $actual_message_history = []; |
| 268 | for ($i=2; $i<4; $i++) { |
| 269 | $actual_message_history[] = $messages->items[$i-2]->data; |
| 270 | } |
| 271 | $expected_message_history = [ 2, 3 ]; |
| 272 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in forward order'); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Publish events and check expected history based on time slice (backwards) |
| 277 | */ |
| 278 | public function testPublishEventsAndCheckExpectedHistoryInTimeSlicesBackwards() { |
| 279 | $interval_start = $interval_end = 0; |
| 280 | |
| 281 | // first publish some messages |
| 282 | $history6 = self::$ably->channel('persisted:history6'); |
| 283 | |
| 284 | // send batches of messages with short inter-message delay |
| 285 | for ($i=0; $i<2; $i++) { |
| 286 | $history6->publish( 'history'.$i, (string) $i ); |
| 287 | usleep(100000); // sleep for 0.1 of a second |
| 288 | } |
| 289 | $interval_start = self::$ably->time(); |
| 290 | for ($i=2; $i<4; $i++) { |
| 291 | $history6->publish( 'history'.$i, (string) $i ); |
| 292 | usleep(100000); // sleep for 0.1 of a second |
| 293 | } |
| 294 | $interval_end = self::$ably->time(); |
| 295 | for ($i=4; $i<6; $i++) { |
| 296 | $history6->publish( 'history'.$i, (string) $i ); |
| 297 | usleep(100000); // sleep for 0.1 of a second |
| 298 | } |
| 299 | |
| 300 | // get the history for this channel |
| 301 | $messages = $history6->history([ |
| 302 | 'direction' => 'backwards', |
| 303 | 'start' => $interval_start, |
| 304 | 'end' => $interval_end, |
| 305 | ]); |
| 306 | |
| 307 | $this->assertEquals( 2, count($messages->items), 'Expected 20 messages' ); |
| 308 | |
| 309 | // verify message order |
| 310 | $actual_message_history = []; |
| 311 | for ($i=2; $i<4; $i++) { |
| 312 | $actual_message_history[] = $messages->items[$i-2]->data; |
| 313 | } |
| 314 | $expected_message_history = [ 3, 2 ]; |
| 315 | $this->assertEquals( $expected_message_history, $actual_message_history, 'Expect messages in backward order' ); |
| 316 | } |
| 317 | } |