diff --git a/includes/api.php b/includes/api.php index b230259b..0a6b312f 100644 --- a/includes/api.php +++ b/includes/api.php @@ -10,10 +10,11 @@ * Get the output for using the links to the translations in your code * * @package Msls - * @param mixed $attr + * @since 3.0.0 The $attr parameter is optional and defaults to an empty array. + * @param mixed $attr Optional. Attributes forwarded to the switcher output. Default empty array. * @return string */ -function msls_get_switcher( $attr ): string { +function msls_get_switcher( $attr = array() ): string { $arr = is_array( $attr ) ? $attr : array(); $obj = apply_filters( 'msls_get_output', null ); diff --git a/tests/phpunit/TestApi.php b/tests/phpunit/TestApi.php new file mode 100644 index 00000000..44d4cf39 --- /dev/null +++ b/tests/phpunit/TestApi.php @@ -0,0 +1,45 @@ +once()->with( 'msls_get_output', null )->andReturn( null ); + + $this->assertSame( '', msls_get_switcher() ); + } + + public function test_msls_get_switcher_with_array(): void { + $attr = array( 'before_item' => '
  • ' ); + + $output = \Mockery::mock(); + $output->shouldReceive( 'set_tags' )->once()->with( $attr )->andReturn( 'switcher' ); + + Functions\expect( 'apply_filters' )->once()->with( 'msls_get_output', null )->andReturn( $output ); + + $this->assertSame( 'switcher', msls_get_switcher( $attr ) ); + } + + public function test_msls_get_switcher_coerces_non_array_to_empty_array(): void { + $output = \Mockery::mock(); + $output->shouldReceive( 'set_tags' )->once()->with( array() )->andReturn( 'switcher' ); + + Functions\expect( 'apply_filters' )->once()->with( 'msls_get_output', null )->andReturn( $output ); + + // The [sc_msls] shortcode passes '' when used without attributes. + $this->assertSame( 'switcher', msls_get_switcher( '' ) ); + } +}