/usr/share/php/kohana3.1/system/tests/kohana/ArrTest.php is in libkohana3.1-core-php 3.1.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the Arr lib that's shipped with kohana
*
* @group kohana
* @group kohana.arr
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ArrTest extends Unittest_TestCase
{
/**
* Provides test data for test_callback()
*
* @return array
*/
public function provider_callback()
{
return array(
// Tests....
// That no parameters returns null
array('function', array('function', NULL)),
// That we can get an array of parameters values
array('function(1,2,3)', array('function', array('1', '2', '3'))),
// That it's not just using the callback "function"
array('different_name(harry,jerry)', array('different_name', array('harry', 'jerry'))),
// That static callbacks are parsed into arrays
array('kohana::appify(this)', array(array('kohana', 'appify'), array('this'))),
// Spaces are preserved in parameters
array('deal::make(me, my mate )', array(array('deal', 'make'), array('me', ' my mate ')))
// TODO: add more cases
);
}
/**
* Tests Arr::callback()
*
* @test
* @dataProvider provider_callback
* @param string $str String to parse
* @param array $expected Callback and its parameters
*/
public function test_callback($str, $expected)
{
$result = Arr::callback($str);
$this->assertSame(2, count($result));
$this->assertSame($expected, $result);
}
/**
* Provides test data for test_extract
*
* @return array
*/
public function provider_extract()
{
return array(
array(
array('kohana' => 'awesome', 'blueflame' => 'was'),
array('kohana', 'cakephp', 'symfony'),
NULL,
array('kohana' => 'awesome', 'cakephp' => NULL, 'symfony' => NULL)
),
// I realise noone should EVER code like this in real life,
// but unit testing is very very very very boring
array(
array('chocolate cake' => 'in stock', 'carrot cake' => 'in stock'),
array('carrot cake', 'humble pie'),
'not in stock',
array('carrot cake' => 'in stock', 'humble pie' => 'not in stock'),
),
);
}
/**
* Tests Arr::extract()
*
* @test
* @dataProvider provider_extract
* @param array $array
* @param array $keys
* @param mixed $default
* @param array $expected
*/
public function test_extract(array $array, array $keys, $default, $expected)
{
$array = Arr::extract($array, $keys, $default);
$this->assertSame(count($expected), count($array));
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_pluck
*
* @return array
*/
public function provider_pluck()
{
return array(
array(
array(
array('id' => 20, 'name' => 'John Smith'),
array('name' => 'Linda'),
array('id' => 25, 'name' => 'Fred'),
),
'id',
array(20, 25)
),
);
}
/**
* Tests Arr::pluck()
*
* @test
* @dataProvider provider_pluck
* @param array $array
* @param string $key
* @param array $expected
*/
public function test_pluck(array $array, $key, $expected)
{
$array = Arr::pluck($array, $key);
$this->assertSame(count($expected), count($array));
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_get()
*
* @return array
*/
public function provider_get()
{
return array(
array(array('uno', 'dos', 'tress'), 1, NULL, 'dos'),
array(array('we' => 'can', 'make' => 'change'), 'we', NULL, 'can'),
array(array('uno', 'dos', 'tress'), 10, NULL, NULL),
array(array('we' => 'can', 'make' => 'change'), 'he', NULL, NULL),
array(array('we' => 'can', 'make' => 'change'), 'he', 'who', 'who'),
array(array('we' => 'can', 'make' => 'change'), 'he', array('arrays'), array('arrays')),
);
}
/**
* Tests Arr::get()
*
* @test
* @dataProvider provider_get()
* @param array $array Array to look in
* @param string|integer $key Key to look for
* @param mixed $default What to return if $key isn't set
* @param mixed $expected The expected value returned
*/
public function test_get(array $array, $key, $default, $expected)
{
$this->assertSame(
$expected,
Arr::get($array, $key, $default)
);
}
/**
* Provides test data for test_is_assoc()
*
* @return array
*/
public function provider_is_assoc()
{
return array(
array(array('one', 'two', 'three'), FALSE),
array(array('one' => 'o clock', 'two' => 'o clock', 'three' => 'o clock'), TRUE),
);
}
/**
* Tests Arr::is_assoc()
*
* @test
* @dataProvider provider_is_assoc
* @param array $array Array to check
* @param boolean $expected Is $array assoc
*/
public function test_is_assoc(array $array, $expected)
{
$this->assertSame(
$expected,
Arr::is_assoc($array)
);
}
/**
* Provides test data for test_is_array()
*
* @return array
*/
public function provider_is_array()
{
return array(
array($a = array('one', 'two', 'three'), TRUE),
array(new ArrayObject($a), TRUE),
array(new ArrayIterator($a), TRUE),
array('not an array', FALSE),
array(new stdClass, FALSE),
);
}
/**
* Tests Arr::is_array()
*
* @test
* @dataProvider provider_is_array
* @param mixed $value Value to check
* @param boolean $expected Is $value an array?
*/
public function test_is_array($array, $expected)
{
$this->assertSame(
$expected,
Arr::is_array($array)
);
}
public function provider_merge()
{
return array(
// Test how it merges arrays and sub arrays with assoc keys
array(
array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane')),
array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane')),
array('name' => 'mary', 'children' => array('jane')),
),
// See how it merges sub-arrays with numerical indexes
array(
array(array('test1','test3'), array('test2','test4')),
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
),
array(
array(array('test1','test3'), array('test2','test4')),
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
),
array(
array('digits' => array(0, 1, 2, 3)),
array('digits' => array(0, 1)),
array('digits' => array(2, 3)),
),
// See how it manages merging items with numerical indexes
array(
array(0, 1, 2, 3),
array(0, 1),
array(2, 3),
),
// Try and get it to merge assoc. arrays recursively
array(
array('foo' => 'bar', array('temp' => 'life')),
array('foo' => 'bin', array('temp' => 'name')),
array('foo' => 'bar', array('temp' => 'life')),
),
// Bug #3139
array(
array('foo' => array('bar')),
array('foo' => 'bar'),
array('foo' => array('bar')),
),
array(
array('foo' => 'bar'),
array('foo' => array('bar')),
array('foo' => 'bar'),
),
);
}
/**
*
* @test
* @dataProvider provider_merge
*/
public function test_merge($expected, $array1, $array2)
{
$this->assertSame(
$expected,
Arr::merge($array1,$array2)
);
}
/**
* Provides test data for test_path()
*
* @return array
*/
public function provider_path()
{
$array = array(
'foobar' => array('definition' => 'lost'),
'kohana' => 'awesome',
'users' => array(
1 => array('name' => 'matt'),
2 => array('name' => 'john', 'interests' => array('hocky' => array('length' => 2), 'football' => array())),
3 => 'frank', // Issue #3194
),
'object' => new ArrayObject(array('iterator' => TRUE)), // Iterable object should work exactly the same
);
return array(
// Tests returns normal values
array($array['foobar'], $array, 'foobar'),
array($array['kohana'], $array, 'kohana'),
array($array['foobar']['definition'], $array, 'foobar.definition'),
// Custom delimiters
array($array['foobar']['definition'], $array, 'foobar/definition', NULL, '/'),
// We should be able to use NULL as a default, returned if the key DNX
array(NULL, $array, 'foobar.alternatives', NULL),
array(NULL, $array, 'kohana.alternatives', NULL),
// Try using a string as a default
array('nothing', $array, 'kohana.alternatives', 'nothing'),
// Make sure you can use arrays as defaults
array(array('far', 'wide'), $array, 'cheese.origins', array('far', 'wide')),
// Ensures path() casts ints to actual integers for keys
array($array['users'][1]['name'], $array, 'users.1.name'),
// Test that a wildcard returns the entire array at that "level"
array($array['users'], $array, 'users.*'),
// Now we check that keys after a wilcard will be processed
array(array(0 => array(0 => 2)), $array, 'users.*.interests.*.length'),
// See what happens when it can't dig any deeper from a wildcard
array(NULL, $array, 'users.*.fans'),
// Starting wildcards, issue #3269
array(array('matt', 'john'), $array['users'], '*.name'),
// Path as array, issue #3260
array($array['users'][2]['name'], $array, array('users', 2, 'name')),
array($array['object']['iterator'], $array, 'object.iterator'),
);
}
/**
* Tests Arr::path()
*
* @test
* @dataProvider provider_path
* @param string $path The path to follow
* @param mixed $default The value to return if dnx
* @param boolean $expected The expected value
* @param string $delimiter The path delimiter
*/
public function test_path($expected, $array, $path, $default = NULL, $delimiter = NULL)
{
$this->assertSame(
$expected,
Arr::path($array, $path, $default, $delimiter)
);
}
/**
* Provides test data for test_path()
*
* @return array
*/
public function provider_set_path()
{
return array(
// Tests returns normal values
array(array('foo' => 'bar'), array(), 'foo', 'bar'),
array(array('kohana' => array('is' => 'awesome')), array(), 'kohana.is', 'awesome'),
array(array('kohana' => array('is' => 'cool', 'and' => 'slow')),
array('kohana' => array('is' => 'cool')), 'kohana.and', 'slow'),
// Custom delimiters
array(array('kohana' => array('is' => 'awesome')), array(), 'kohana/is', 'awesome', '/'),
// Ensures set_path() casts ints to actual integers for keys
array(array('foo' => array('bar')), array('foo' => array('test')), 'foo.0', 'bar'),
);
}
/**
* Tests Arr::path()
*
* @test
* @dataProvider provider_set_path
* @param string $path The path to follow
* @param boolean $expected The expected value
* @param string $delimiter The path delimiter
*/
public function test_set_path($expected, $array, $path, $value, $delimiter = NULL)
{
Arr::set_path($array, $path, $value, $delimiter);
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_range()
*
* @return array
*/
public function provider_range()
{
return array(
array(1, 2),
array(1, 100),
array(25, 10),
);
}
/**
* Tests Arr::range()
*
* @dataProvider provider_range
* @param integer $step The step between each value in the array
* @param integer $max The max value of the range (inclusive)
*/
public function test_range($step, $max)
{
$range = Arr::range($step, $max);
$this->assertSame((int) floor($max / $step), count($range));
$current = $step;
foreach($range as $key => $value)
{
$this->assertSame($key, $value);
$this->assertSame($current, $key);
$this->assertLessThanOrEqual($max, $key);
$current += $step;
}
}
/**
* Provides test data for test_unshift()
*
* @return array
*/
public function provider_unshift()
{
return array(
array(array('one' => '1', 'two' => '2',), 'zero', '0'),
array(array('step 1', 'step 2', 'step 3'), 'step 0', 'wow')
);
}
/**
* Tests Arr::unshift()
*
* @test
* @dataProvider provider_unshift
* @param array $array
* @param string $key
* @param mixed $value
*/
public function test_unshift(array $array, $key, $value)
{
$original = $array;
Arr::unshift($array, $key, $value);
$this->assertNotSame($original, $array);
$this->assertSame(count($original) + 1, count($array));
$this->assertArrayHasKey($key, $array);
$this->assertSame($value, reset($array));
$this->assertSame(key($array), $key);
}
/**
* Provies test data for test_overwrite
*
* @return array Test Data
*/
public function provider_overwrite()
{
return array(
array(
array('name' => 'Henry', 'mood' => 'tired', 'food' => 'waffles', 'sport' => 'checkers'),
array('name' => 'John', 'mood' => 'bored', 'food' => 'bacon', 'sport' => 'checkers'),
array('name' => 'Matt', 'mood' => 'tired', 'food' => 'waffles'),
array('name' => 'Henry', 'age' => 18,),
),
);
}
/**
*
* @test
* @dataProvider provider_overwrite
*/
public function test_overwrite($expected, $arr1, $arr2, $arr3 = array(), $arr4 = array())
{
$this->assertSame(
$expected,
Arr::overwrite($arr1, $arr2, $arr3, $arr4)
);
}
/**
* Provides test data for test_map
*
* @return array Test Data
*/
public function provider_map()
{
return array(
array('strip_tags', array('<p>foobar</p>'), array('foobar')),
array('strip_tags', array(array('<p>foobar</p>'), array('<p>foobar</p>')), array(array('foobar'), array('foobar'))),
);
}
/**
*
* @test
* @dataProvider provider_map
*/
public function test_map($method, $source, $expected)
{
$this->assertSame(
$expected,
Arr::map($method, $source)
);
}
/**
* Provides test data for test_flatten
*
* @return array Test Data
*/
public function provider_flatten()
{
return array(
array(array('set' => array('one' => 'something'), 'two' => 'other'), array('one' => 'something', 'two' => 'other')),
);
}
/**
*
* @test
* @dataProvider provider_flatten
*/
public function test_flatten($source, $expected)
{
$this->assertSame(
$expected,
Arr::flatten($source)
);
}
}
|