Skip to content

Commit

Permalink
Merge pull request #371 from lloc/raise-coverage
Browse files Browse the repository at this point in the history
Raise coverage
  • Loading branch information
lloc authored Sep 6, 2024
2 parents 1db2ba0 + dcfe857 commit 7744cae
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 42 deletions.
14 changes: 3 additions & 11 deletions includes/MslsShortCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@

class MslsShortCode {


protected MslsOptions $options;

public function __construct( MslsOptions $options ) {
$this->options = $options;
}

public static function init(): void {
$obj = new self( msls_options() );
add_shortcode( 'sc_msls_widget', array( $obj, 'render_widget' ) );
add_shortcode( 'sc_msls_widget', array( __CLASS__, 'render_widget' ) );
add_shortcode( 'sc_msls', 'get_the_msls' );
}

Expand All @@ -22,8 +14,8 @@ public static function init(): void {
*
* @return string|false
*/
public function render_widget() {
if ( $this->options->is_excluded() ) {
public static function render_widget() {
if ( msls_options()->is_excluded() ) {
return '';
}

Expand Down
72 changes: 47 additions & 25 deletions tests/phpunit/TestMslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ public function get_test(): MslsOptions {
return new MslsOptions();
}

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

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

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

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

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

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

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

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

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

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

function test_set_method(): void {
public function test_save(): void {
$arr = array(
'de_DE' => 1,
'it_IT' => 2,
);

Functions\expect( 'delete_option' )->once()->with( 'msls' );
Functions\expect( 'add_option' )->once()->with( 'msls', $arr, '', true );

$obj = $this->get_test();

$this->assertTrue( $obj->set( array() ) );
$this->assertTrue(
$obj->set(
$this->expectNotToPerformAssertions();
$obj->save( $arr );
}

public static function set_provider(): array {
return array(
array( true, array() ),
array(
true,
array(
'temp' => 'abc',
'en' => 1,
'us' => 2,
)
)
),
),
array( false, 'Test' ),
array( false, 1 ),
array( false, 1.1 ),
array( false, null ),
array( false, new \stdClass() ),
);
}

/**
* @dataProvider set_provider
*/
function test_set( $expected, $input ): void {
$obj = $this->get_test();

$this->assertFalse( $obj->set( 'Test' ) );
$this->assertFalse( $obj->set( 1 ) );
$this->assertFalse( $obj->set( 1.1 ) );
$this->assertFalse( $obj->set( null ) );
$this->assertFalse( $obj->set( new \stdClass() ) );
$this->assertEquals( $expected, $obj->set( $input ) );
}

function test_get_permalink_method(): void {
function test_get_permalink(): void {
$obj = $this->get_test();

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

function test_get_postlink_method(): void {
function test_get_postlink(): void {
$obj = $this->get_test();

$this->assertIsSTring( $obj->get_postlink( 'de_DE' ) );
$this->assertEquals( '', $obj->get_postlink( 'de_DE' ) );
}

function test_get_current_link_method(): void {
function test_get_current_link(): void {
$obj = $this->get_test();

$this->assertIsSTring( $obj->get_current_link() );
}

function test_is_excluded_method(): void {
function test_is_excluded(): void {
$obj = $this->get_test();

$this->assertIsBool( $obj->is_excluded() );
}

function test_is_content_filter_method(): void {
function test_is_content_filter(): void {
$obj = $this->get_test();

$this->assertIsBool( $obj->is_content_filter() );
}

function test_get_order_method(): void {
function test_get_order(): void {
$obj = $this->get_test();

$this->assertIsSTring( $obj->get_order() );
}

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

$obj = $this->get_test();

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

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

function test_get_available_languages_method(): void {
function test_get_available_languages(): void {
Functions\expect( 'get_available_languages' )->once()->andReturn( array( 'de_DE', 'it_IT' ) );
Functions\expect( 'format_code_lang' )->atLeast()->once()->andReturnUsing(
function ( $code ) {
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/TestMslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace lloc\MslsTests;

use Brain\Monkey\Functions;
use Brain\Monkey\Actions;
use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsOptions;
Expand Down Expand Up @@ -34,6 +35,28 @@ protected function setUp(): void {
$this->test = new MslsPostTag( $options, $collection );
}

public function test_init() {
$options = \Mockery::mock( MslsOptions::class );
$options->activate_autocomplete = true;

$collection = \Mockery::mock( MslsBlogCollection::class );

$taxonomy = \Mockery::mock( MslsTaxonomy::class );
$taxonomy->shouldReceive( 'acl_request' )->once()->andReturn( 'post_tag' );

Functions\expect( 'msls_options' )->atLeast()->once()->andReturn( $options );
Functions\expect( 'msls_blog_collection' )->atLeast()->once()->andReturn( $collection );
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );

Actions\expectAdded( 'post_tag_edit_form_fields' )->once();
Actions\expectAdded( 'post_tag_add_form_fields' )->once();
Actions\expectAdded( 'edited_post_tag' )->once();
Actions\expectAdded( 'create_post_tag' )->once();

$this->expectNotToPerformAssertions();
MslsPostTag::init();
}

/**
* Verify the static suggest-method
*/
Expand Down
20 changes: 14 additions & 6 deletions tests/phpunit/TestMslsShortCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,39 @@
namespace lloc\MslsTests;

use Brain\Monkey\Functions;
use lloc\Msls\MslsBlock;
use lloc\Msls\MslsOptions;

use lloc\Msls\MslsShortCode;

use function Brain\Monkey\Functions;

class TestMslsShortCode extends MslsUnitTestCase {

public function test_init(): void {
Functions\expect( 'add_shortcode' )->once()->with( 'sc_msls_widget', array( MslsShortCode::class, 'render_widget' ) );
Functions\expect( 'add_shortcode' )->once()->with( 'sc_msls', 'get_the_msls' );

$this->expectNotToPerformAssertions();
MslsShortCode::init();
}

public function test_block_render_excluded_true(): void {
$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_excluded' )->andReturn( true );

$this->assertEquals( '', ( new MslsShortCode( $options ) )->render_widget() );
Functions\expect( 'msls_options' )->once()->andReturn( $options );

$this->assertEquals( '', MslsShortCode::render_widget() );
}


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

Functions\expect( 'msls_options' )->once()->andReturn( $options );

$expected = '<div class="msls-shortcode">Widget Output</div>';

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

$this->assertEquals( $expected, ( new MslsShortCode( $options ) )->render_widget() );
$this->assertEquals( $expected, MslsShortCode::render_widget() );
}
}

0 comments on commit 7744cae

Please sign in to comment.