1<?php 2/** 3 * Copyright 2011-2017 Horde LLC (http://www.horde.org/) 4 * 5 * See the enclosed file COPYING for license information (LGPL). If you 6 * did not receive this file, see http://www.horde.org/licenses/lgpl21. 7 * 8 * @category Horde 9 * @copyright 2011-2016 Horde LLC 10 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 11 * @package Imap_Client 12 * @subpackage UnitTests 13 */ 14 15/** 16 * Tests for the Ids object. 17 * 18 * @author Michael Slusarz <slusarz@horde.org> 19 * @category Horde 20 * @copyright 2011-2016 Horde LLC 21 * @ignore 22 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 23 * @package Imap_Client 24 * @subpackage UnitTests 25 */ 26class Horde_Imap_Client_IdsTest extends PHPUnit_Framework_TestCase 27{ 28 public function testBasicAddingOfIds() 29 { 30 $ids = new Horde_Imap_Client_Ids(array(1, 3, 5)); 31 32 $this->assertEquals( 33 3, 34 count($ids) 35 ); 36 37 $this->assertEquals( 38 '1,3,5', 39 strval($ids) 40 ); 41 42 $this->assertFalse($ids->isEmpty()); 43 } 44 45 /** 46 * @dataProvider ignoreInvalidAddsProvider 47 */ 48 public function testIgnoreInvalidAdds($in) 49 { 50 $ids = new Horde_Imap_Client_Ids($in); 51 $this->assertEquals( 52 0, 53 count($ids) 54 ); 55 56 $ids = new Horde_Imap_Client_Ids(); 57 $ids->add($in); 58 $this->assertEquals( 59 0, 60 count($ids) 61 ); 62 } 63 64 public function ignoreInvalidAddsProvider() 65 { 66 return array( 67 array(null), 68 array(new stdClass) 69 ); 70 } 71 72 public function testEmptyIdsArray() 73 { 74 $ids = new Horde_Imap_Client_Ids(array()); 75 76 $this->assertEquals( 77 0, 78 count($ids) 79 ); 80 81 $this->assertEquals( 82 '', 83 strval($ids) 84 ); 85 86 $this->assertTrue($ids->isEmpty()); 87 } 88 89 /** 90 * @dataProvider sequenceParsingProvider 91 */ 92 public function testSequenceParsing($in, $expected) 93 { 94 $ids = new Horde_Imap_Client_Ids($in); 95 96 $this->assertEquals( 97 count($expected), 98 count($ids) 99 ); 100 101 $this->assertEquals( 102 $expected, 103 iterator_to_array($ids) 104 ); 105 } 106 107 public function sequenceParsingProvider() 108 { 109 return array( 110 array('12:10', array(10, 11, 12)), 111 array('12,11,10', array(12, 11, 10)), 112 array('10:12,10,11,12,10:12', array(10, 11, 12)), 113 array('10', array(10)) 114 ); 115 } 116 117 /** 118 * @dataProvider rangeGenerationProvider 119 */ 120 public function testRangeGeneration($in, $expected) 121 { 122 $ids = new Horde_Imap_Client_Ids($in); 123 124 $this->assertEquals( 125 $expected, 126 $ids->range_string 127 ); 128 } 129 130 public function rangeGenerationProvider() 131 { 132 return array( 133 array('100,300,200', '100:300'), 134 array(Horde_Imap_Client_Ids::ALL, ''), 135 array('50', '50') 136 ); 137 } 138 139 public function testSorting() 140 { 141 $ids = new Horde_Imap_Client_Ids('14,12,10'); 142 143 $this->assertEquals( 144 '14,12,10', 145 $ids->tostring 146 ); 147 $this->assertEquals( 148 '10,12,14', 149 $ids->tostring_sort 150 ); 151 } 152 153 /** 154 * @dataProvider specialIdValueStringRepresentationsProvider 155 */ 156 public function testSpecialIdValueStringRepresentations($in, $expected) 157 { 158 $ids = new Horde_Imap_Client_Ids($in); 159 160 $this->assertEquals( 161 $expected, 162 $ids->tostring 163 ); 164 } 165 166 public function specialIdValueStringRepresentationsProvider() 167 { 168 return array( 169 array(Horde_Imap_Client_Ids::ALL, '1:*'), 170 array(Horde_Imap_Client_Ids::SEARCH_RES, '$'), 171 array(Horde_Imap_Client_Ids::LARGEST, '*') 172 ); 173 } 174 175 public function testDuplicatesAllowed() 176 { 177 $ids = new Horde_Imap_Client_Ids('1:10,1:10'); 178 $this->assertEquals( 179 10, 180 count($ids) 181 ); 182 183 $ids = new Horde_Imap_Client_Ids(); 184 $ids->duplicates = true; 185 $ids->add('1:10,1:10'); 186 $this->assertEquals( 187 20, 188 count($ids) 189 ); 190 } 191 192 public function testSplit() 193 { 194 // ~5000 non-sequential IDs 195 $ids = new Horde_Imap_Client_Ids(range(1, 10000, 2)); 196 197 $split = $ids->split(2000); 198 199 $this->assertGreaterThan(1, count($split)); 200 201 foreach (array_slice($split, 0, -1) as $val) { 202 $this->assertGreaterThan( 203 2000, 204 strlen($val) 205 ); 206 207 $this->assertNotEquals( 208 ',', 209 substr($val, -1) 210 ); 211 } 212 213 $last = array_pop($split); 214 $this->assertLessThan( 215 2001, 216 strlen($last) 217 ); 218 219 $this->assertNotEquals( 220 ',', 221 substr($last, -1) 222 ); 223 } 224 225 public function testSplitOnAll() 226 { 227 $ids = new Horde_Imap_Client_Ids(Horde_Imap_Client_Ids::ALL); 228 229 $this->assertEquals( 230 array( 231 '1:*' 232 ), 233 $ids->split(2000) 234 ); 235 } 236 237 public function testRemove() 238 { 239 $ids = new Horde_Imap_Client_Ids('1:100'); 240 $this->assertEquals( 241 100, 242 count($ids) 243 ); 244 245 // Remove from array 246 $ids->remove(range(1, 10)); 247 $this->assertEquals( 248 90, 249 count($ids) 250 ); 251 252 // Removing same IDs shouldn't change anything. 253 $ids->remove(range(1, 10)); 254 $this->assertEquals( 255 90, 256 count($ids) 257 ); 258 259 // Remove via sequence string 260 $ids->remove('11:20'); 261 $this->assertEquals( 262 80, 263 count($ids) 264 ); 265 266 // Remove via object 267 $ids->remove(new Horde_Imap_Client_Ids('21:30')); 268 $this->assertEquals( 269 70, 270 count($ids) 271 ); 272 } 273 274 /** 275 * @dataProvider minAndMaxProvider 276 */ 277 public function testMinAndMax($in, $min, $max) 278 { 279 $ids = new Horde_Imap_Client_Ids($in); 280 281 $this->assertEquals( 282 $min, 283 $ids->min 284 ); 285 $this->assertEquals( 286 $max, 287 $ids->max 288 ); 289 } 290 291 public function minAndMaxProvider() 292 { 293 return array( 294 array(array(1), 1, 1), 295 array(array(1, 2), 1, 2), 296 array(array(1, 5, 3), 1, 5) 297 ); 298 } 299 300 public function testReverse() 301 { 302 $ids = new Horde_Imap_Client_Ids(array(1, 3, 5)); 303 $ids->reverse(); 304 305 $this->assertEquals( 306 array(5, 3, 1), 307 $ids->ids 308 ); 309 } 310 311 /** 312 * @dataProvider sequenceStringGenerationProvider 313 */ 314 public function testSequenceStringGeneration($in, $expected) 315 { 316 $ids = new Horde_Imap_Client_Ids($in); 317 318 $this->assertEquals( 319 $expected, 320 strval($ids) 321 ); 322 } 323 324 public function sequenceStringGenerationProvider() 325 { 326 return array( 327 array(array(1, 2, 3), '1:3'), 328 array(array(3, 2, 1), '3,2,1'), 329 array(array(1, 2, 3, 5), '1:3,5') 330 ); 331 } 332 333 public function testClone() 334 { 335 $ids = new Horde_Imap_Client_Ids(array(1, 3)); 336 337 $ids2 = clone $ids; 338 $ids2->add(5); 339 340 $this->assertEquals( 341 array(1, 3), 342 iterator_to_array($ids) 343 ); 344 $this->assertEquals( 345 array(1, 3, 5), 346 iterator_to_array($ids2) 347 ); 348 } 349 350 public function testSerialize() 351 { 352 $ids = new Horde_Imap_Client_Ids(array(1, 3, 5)); 353 354 $ids2 = unserialize(serialize($ids)); 355 356 $this->assertEquals( 357 array(1, 3, 5), 358 iterator_to_array($ids2) 359 ); 360 } 361 362} 363