Changeset 51663 for trunk/tests/phpunit/tests/functions/wpListPluck.php
- Timestamp:
- 08/26/2021 03:54:29 PM (3 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/functions/wpListPluck.php
r51609 r51663 2 2 3 3 /** 4 * Test wp_ filter_object_list(), wp_list_filter(), wp_list_pluck().4 * Test wp_list_pluck(). 5 5 * 6 6 * @group functions.php 7 * @covers ::wp_filter_object_list8 7 * @covers ::wp_list_pluck 9 8 */ 10 class Tests_Functions_wpList Filterextends WP_UnitTestCase {9 class Tests_Functions_wpListPluck extends WP_UnitTestCase { 11 10 public $object_list = array(); 12 11 public $array_list = array(); … … 43 42 } 44 43 45 function test_filter_object_list_and() { 46 $list = wp_filter_object_list( 47 $this->object_list, 48 array( 49 'field1' => true, 50 'field2' => true, 51 ), 52 'AND' 53 ); 54 $this->assertCount( 2, $list ); 55 $this->assertArrayHasKey( 'foo', $list ); 56 $this->assertArrayHasKey( 'bar', $list ); 57 } 58 59 function test_filter_object_list_or() { 60 $list = wp_filter_object_list( 61 $this->object_list, 62 array( 63 'field1' => true, 64 'field2' => true, 65 ), 66 'OR' 67 ); 68 $this->assertCount( 3, $list ); 69 $this->assertArrayHasKey( 'foo', $list ); 70 $this->assertArrayHasKey( 'bar', $list ); 71 $this->assertArrayHasKey( 'baz', $list ); 72 } 73 74 function test_filter_object_list_not() { 75 $list = wp_filter_object_list( 76 $this->object_list, 77 array( 78 'field2' => true, 79 'field3' => true, 80 ), 81 'NOT' 82 ); 83 $this->assertCount( 1, $list ); 84 $this->assertArrayHasKey( 'baz', $list ); 85 } 86 87 function test_filter_object_list_and_field() { 88 $list = wp_filter_object_list( 89 $this->object_list, 90 array( 91 'field1' => true, 92 'field2' => true, 93 ), 94 'AND', 95 'name' 96 ); 97 $this->assertSame( 98 array( 99 'foo' => 'foo', 100 'bar' => 'bar', 101 ), 102 $list 103 ); 104 } 105 106 function test_filter_object_list_or_field() { 107 $list = wp_filter_object_list( 108 $this->object_list, 109 array( 110 'field2' => true, 111 'field3' => true, 112 ), 113 'OR', 114 'name' 115 ); 116 $this->assertSame( 117 array( 118 'foo' => 'foo', 119 'bar' => 'bar', 120 ), 121 $list 122 ); 123 } 124 125 function test_filter_object_list_not_field() { 126 $list = wp_filter_object_list( 127 $this->object_list, 128 array( 129 'field2' => true, 130 'field3' => true, 131 ), 132 'NOT', 133 'name' 134 ); 135 $this->assertSame( array( 'baz' => 'baz' ), $list ); 136 } 137 138 function test_wp_list_pluck() { 44 function test_wp_list_pluck_array_and_object() { 139 45 $list = wp_list_pluck( $this->object_list, 'name' ); 140 46 $this->assertSame( … … 287 193 } 288 194 289 function test_filter_object_list_nested_array_and() { 290 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' ); 291 $this->assertCount( 1, $list ); 292 $this->assertArrayHasKey( 'baz', $list ); 293 } 294 295 function test_filter_object_list_nested_array_not() { 296 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'red' ) ), 'NOT' ); 297 $this->assertCount( 2, $list ); 298 $this->assertArrayHasKey( 'bar', $list ); 299 $this->assertArrayHasKey( 'baz', $list ); 300 } 301 302 function test_filter_object_list_nested_array_or() { 303 $list = wp_filter_object_list( 304 $this->object_list, 305 array( 306 'field3' => true, 307 'field4' => array( 'blue' ), 308 ), 309 'OR' 310 ); 311 $this->assertCount( 2, $list ); 312 $this->assertArrayHasKey( 'foo', $list ); 313 $this->assertArrayHasKey( 'baz', $list ); 314 } 315 316 function test_filter_object_list_nested_array_or_singular() { 317 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'OR' ); 318 $this->assertCount( 1, $list ); 319 $this->assertArrayHasKey( 'baz', $list ); 320 } 321 322 function test_filter_object_list_nested_array_and_field() { 323 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND', 'name' ); 324 $this->assertSame( array( 'baz' => 'baz' ), $list ); 325 } 326 327 function test_filter_object_list_nested_array_not_field() { 328 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'green' ) ), 'NOT', 'name' ); 329 $this->assertSame( 330 array( 331 'foo' => 'foo', 332 'baz' => 'baz', 333 ), 334 $list 335 ); 336 } 337 338 function test_filter_object_list_nested_array_or_field() { 339 $list = wp_filter_object_list( 340 $this->object_list, 341 array( 342 'field3' => true, 343 'field4' => array( 'blue' ), 344 ), 345 'OR', 346 'name' 347 ); 348 $this->assertSame( 349 array( 350 'foo' => 'foo', 351 'baz' => 'baz', 352 ), 353 $list 195 /** 196 * @dataProvider data_test_wp_list_pluck 197 * 198 * @param array $list List of objects or arrays. 199 * @param int|string $field Field from the object to place instead of the entire object 200 * @param int|string $index_key Field from the object to use as keys for the new array. 201 * @param array $expected Expected result. 202 */ 203 public function test_wp_list_pluck( $list, $field, $index_key, $expected ) { 204 $this->assertSameSetsWithIndex( $expected, wp_list_pluck( $list, $field, $index_key ) ); 205 } 206 207 public function data_test_wp_list_pluck() { 208 return array( 209 'arrays' => array( 210 array( 211 array( 212 'foo' => 'bar', 213 'bar' => 'baz', 214 'abc' => 'xyz', 215 ), 216 array( 217 'foo' => 'foo', 218 '123' => '456', 219 'lorem' => 'ipsum', 220 ), 221 array( 'foo' => 'baz' ), 222 ), 223 'foo', 224 null, 225 array( 'bar', 'foo', 'baz' ), 226 ), 227 'arrays with index key' => array( 228 array( 229 array( 230 'foo' => 'bar', 231 'bar' => 'baz', 232 'abc' => 'xyz', 233 'key' => 'foo', 234 ), 235 array( 236 'foo' => 'foo', 237 '123' => '456', 238 'lorem' => 'ipsum', 239 'key' => 'bar', 240 ), 241 array( 242 'foo' => 'baz', 243 'key' => 'value', 244 ), 245 ), 246 'foo', 247 'key', 248 array( 249 'foo' => 'bar', 250 'bar' => 'foo', 251 'value' => 'baz', 252 ), 253 ), 254 'arrays with index key missing' => array( 255 array( 256 array( 257 'foo' => 'bar', 258 'bar' => 'baz', 259 'abc' => 'xyz', 260 ), 261 array( 262 'foo' => 'foo', 263 '123' => '456', 264 'lorem' => 'ipsum', 265 'key' => 'bar', 266 ), 267 array( 268 'foo' => 'baz', 269 'key' => 'value', 270 ), 271 ), 272 'foo', 273 'key', 274 array( 275 'bar', 276 'bar' => 'foo', 277 'value' => 'baz', 278 ), 279 ), 280 'objects' => array( 281 array( 282 (object) array( 283 'foo' => 'bar', 284 'bar' => 'baz', 285 'abc' => 'xyz', 286 ), 287 (object) array( 288 'foo' => 'foo', 289 '123' => '456', 290 'lorem' => 'ipsum', 291 ), 292 (object) array( 'foo' => 'baz' ), 293 ), 294 'foo', 295 null, 296 array( 'bar', 'foo', 'baz' ), 297 ), 298 'objects with index key' => array( 299 array( 300 (object) array( 301 'foo' => 'bar', 302 'bar' => 'baz', 303 'abc' => 'xyz', 304 'key' => 'foo', 305 ), 306 (object) array( 307 'foo' => 'foo', 308 '123' => '456', 309 'lorem' => 'ipsum', 310 'key' => 'bar', 311 ), 312 (object) array( 313 'foo' => 'baz', 314 'key' => 'value', 315 ), 316 ), 317 'foo', 318 'key', 319 array( 320 'foo' => 'bar', 321 'bar' => 'foo', 322 'value' => 'baz', 323 ), 324 ), 325 'objects with index key missing' => array( 326 array( 327 (object) array( 328 'foo' => 'bar', 329 'bar' => 'baz', 330 'abc' => 'xyz', 331 ), 332 (object) array( 333 'foo' => 'foo', 334 '123' => '456', 335 'lorem' => 'ipsum', 336 'key' => 'bar', 337 ), 338 (object) array( 339 'foo' => 'baz', 340 'key' => 'value', 341 ), 342 ), 343 'foo', 344 'key', 345 array( 346 'bar', 347 'bar' => 'foo', 348 'value' => 'baz', 349 ), 350 ), 354 351 ); 355 352 }
Note: See TracChangeset
for help on using the changeset viewer.