Skip to content

Commit 7744cae

Browse files
authored
Merge pull request #371 from lloc/raise-coverage
Raise coverage
2 parents 1db2ba0 + dcfe857 commit 7744cae

File tree

4 files changed

+87
-42
lines changed

4 files changed

+87
-42
lines changed

includes/MslsShortCode.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44

55
class MslsShortCode {
66

7-
8-
protected MslsOptions $options;
9-
10-
public function __construct( MslsOptions $options ) {
11-
$this->options = $options;
12-
}
13-
147
public static function init(): void {
15-
$obj = new self( msls_options() );
16-
add_shortcode( 'sc_msls_widget', array( $obj, 'render_widget' ) );
8+
add_shortcode( 'sc_msls_widget', array( __CLASS__, 'render_widget' ) );
179
add_shortcode( 'sc_msls', 'get_the_msls' );
1810
}
1911

@@ -22,8 +14,8 @@ public static function init(): void {
2214
*
2315
* @return string|false
2416
*/
25-
public function render_widget() {
26-
if ( $this->options->is_excluded() ) {
17+
public static function render_widget() {
18+
if ( msls_options()->is_excluded() ) {
2719
return '';
2820
}
2921

tests/phpunit/TestMslsOptions.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ public function get_test(): MslsOptions {
1717
return new MslsOptions();
1818
}
1919

20-
public function test_is_main_page_method(): void {
20+
public function test_is_main_page(): void {
2121
Functions\when( 'is_front_page' )->justReturn( true );
2222

2323
$this->assertIsBool( MslsOptions::is_main_page() );
2424
}
2525

26-
public function test_is_tax_page_method(): void {
26+
public function test_is_tax_page(): void {
2727
Functions\when( 'is_category' )->justReturn( true );
2828

2929
$this->assertIsBool( MslsOptions::is_tax_page() );
3030
}
3131

32-
public function test_is_query_page_method(): void {
32+
public function test_is_query_page(): void {
3333
Functions\when( 'is_date' )->justReturn( true );
3434

3535
$this->assertIsBool( MslsOptions::is_query_page() );
3636
}
3737

38-
public function test_create_method(): void {
38+
public function test_create(): void {
3939
$post_type = \Mockery::mock( MslsPostType::class );
4040
$post_type->shouldReceive( 'is_taxonomy' )->once()->andReturnFalse();
4141

@@ -47,7 +47,7 @@ public function test_create_method(): void {
4747
$this->assertInstanceOf( MslsOptions::class, MslsOptions::create() );
4848
}
4949

50-
public function test_get_arg_method(): void {
50+
public function test_get_arg(): void {
5151
$obj = $this->get_test();
5252

5353
$this->assertNull( $obj->get_arg( 0 ) );
@@ -56,73 +56,95 @@ public function test_get_arg_method(): void {
5656
$this->assertIsArray( $obj->get_arg( 0, array() ) );
5757
}
5858

59-
function test_set_method(): void {
59+
public function test_save(): void {
60+
$arr = array(
61+
'de_DE' => 1,
62+
'it_IT' => 2,
63+
);
64+
65+
Functions\expect( 'delete_option' )->once()->with( 'msls' );
66+
Functions\expect( 'add_option' )->once()->with( 'msls', $arr, '', true );
67+
6068
$obj = $this->get_test();
6169

62-
$this->assertTrue( $obj->set( array() ) );
63-
$this->assertTrue(
64-
$obj->set(
70+
$this->expectNotToPerformAssertions();
71+
$obj->save( $arr );
72+
}
73+
74+
public static function set_provider(): array {
75+
return array(
76+
array( true, array() ),
77+
array(
78+
true,
6579
array(
6680
'temp' => 'abc',
6781
'en' => 1,
6882
'us' => 2,
69-
)
70-
)
83+
),
84+
),
85+
array( false, 'Test' ),
86+
array( false, 1 ),
87+
array( false, 1.1 ),
88+
array( false, null ),
89+
array( false, new \stdClass() ),
7190
);
91+
}
92+
93+
/**
94+
* @dataProvider set_provider
95+
*/
96+
function test_set( $expected, $input ): void {
97+
$obj = $this->get_test();
7298

73-
$this->assertFalse( $obj->set( 'Test' ) );
74-
$this->assertFalse( $obj->set( 1 ) );
75-
$this->assertFalse( $obj->set( 1.1 ) );
76-
$this->assertFalse( $obj->set( null ) );
77-
$this->assertFalse( $obj->set( new \stdClass() ) );
99+
$this->assertEquals( $expected, $obj->set( $input ) );
78100
}
79101

80-
function test_get_permalink_method(): void {
102+
function test_get_permalink(): void {
81103
$obj = $this->get_test();
82104

83105
$this->assertIsSTring( $obj->get_permalink( 'de_DE' ) );
84106
}
85107

86-
function test_get_postlink_method(): void {
108+
function test_get_postlink(): void {
87109
$obj = $this->get_test();
88110

89111
$this->assertIsSTring( $obj->get_postlink( 'de_DE' ) );
90112
$this->assertEquals( '', $obj->get_postlink( 'de_DE' ) );
91113
}
92114

93-
function test_get_current_link_method(): void {
115+
function test_get_current_link(): void {
94116
$obj = $this->get_test();
95117

96118
$this->assertIsSTring( $obj->get_current_link() );
97119
}
98120

99-
function test_is_excluded_method(): void {
121+
function test_is_excluded(): void {
100122
$obj = $this->get_test();
101123

102124
$this->assertIsBool( $obj->is_excluded() );
103125
}
104126

105-
function test_is_content_filter_method(): void {
127+
function test_is_content_filter(): void {
106128
$obj = $this->get_test();
107129

108130
$this->assertIsBool( $obj->is_content_filter() );
109131
}
110132

111-
function test_get_order_method(): void {
133+
function test_get_order(): void {
112134
$obj = $this->get_test();
113135

114136
$this->assertIsSTring( $obj->get_order() );
115137
}
116138

117-
function test_get_url_method(): void {
139+
function test_get_url(): void {
118140
Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' );
119141

120142
$obj = $this->get_test();
121143

122144
$this->assertIsSTring( $obj->get_url( '/dev/test' ) );
123145
}
124146

125-
function test_get_flag_url_method(): void {
147+
function test_get_flag_url(): void {
126148
Functions\when( 'is_admin' )->justReturn( true );
127149
Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' );
128150
Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' );
@@ -132,7 +154,7 @@ function test_get_flag_url_method(): void {
132154
$this->assertIsSTring( $obj->get_flag_url( 'de_DE' ) );
133155
}
134156

135-
function test_get_available_languages_method(): void {
157+
function test_get_available_languages(): void {
136158
Functions\expect( 'get_available_languages' )->once()->andReturn( array( 'de_DE', 'it_IT' ) );
137159
Functions\expect( 'format_code_lang' )->atLeast()->once()->andReturnUsing(
138160
function ( $code ) {

tests/phpunit/TestMslsPostTag.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace lloc\MslsTests;
44

55
use Brain\Monkey\Functions;
6+
use Brain\Monkey\Actions;
67
use lloc\Msls\MslsBlog;
78
use lloc\Msls\MslsBlogCollection;
89
use lloc\Msls\MslsOptions;
@@ -34,6 +35,28 @@ protected function setUp(): void {
3435
$this->test = new MslsPostTag( $options, $collection );
3536
}
3637

38+
public function test_init() {
39+
$options = \Mockery::mock( MslsOptions::class );
40+
$options->activate_autocomplete = true;
41+
42+
$collection = \Mockery::mock( MslsBlogCollection::class );
43+
44+
$taxonomy = \Mockery::mock( MslsTaxonomy::class );
45+
$taxonomy->shouldReceive( 'acl_request' )->once()->andReturn( 'post_tag' );
46+
47+
Functions\expect( 'msls_options' )->atLeast()->once()->andReturn( $options );
48+
Functions\expect( 'msls_blog_collection' )->atLeast()->once()->andReturn( $collection );
49+
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );
50+
51+
Actions\expectAdded( 'post_tag_edit_form_fields' )->once();
52+
Actions\expectAdded( 'post_tag_add_form_fields' )->once();
53+
Actions\expectAdded( 'edited_post_tag' )->once();
54+
Actions\expectAdded( 'create_post_tag' )->once();
55+
56+
$this->expectNotToPerformAssertions();
57+
MslsPostTag::init();
58+
}
59+
3760
/**
3861
* Verify the static suggest-method
3962
*/

tests/phpunit/TestMslsShortCode.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@
33
namespace lloc\MslsTests;
44

55
use Brain\Monkey\Functions;
6-
use lloc\Msls\MslsBlock;
76
use lloc\Msls\MslsOptions;
8-
97
use lloc\Msls\MslsShortCode;
108

11-
use function Brain\Monkey\Functions;
12-
139
class TestMslsShortCode extends MslsUnitTestCase {
1410

11+
public function test_init(): void {
12+
Functions\expect( 'add_shortcode' )->once()->with( 'sc_msls_widget', array( MslsShortCode::class, 'render_widget' ) );
13+
Functions\expect( 'add_shortcode' )->once()->with( 'sc_msls', 'get_the_msls' );
14+
15+
$this->expectNotToPerformAssertions();
16+
MslsShortCode::init();
17+
}
18+
1519
public function test_block_render_excluded_true(): void {
1620
$options = \Mockery::mock( MslsOptions::class );
1721
$options->shouldReceive( 'is_excluded' )->andReturn( true );
1822

19-
$this->assertEquals( '', ( new MslsShortCode( $options ) )->render_widget() );
23+
Functions\expect( 'msls_options' )->once()->andReturn( $options );
24+
25+
$this->assertEquals( '', MslsShortCode::render_widget() );
2026
}
2127

2228

2329
public function test_block_render_excluded_false(): void {
2430
$options = \Mockery::mock( MslsOptions::class );
2531
$options->shouldReceive( 'is_excluded' )->andReturn( false );
2632

33+
Functions\expect( 'msls_options' )->once()->andReturn( $options );
34+
2735
$expected = '<div class="msls-shortcode">Widget Output</div>';
2836

2937
Functions\when( 'the_widget' )->justEcho( $expected );
3038

31-
$this->assertEquals( $expected, ( new MslsShortCode( $options ) )->render_widget() );
39+
$this->assertEquals( $expected, MslsShortCode::render_widget() );
3240
}
3341
}

0 commit comments

Comments
 (0)