Changeset 51663
- Timestamp:
- 08/26/2021 03:54:29 PM (3 years ago)
- Location:
- trunk/tests/phpunit/tests/functions
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/functions/wpListFilter.php
r51568 r51663 2 2 3 3 /** 4 * Test wp_filter_object_list() , wp_list_filter(), wp_list_pluck().4 * Test wp_filter_object_list(). 5 5 * 6 6 * @group functions.php 7 7 * @covers ::wp_filter_object_list 8 * @covers ::wp_list_pluck9 8 */ 10 9 class Tests_Functions_wpListFilter extends WP_UnitTestCase { … … 136 135 } 137 136 138 function test_wp_list_pluck() {139 $list = wp_list_pluck( $this->object_list, 'name' );140 $this->assertSame(141 array(142 'foo' => 'foo',143 'bar' => 'bar',144 'baz' => 'baz',145 ),146 $list147 );148 149 $list = wp_list_pluck( $this->array_list, 'name' );150 $this->assertSame(151 array(152 'foo' => 'foo',153 'bar' => 'bar',154 'baz' => 'baz',155 ),156 $list157 );158 }159 160 /**161 * @ticket 28666162 */163 function test_wp_list_pluck_index_key() {164 $list = wp_list_pluck( $this->array_list, 'name', 'id' );165 $this->assertSame(166 array(167 'f' => 'foo',168 'b' => 'bar',169 'z' => 'baz',170 ),171 $list172 );173 }174 175 /**176 * @ticket 28666177 */178 function test_wp_list_pluck_object_index_key() {179 $list = wp_list_pluck( $this->object_list, 'name', 'id' );180 $this->assertSame(181 array(182 'f' => 'foo',183 'b' => 'bar',184 'z' => 'baz',185 ),186 $list187 );188 }189 190 /**191 * @ticket 28666192 */193 function test_wp_list_pluck_missing_index_key() {194 $list = wp_list_pluck( $this->array_list, 'name', 'nonexistent' );195 $this->assertSame(196 array(197 0 => 'foo',198 1 => 'bar',199 2 => 'baz',200 ),201 $list202 );203 }204 205 /**206 * @ticket 28666207 */208 function test_wp_list_pluck_partial_missing_index_key() {209 $array_list = $this->array_list;210 unset( $array_list['bar']['id'] );211 $list = wp_list_pluck( $array_list, 'name', 'id' );212 $this->assertSame(213 array(214 'f' => 'foo',215 0 => 'bar',216 'z' => 'baz',217 ),218 $list219 );220 }221 222 /**223 * @ticket 28666224 */225 function test_wp_list_pluck_mixed_index_key() {226 $mixed_list = $this->array_list;227 $mixed_list['bar'] = (object) $mixed_list['bar'];228 $list = wp_list_pluck( $mixed_list, 'name', 'id' );229 $this->assertSame(230 array(231 'f' => 'foo',232 'b' => 'bar',233 'z' => 'baz',234 ),235 $list236 );237 }238 239 /**240 * @ticket 16895241 */242 function test_wp_list_pluck_containing_references() {243 $ref_list = array(244 & $this->object_list['foo'],245 & $this->object_list['bar'],246 );247 248 $this->assertInstanceOf( 'stdClass', $ref_list[0] );249 $this->assertInstanceOf( 'stdClass', $ref_list[1] );250 251 $list = wp_list_pluck( $ref_list, 'name' );252 $this->assertSame(253 array(254 'foo',255 'bar',256 ),257 $list258 );259 260 $this->assertInstanceOf( 'stdClass', $ref_list[0] );261 $this->assertInstanceOf( 'stdClass', $ref_list[1] );262 }263 264 /**265 * @ticket 16895266 */267 function test_wp_list_pluck_containing_references_keys() {268 $ref_list = array(269 & $this->object_list['foo'],270 & $this->object_list['bar'],271 );272 273 $this->assertInstanceOf( 'stdClass', $ref_list[0] );274 $this->assertInstanceOf( 'stdClass', $ref_list[1] );275 276 $list = wp_list_pluck( $ref_list, 'name', 'id' );277 $this->assertSame(278 array(279 'f' => 'foo',280 'b' => 'bar',281 ),282 $list283 );284 285 $this->assertInstanceOf( 'stdClass', $ref_list[0] );286 $this->assertInstanceOf( 'stdClass', $ref_list[1] );287 }288 289 137 function test_filter_object_list_nested_array_and() { 290 138 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' ); -
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.