Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Martyn Jones committed Jul 4, 2023
1 parent 9752642 commit 8b95852
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Internal/DependencyManagement/GoogleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function register_guzzle() {
$handler_stack->remove( 'http_errors' );
$handler_stack->push( $this->error_handler(), 'http_errors' );
$handler_stack->push( $this->add_auth_header() );
$handler_stack->push( $this->add_plugin_version_header() );
$handler_stack->push( $this->add_plugin_version_header(), 'plugin_version_header' );

// Override endpoint URL if we are using http locally.
if ( 0 === strpos( $this->get_connect_server_url_root()->getValue(), 'http://' ) ) {
Expand Down Expand Up @@ -258,18 +258,19 @@ protected function add_auth_header(): callable {
/**
* Add client name and version headers to request
*
* @since x.x.x
*
* @return callable
*/
protected function add_plugin_version_header(): callable {
public function add_plugin_version_header(): callable {
return function( callable $handler ) {
return function( RequestInterface $request, array $options ) use ( $handler ) {
try {
$request = $request->withHeader( 'x-client-name', $this->get_client_name() );
$request = $request->withHeader( 'x-client-version', $this->get_version() );
$request = $request->withHeader( 'x-client-name', $this->get_client_name() )
->withHeader( 'x-client-version', $this->get_version() );
} catch ( WPError $error ) {
do_action( 'woocommerce_gla_guzzle_client_exception', $error, __METHOD__ . ' in add_plugin_version_header()' );

Check warning on line 272 in src/Internal/DependencyManagement/GoogleServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Internal/DependencyManagement/GoogleServiceProvider.php#L271-L272

Added lines #L271 - L272 were not covered by tests
}

return $handler( $request, $options );
};
};
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/API/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API;

use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\ContainerAwareUnitTest;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\GuzzleHttp\Client;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\DependencyManagement\GoogleServiceProvider;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Http\Message\RequestInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use ReflectionClass;

defined( 'ABSPATH' ) || exit;

/**
* Class ClientTest
*
* @since x.x.x
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API
*/
class ClientTest extends ContainerAwareUnitTest {
use PluginHelper;

/**
* Confirm that the client handler stack includes the `plugin_version_header
*
* @return void
*/
public function test_plugin_version_header_in_handler_stack(): void {
$client = $this->container->get( Client::class );
$handler = $client->getConfig( 'handler' );
$reflection = new ReflectionClass( $handler );

$property = $reflection->getProperty( 'stack' );
$property->setAccessible( true );

$handler_stack = $property->getValue( $handler );

$this->assertNotFalse( array_search( 'plugin_version_header', array_column( $handler_stack, 1 ), true ) );
}

/**
* Confirm that withHeader is called on RequestInterface in
* add_plugin_version_header to set the x-client-name header.
*
* @return void
*/
public function test_plugin_version_headers(): void {
$service = new GoogleServiceProvider();

$request = $this->createMock( RequestInterface::class );
$request->expects( $this->once() )
->method( 'withHeader' )
->withConsecutive( [ 'x-client-name', $this->get_client_name() ] );

$service->add_plugin_version_header()( function(){ } )( $request, [] );
}
}

0 comments on commit 8b95852

Please sign in to comment.