Skip to content

Commit a00ead9

Browse files
committed
Options tests added
1 parent 983fed5 commit a00ead9

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

includes/MslsOptions.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,16 @@ public function get_url( $dir ) {
323323
/**
324324
* Returns slug for a post type
325325
*
326+
* @todo This method is not used anywhere in the codebase. Should it be removed?
327+
*
326328
* @param string $post_type
327329
*
328330
* @return string
329331
*/
330-
public function get_slug( $post_type ) {
332+
public function get_slug( string $post_type ): string {
331333
$key = "rewrite_{$post_type}";
332334

333-
error_log( $key );
334-
335-
return isset( $this->$key ) ? $this->$key : '';
335+
return $this->$key ?? '';
336336
}
337337

338338
/**
@@ -352,11 +352,7 @@ public function get_icon( $language ) {
352352
* @return string
353353
*/
354354
public function get_flag_url( $language ) {
355-
if ( ! is_admin() && isset( $this->image_url ) ) {
356-
$url = $this->__get( 'image_url' );
357-
} else {
358-
$url = $this->get_url( 'flags' );
359-
}
355+
$url = ! is_admin() && isset( $this->image_url ) ? $this->__get( 'image_url' ) : $this->get_url( 'flags' );
360356

361357
/**
362358
* Override the path to the flag-icons

includes/MslsOptionsQueryPostType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsOptionsQueryPostType
4+
*
45
* @author Dennis Ploetner <[email protected]>
56
* @since 0.9.8
67
*/
@@ -15,7 +16,7 @@
1516
class MslsOptionsQueryPostType extends MslsOptionsQuery {
1617

1718
/**
18-
* Check if the array has an non empty item which has $language as a key
19+
* Check if the array has a non-empty item which has $language as a key
1920
*
2021
* @param string $language
2122
*
@@ -37,5 +38,4 @@ public function has_value( $language ) {
3738
public function get_current_link() {
3839
return (string) get_post_type_archive_link( $this->get_arg( 0, '' ) );
3940
}
40-
4141
}

includes/MslsOptionsQueryYear.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsOptionsQueryYear
4+
*
45
* @author Dennis Ploetner <[email protected]>
56
* @since 0.9.8
67
*/
@@ -15,7 +16,7 @@
1516
class MslsOptionsQueryYear extends MslsOptionsQuery {
1617

1718
/**
18-
* Check if the array has an non empty item which has $language as a key
19+
* Check if the array has a non-empty item which has $language as a key
1920
*
2021
* @param string $language
2122
*
@@ -44,5 +45,4 @@ public function has_value( $language ) {
4445
public function get_current_link() {
4546
return get_year_link( $this->get_arg( 0, 0 ) );
4647
}
47-
4848
}

tests/TestMslsOptions.php

+44-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Brain\Monkey\Functions;
66

7+
use lloc\Msls\MslsAdminIcon;
78
use lloc\Msls\MslsOptions;
89

910
class TestMslsOptions extends MslsUnitTestCase {
1011

1112
public function get_test(): MslsOptions {
1213
Functions\when( 'home_url' )->justReturn( 'https://lloc.de' );
13-
Functions\when( 'get_option' )->justReturn( [] );
14+
Functions\when( 'get_option' )->justReturn( array() );
1415
Functions\when( 'update_option' )->justReturn( true );
1516

1617
return new MslsOptions();
@@ -36,9 +37,9 @@ public function test_is_query_page_method(): void {
3637

3738
public function test_create_method(): void {
3839
Functions\when( 'is_admin' )->justReturn( true );
39-
Functions\when( 'get_post_types' )->justReturn( [] );
40+
Functions\when( 'get_post_types' )->justReturn( array() );
4041
Functions\when( 'get_post_type' )->justReturn( 'post' );
41-
Functions\when( 'get_option' )->justReturn( [] );
42+
Functions\when( 'get_option' )->justReturn( array() );
4243

4344
$this->assertInstanceOf( MslsOptions::class, MslsOptions::create() );
4445
}
@@ -49,14 +50,23 @@ public function test_get_arg_method(): void {
4950
$this->assertNull( $obj->get_arg( 0 ) );
5051
$this->assertIsSTring( $obj->get_arg( 0, '' ) );
5152
$this->assertIsFloat( $obj->get_arg( 0, 1.1 ) );
52-
$this->assertIsArray( $obj->get_arg( 0, [] ) );
53+
$this->assertIsArray( $obj->get_arg( 0, array() ) );
5354
}
5455

5556
function test_set_method(): void {
5657
$obj = $this->get_test();
5758

58-
$this->assertTrue( $obj->set( [] ) );
59-
$this->assertTrue( $obj->set( array( 'temp' => 'abc' ) ) );
59+
$this->assertTrue( $obj->set( array() ) );
60+
$this->assertTrue(
61+
$obj->set(
62+
array(
63+
'temp' => 'abc',
64+
'en' => 1,
65+
'us' => 2,
66+
)
67+
)
68+
);
69+
6070
$this->assertFalse( $obj->set( 'Test' ) );
6171
$this->assertFalse( $obj->set( 1 ) );
6272
$this->assertFalse( $obj->set( 1.1 ) );
@@ -120,11 +130,37 @@ function test_get_flag_url_method(): void {
120130
}
121131

122132
function test_get_available_languages_method(): void {
123-
Functions\when( 'get_available_languages' )->justReturn( [] );
133+
Functions\expect( 'get_available_languages' )->once()->andReturn( array( 'de_DE', 'it_IT' ) );
134+
Functions\expect( 'format_code_lang' )->atLeast()->once()->andReturnUsing(
135+
function ( $code ) {
136+
$map = array(
137+
'de_DE' => 'German',
138+
'it_IT' => 'Italian',
139+
);
140+
return $map[ $code ] ?? 'American English';
141+
}
142+
);
143+
144+
$obj = $this->get_test();
145+
146+
$expected = array(
147+
'en_US' => 'American English',
148+
'de_DE' => 'German',
149+
'it_IT' => 'Italian',
150+
);
151+
$this->assertEquals( $expected, $obj->get_available_languages() );
152+
}
124153

154+
public function test_get_icon_type_standard(): void {
125155
$obj = $this->get_test();
126156

127-
$this->assertIsArray( $obj->get_available_languages() );
157+
$this->assertEquals( MslsAdminIcon::TYPE_FLAG, $obj->get_icon_type() );
128158
}
129159

160+
public function test_get_icon_type_admin_display(): void {
161+
$obj = $this->get_test();
162+
$obj->set( array( 'admin_display' => MslsAdminIcon::TYPE_LABEL ) );
163+
164+
$this->assertEquals( MslsAdminIcon::TYPE_LABEL, $obj->get_icon_type() );
165+
}
130166
}

0 commit comments

Comments
 (0)