MapDeep.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @group formatting
  4. * @ticket 22300
  5. */
  6. class Tests_Formatting_MapDeep extends WP_UnitTestCase {
  7. function setUp() {
  8. if ( ! function_exists( 'map_deep' ) ) {
  9. $this->markTestSkipped( "map_deep function doesn't exist" );
  10. }
  11. parent::setUp();
  12. }
  13. function test_map_deep_with_any_function_over_empty_array_should_return_empty_array() {
  14. $this->assertEquals( array(), map_deep( array( $this, 'return_baba' ), array() ) );
  15. }
  16. function test_map_deep_should_map_each_element_of_array_one_level_deep() {
  17. $this->assertEquals( array( 'ababa', 'xbaba' ), map_deep( array( $this, 'append_baba' ), array( 'a', 'x' ) ) );
  18. }
  19. function test_map_deep_should_map_each_element_of_array_two_levels_deep() {
  20. $this->assertEquals( array( 'ababa', array( 'xbaba' ) ), map_deep( array( $this, 'append_baba' ), array( 'a', array( 'x' ) ) ) );
  21. }
  22. function test_map_deep_should_map_each_object_element_of_an_array() {
  23. $this->assertEquals( array( 'var0' => 'ababa', 'var1' => (object)array( 'xbaba' ) ),
  24. map_deep( array( $this, 'append_baba' ), array( 'var0' => 'a', 'var1' => (object)array( 'x' ) ) ) );
  25. }
  26. function test_map_deep_should_apply_the_function_to_a_string() {
  27. $this->assertEquals( 'xbaba', map_deep( array( $this, 'append_baba' ), 'x' ) );
  28. }
  29. function test_map_deep_should_apply_the_function_to_an_integer() {
  30. $this->assertEquals( '5baba' , map_deep( array( $this, 'append_baba' ), 5 ) );
  31. }
  32. function test_map_deep_should_map_each_property_of_an_object() {
  33. $this->assertEquals( (object)array( 'var0' => 'ababa', 'var1' => 'xbaba' ),
  34. map_deep( array( $this, 'append_baba' ), (object)array( 'var0' => 'a', 'var1' => 'x' ) ) );
  35. }
  36. function test_map_deep_should_map_each_array_property_of_an_object() {
  37. $this->assertEquals( (object)array( 'var0' => 'ababa', 'var1' => array( 'xbaba' ) ),
  38. map_deep( array( $this, 'append_baba' ), (object)array( 'var0' => 'a', 'var1' => array( 'x' ) ) ) );
  39. }
  40. function test_map_deep_should_map_each_object_property_of_an_object() {
  41. $this->assertEquals( (object)array( 'var0' => 'ababa', 'var1' => (object)array( 'xbaba' ) ),
  42. map_deep( array( $this, 'append_baba' ), (object)array( 'var0' => 'a', 'var1' => (object)array( 'x' ) ) ) );
  43. }
  44. function return_baba( $value ) {
  45. return 'baba';
  46. }
  47. function append_baba( $value ) {
  48. return $value . 'baba';
  49. }
  50. }