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
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
4 changes: 4 additions & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ extensions:
- Codeception\Command\GenerateWPUnit
params:
- .env.testing
coverage:
enabled: true
include:
- src/Telemetry/*.php
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"lucatume/di52": "3.0.0",
"lucatume/wp-browser": "^4.0",
"phpcompatibility/phpcompatibility-wp": "*",
"phpunit/php-code-coverage": "^9.2",
"szepeviktor/phpstan-wordpress": "^1.1",
"the-events-calendar/coding-standards": "dev-master",
"wp-coding-standards/wpcs": "^2.3"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions src/Telemetry/Contracts/Template_Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ interface Template_Interface {
*/
public function render( string $stellar_slug );

/**
* Enqueues assets for the rendered template.
*
* @since 1.0.0
*
* @return void
*/
public function enqueue();

/**
* Determines if the template should be rendered.
*
Expand Down
11 changes: 0 additions & 11 deletions src/Telemetry/Exit_Interview/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,6 @@ public function render( string $stellar_slug ) {
load_template( dirname( dirname( __DIR__ ) ) . '/views/exit-interview.php', false, $this->get_args( $stellar_slug ) );
}

/**
* @inheritDoc
*
* @since 1.0.0
*
* @return void
*/
public function enqueue() {
// TODO: Implement enqueue() method.
}

/**
* @inheritDoc
*
Expand Down
9 changes: 0 additions & 9 deletions src/Telemetry/Opt_In/Opt_In_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ public function __construct( Status $opt_in_status ) {
$this->opt_in_status = $opt_in_status;
}

/**
* @inheritDoc
*
* @return void
*/
public function enqueue(): void {
// TODO: Once FE template is done, enqueue it here.
}

/**
* Gets the arguments for configuring how the Opt-In modal is rendered.
*
Expand Down
24 changes: 22 additions & 2 deletions tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

use Codeception\TestCase\WPTestCase;
/**
* Handles all tests related to the Config class.
*/
use StellarWP\Telemetry\Config;
use lucatume\WPBrowser\TestCase\WPTestCase;
use StellarWP\Telemetry\Tests\Container;

class ConfigTest extends WPTestCase {

Expand All @@ -28,9 +31,26 @@ public function test_it_should_throw_exception_without_container() {
Config::get_container();
}

public function test_it_returns_true_with_container_set() {
Config::set_container( new Container() );

$this->assertTrue( Config::has_container() );
}

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

public function test_it_should_set_stellar_slug() {
Config::set_stellar_slug( 'unique_slug' );

$this->assertEquals( 'unique_slug', Config::get_stellar_slug() );
}

public function test_it_should_add_stellar_slug() {
Config::add_stellar_slug( 'additional_stellar_slug', 'path/to/wp-slug' );

$this->assertIsArray( Config::get_all_stellar_slugs() );
$this->assertContains( 'additional_stellar_slug', array_keys( Config::get_all_stellar_slugs() ) );
}
}
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() );
}
}
200 changes: 199 additions & 1 deletion tests/wpunit/EventTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
/**
* Handles all tests related to event sending.
*/

use lucatume\WPBrowser\TestCase\WPTestCase;
use StellarWP\Telemetry\Config;
use StellarWP\Telemetry\Data_Providers\Null_Data_Provider;
use StellarWP\Telemetry\Events\Event;
Expand All @@ -8,7 +12,7 @@
use StellarWP\Telemetry\Tests\Support\Traits\With_Test_Container;
use StellarWP\Telemetry\Tests\Support\Traits\With_Uopz;

class EventTest extends \Codeception\TestCase\WPTestCase {
class EventTest extends WPTestCase {

use With_Test_Container;
use With_Uopz;
Expand Down Expand Up @@ -72,4 +76,198 @@ static function ( string $url, array $args ) use ( $mock_response, &$call_url, &
$this->assertSame( $event_data, $call_args['body'] );
$this->assertTrue( $sent );
}

/**
* It should return false with failed event send.
*
* @test
*/
public function should_return_false_with_failed_send() {
$mock_response = [
'headers' => [],
'body' => json_encode([]), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
$call_url = null;
$call_args = null;
$this->set_fn_return(
'wp_remote_post',
static function ( string $url, array $args ) use ( $mock_response, &$call_url, &$call_args ) {
$call_url = $url;
$call_args = $args;

return $mock_response;
},
true
);

$opt_in_status = new Status();
$telemetry = new Telemetry( new Null_Data_Provider(), $opt_in_status );
$event = new Event( $telemetry );

$opt_in_status->set_status( true );

$telemetry->save_token( 'abcd1234' );

$event_data = [
'token' => 'abcd1234',
'stellar_slug' => Config::get_stellar_slug(),
'event' => 'my-event',
'event_data' => wp_json_encode(
[
'some' => 'data',
]
),
];

$sent = $event->send( 'my-event', [ 'some' => 'data' ] );

$this->assertEquals( Config::get_server_url() . '/events', $call_url );
$this->assertSame( $event_data, $call_args['body'] );
$this->assertFalse( $sent );
}


/**
* It should successfully send a batch of events.
*
* @test
*/
public function should_send_batched_events() {
$mock_response = [
'headers' => [],
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
[
'status' => 'true',
'message' => 'success',
]
),
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
$call_url = null;
$call_args = null;
$this->set_fn_return(
'wp_remote_post',
static function ( string $url, array $args ) use ( $mock_response, &$call_url, &$call_args ) {
$call_url = $url;
$call_args = $args;

return $mock_response;
},
true
);

$opt_in_status = new Status();
$telemetry = new Telemetry( new Null_Data_Provider(), $opt_in_status );
$event = new Event( $telemetry );

$opt_in_status->set_status( true );

$telemetry->save_token( 'abcd1234' );

$events = [
[
'stellar_slug' => 'the-events-calendar',
'event' => 'event-created',
'event_data' => wp_json_encode(
[
'title' => 'Annual Golf Tournament',
]
),
],
[
'stellar_slug' => 'give',
'event' => 'published-donation-form',
'event_data' => wp_json_encode(
[
'donation_amount' => '50000',
]
),
]
];

$expected_args = [
'token' => 'abcd1234',
'events' => $events,
];

$sent = $event->send_batch( $events );

$this->assertEquals( Config::get_server_url() . '/events', $call_url );
$this->assertSame( $expected_args, $call_args['body'] );
$this->assertTrue( $sent );
}

/**
* It should successfully send a batch of events.
*
* @test
*/
public function should_return_false_with_failed_batch_send() {
$mock_response = [
'headers' => [],
'body' => json_encode([]), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
'response' => [
'code' => false,
'message' => false,
],
'cookies' => [],
'http_response' => null,
];
$call_url = null;
$call_args = null;
$this->set_fn_return(
'wp_remote_post',
static function ( string $url, array $args ) use ( $mock_response, &$call_url, &$call_args ) {
$call_url = $url;
$call_args = $args;

return $mock_response;
},
true
);

$opt_in_status = new Status();
$telemetry = new Telemetry( new Null_Data_Provider(), $opt_in_status );
$event = new Event( $telemetry );

$opt_in_status->set_status( true );

$telemetry->save_token( 'abcd1234' );

$events = [
[
'stellar_slug' => 'the-events-calendar',
'event' => 'event-created',
'event_data' => wp_json_encode(
[
'title' => 'Annual Golf Tournament',
]
),
],
[
'stellar_slug' => 'give',
'event' => 'published-donation-form',
'event_data' => wp_json_encode(
[
'donation_amount' => '50000',
]
),
]
];

$sent = $event->send_batch( $events );

$this->assertFalse( $sent );
}
}
Loading
Loading