Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions includes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/TestApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare( strict_types=1 );

namespace lloc\MslsTests;

use Brain\Monkey\Functions;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

#[RunTestsInSeparateProcesses]
#[PreserveGlobalState( false )]
final class TestApi extends MslsUnitTestCase {

protected function setUp(): void {
parent::setUp();

require_once __DIR__ . '/../../includes/api.php';
}

public function test_msls_get_switcher_without_arguments(): void {
Functions\expect( 'apply_filters' )->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' => '<li>' );

$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( '' ) );
}
}
Loading