Skip to content

Commit

Permalink
Adjusting unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
message-dimke committed Jul 11, 2024
1 parent b4b908a commit a9cacf2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
9 changes: 7 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
defaultTestSuite="unit"
>
<testsuites>
<testsuite name="unit">
<directory suffix=".php">./tests/Unit</directory>
<directory>./tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>./tests/Integration</directory>
</testsuite>
<testsuite name="e2e">
<directory>./tests/E2e</directory>
</testsuite>
</testsuites>
</phpunit>
11 changes: 7 additions & 4 deletions tests/E2e/Pinterest401DisconnectE2eTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php
<?php declare( strict_types=1 );

use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\Notes\TokenInvalidFailure;
namespace Automattic\WooCommerce\Pinterest\Tests\E2e;

class Pinterest401DisconnectE2eTest extends WP_UnitTestCase {
use Automattic\WooCommerce\Pinterest\Notes\TokenInvalidFailure;
use Exception;
use Pinterest_For_Woocommerce;

class Pinterest401DisconnectE2eTest extends \WP_UnitTestCase
{
protected function setUp(): void {
parent::setUp();

Expand Down
14 changes: 12 additions & 2 deletions tests/E2e/PinterestConnectE2eTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php
<?php declare( strict_types=1 );

namespace Automattic\WooCommerce\Pinterest\Tests\E2e;

use Automattic\WooCommerce\Pinterest\Notes\TokenInvalidFailure;
use Pinterest_For_Woocommerce;

class PinterestConnectE2eTest extends \WP_UnitTestCase {

class PinterestConnectE2eTest extends WP_UnitTestCase {
protected function setUp(): void {
parent::setUp();

Pinterest_For_Woocommerce::save_settings( [] );
}

/**
* Tests successful Pinterest auth produces proper settings after all pinterest_for_woocommerce_token_saved hooks are fired.
Expand Down Expand Up @@ -80,6 +89,7 @@ public function test_successful_pinterest_connect_sets_proper_settings() {
'erase_plugin_data' => false,
'tracking_advertiser' => '549765662491',
'tracking_tag' => '2613286171854',
'track_conversions_capi' => false,
),
$settings
);
Expand Down
61 changes: 61 additions & 0 deletions tests/Integration/FeedsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace Automattic\WooCommerce\Pinterest\Tests\Integration;

use Automattic\WooCommerce\Pinterest\Feeds;
use Automattic\WooCommerce\Pinterest\PinterestApiException;
use Pinterest_For_Woocommerce;

class FeedsTest extends \WP_UnitTestCase {

/**
* Tests emulates Pinterest create feed endpoint response when a feed with the same name already exists.
*
* @return void
*/
public function test_feed_registration_handles_422_name_already_exists()
{
$this->expectException( PinterestApiException::class );
$this->expectExceptionCode( 422 );
$this->expectExceptionMessage( 'Unprocessable Entity' );

Pinterest_For_Woocommerce::save_setting( 'tracking_advertiser', '123' );
Pinterest_For_Woocommerce::save_data( 'local_feed_ids', '' );

$this->create_feed_request_stub();

Feeds::create_feed();
}

private function create_feed_request_stub()
{
add_filter(
'pre_http_request',
function ($response, $parsed_args, $url) {
if ('https://api.pinterest.com/v5/catalogs/feeds?ad_account_id=123' === $url) {
$response = array(
'headers' => array(
'content-type' => 'application/json',
),
'body' => json_encode(
array(
'code' => 4170,
'message' => 'Unprocessable Entity',
)
),
'response' => array(
'code' => 422,
'message' => 'OK',
),
'cookies' => array(),
'filename' => '',
);
}

return $response;
},
10,
3
);
}
}

0 comments on commit a9cacf2

Please sign in to comment.