Skip to content

Commit

Permalink
test(integration): add missing mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-hours committed Sep 3, 2024
1 parent 5f621b7 commit e632fae
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
12 changes: 12 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ function _manually_active_plugins( $plugins ): array {

const WP_ADMIN = true;

if ( file_exists( $file = dirname( __FILE__, 2 ) . '/config/marketplaces.json' ) ) {
if ( ! unlink( $file ) ) {
echo 'Fail to remove file marketplaces.json' . PHP_EOL;
}
}

if ( file_exists( $file = dirname( __FILE__, 2 ) . '/export/flux.csv' ) ) {
if ( ! unlink( $file ) ) {
echo 'Fail to remove file flux.csv' . PHP_EOL;
}
}

// Start up the WP testing environment.
require "{$_tests_dir}/includes/bootstrap.php";

Expand Down
1 change: 1 addition & 0 deletions tests/integration/test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function test_admin_dashboard() {
public function test_admin_dashboard_end_free_trial() {
$this->init_with_mock_client();
$this->mock_on_access_token();
$this->mock_basic_stuff( [ Api\Restriction::API ] );

$json = json_decode( file_get_contents(
$this->sdk_mock_dir . 'restriction-restrictions.json'
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test-connect-cms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
declare(strict_types=1);

use Lengow\Sdk\Resource\Api;

/**
* Class Test_Connect_Cms
*
Expand Down Expand Up @@ -50,6 +52,7 @@ public function test_will_connect() {
public function test_will_fail() {
$this->init_with_mock_client( true, true );
$this->mock_on_access_token_fail();
$this->mock_basic_stuff( [], [ Api\Plugin::API ] );

$post_data = [
'do_action' => 'connect_cms',
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function mock_marketplace() {
public function test_import_order() {
$this->init_with_mock_client();
$this->mock_on_access_token();
$this->mock_basic_stuff();
$this->mock_marketplace();
$this->mock_order_list();

Expand Down
61 changes: 42 additions & 19 deletions tests/integration/trait-mock-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,50 @@ public function init_with_mock_client( bool $without_token = true, bool $without
}, true);
}

public function mock_basic_stuff() {
$this->mock_client->on(
new RequestMatcher( Api\Cms::API, null, [ 'GET' ] ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'cms-list.json'
) )
);
public function mock_basic_stuff( array $exclude = [], array $include = [] ) {

$this->mock_client->on(
new RequestMatcher( Api\Cms::API, null, [ 'PUT', 'POST' ] ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'cms.json'
) )
);
if (
! in_array( Api\Cms::API, $exclude )
&& ( empty( $include ) || in_array( Api\Cms::API, $include ) )
) {
$this->mock_client->on(
new RequestMatcher( Api\Cms::API, null, [ 'GET' ] ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'cms-list.json'
) )
);

$this->mock_client->on(
new RequestMatcher( Api\Restriction::API ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'restriction-restrictions.json'
) )
);
$this->mock_client->on(
new RequestMatcher( Api\Cms::API, null, [ 'PUT', 'POST' ] ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'cms.json'
) )
);
}

if (
! in_array( Api\Restriction::API, $exclude )
&& ( empty( $include ) || in_array( Api\Restriction::API, $include ) )
) {
$this->mock_client->on(
new RequestMatcher( Api\Restriction::API ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'restriction-restrictions.json'
) )
);
}

if (
! in_array( Api\Plugin::API, $exclude )
&& ( empty( $include ) || in_array( Api\Plugin::API, $include ) )
) {
$this->mock_client->on(
new RequestMatcher( Api\Plugin::API, null, [ 'GET' ] ),
new Response( 200, [], file_get_contents(
$this->sdk_mock_dir . 'plugin-list.json'
) )
);
}
}

public function mock_on_access_token() {
Expand Down

0 comments on commit e632fae

Please sign in to comment.