Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Test Coverage #95

Merged
Merged
2 changes: 1 addition & 1 deletion tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_it_returns_true_with_container_set() {
$this->assertTrue( Config::has_container() );
}

public function test_it_returns_true_with_no_container_set() {
public function test_it_returns_false_with_no_container_set() {
$this->assertFalse( Config::has_container() );
}

Expand Down
32 changes: 32 additions & 0 deletions tests/wpunit/CoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Handles all tests related to the Core class.
*/
use StellarWP\Telemetry\Config;
use lucatume\WPBrowser\TestCase\WPTestCase;
use StellarWP\Telemetry\Core;
use StellarWP\Telemetry\Tests\Container;

class CoreTest extends WPTestCase {

public function test_it_throws_exception_without_container() {
$core = new Core();

$this->expectException( RuntimeException::class );
$core->init( '/some/path/to/plugin.php' );
}

public function test_it_returns_a_valid_instance() {
Config::set_container( new Container() );
$core = Config::get_container()->get( Core::class );
$this->assertInstanceOf( Core::class, $core->instance() );
}

public function test_it_returns_container_interface() {
Config::set_container( new Container() );
$core = Config::get_container()->get( Core::class );
$core->init( '/some/path/to/plugin.php' );

$this->assertInstanceOf( \StellarWP\ContainerContract\ContainerInterface::class, $core->container() );
}
}
1 change: 0 additions & 1 deletion tests/wpunit/Telemetry_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Codeception\TestCase\WPTestCase;
use StellarWP\Telemetry\Config;
use StellarWP\Telemetry\Data_Providers\Debug_Data;
use StellarWP\Telemetry\Data_Providers\Null_Data_Provider;
use StellarWP\Telemetry\Opt_In\Status;
use StellarWP\Telemetry\Telemetry\Telemetry;
Expand Down
59 changes: 59 additions & 0 deletions tests/wpunit/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,63 @@ static function ( string $_template_file, bool $require_once, array $args ) use
$this->assertSame( false, $require );
$this->assertSame( $template->get_args( 'telemetry-library' ), $arguments );
}

public function test_get_option_name() {
$template = Config::get_container()->get( Opt_In_Template::class );

$this->assertSame( 'stellarwp_telemetry_telemetry-library_show_optin', $template->get_option_name( 'telemetry-library' ) );
tarecord marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_should_render() {
$template = Config::get_container()->get( Opt_In_Template::class );
$option_name = $template->get_option_name( 'telemetry-library' );

update_option( $option_name, true );

$this->assertTrue( $template->should_render( 'telemetry-library' ) );

update_option( $option_name, false );

$this->assertFalse( $template->should_render( 'telemetry-library' ) );
}

public function test_maybe_render() {
$status = Config::get_container()->get( Status::class );
$template = Config::get_container()->get( Opt_In_Template::class );

update_option(
$status->get_option_name(),
[
'plugins' => [
'the-events-calendar' => [
'wp_slug' => 'the-events-calendar/the-events-calendar.php',
'optin' => false,
],
],
'token' => 'abcd1234',
]
);

update_option( $template->get_option_name( 'telemetry-library' ), true);

$file = null;
$require = null;
$arguments = null;

$this->set_fn_return(
'load_template',
static function ( string $_template_file, bool $require_once, array $args ) use ( &$file, &$require, &$arguments ) {
$file = $_template_file;
$require = $require_once;
$arguments = $args;
},
true
);

$template->maybe_render( 'telemetry-library' );

$this->assertSame( '/var/www/html/wp-content/plugins/telemetry/src/views/optin.php', $file );
$this->assertSame( false, $require );
$this->assertSame( $template->get_args( 'telemetry-library' ), $arguments );
}
}
Loading