Skip to content

Commit

Permalink
Adjust tests to check pagination headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemd24 committed Jul 17, 2024
1 parent d128d8a commit 6ff5916
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
12 changes: 11 additions & 1 deletion tests/Tools/HelperTrait/GoogleAdsClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Google\Ads\GoogleAds\V16\Services\MutateOperation;
use Google\Ads\GoogleAds\V16\Services\MutateAssetGroupResult;
use Google\Ads\GoogleAds\V16\Services\MutateAssetResult;
use Google\Ads\GoogleAds\V16\Services\SearchGoogleAdsResponse;
use Google\ApiCore\ApiException;
use Google\ApiCore\Page;
use Google\ApiCore\PagedListResponse;
Expand Down Expand Up @@ -185,13 +186,22 @@ protected function generate_ads_multiple_query_mock( array $responses ) {
*
* @param array $campaigns_responses Set of campaign data to convert.
* @param array $campaign_criterion_responses Set of campaign criterion data to convert.
* @param bool $assert_pagination Whether to assert pagination.
*/
protected function generate_ads_campaign_query_mock( array $campaigns_responses, $campaign_criterion_responses ) {
protected function generate_ads_campaign_query_mock( array $campaigns_responses, $campaign_criterion_responses, $assert_pagination = false ) {
$campaigns_row_mock = array_map( [ $this, 'generate_campaign_row_mock' ], $campaigns_responses );
$campaign_criterion_row_mock = array_map( [ $this, 'generate_campaign_criterion_row_mock' ], $campaign_criterion_responses );

$list_response = $this->createMock( PagedListResponse::class );
$page = $this->createMock( Page::class );

if ( $assert_pagination ) {
$response_object = $this->createMock( SearchGoogleAdsResponse::class );
$response_object->expects( $this->exactly( 1 ) )->method( 'getTotalResultsCount' )->willReturn( count( $campaigns_responses ) );
$page->expects( $this->exactly( 1 ) )->method( 'getNextPageToken' )->willReturn( '' );
$page->method( 'getResponseObject' )->willReturn( $response_object );
}

$page->method( 'getIterator' )->willReturn(
$campaigns_row_mock,
);
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/API/Google/AdsCampaignTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function test_get_campaigns_with_search_args() {
],
];

$this->generate_ads_campaign_query_mock( $campaigns_data, $campaign_criterion_data );
$this->generate_ads_campaign_query_mock( $campaigns_data, $campaign_criterion_data, true );

$matcher = $this->exactly( 2 );
$this->service_client
Expand All @@ -193,7 +193,14 @@ function ( $request ) use ( $matcher ) {
}
);

$this->assertEquals( $campaigns_data, $this->campaign->get_campaigns( true, true, [ 'per_page' => 2 ] ) );
$this->assertEquals(
[
'campaigns' => $campaigns_data,
'total_results' => count( $campaigns_data ),
'next_page_token' => '',
],
$this->campaign->get_campaigns( true, true, [ 'per_page' => 2 ], true )
);
}

public function test_get_campaigns_with_nonexist_location_id() {
Expand Down
75 changes: 66 additions & 9 deletions tests/Unit/API/Site/Controllers/Ads/CampaignControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@ public function test_get_campaigns() {

$this->ads_campaign->expects( $this->once() )
->method( 'get_campaigns' )
->willReturn( $campaigns_data );
->willReturn(
[
'campaigns' => $campaigns_data,
'total_results' => 2,
'next_page_token' => '',
]
);

$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET' );

$this->assertEquals( $campaigns_data, $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 2, $headers['X-WP-Total'] );
$this->assertEquals( 1, $headers['X-WP-TotalPages'] );
$this->assertArrayNotHasKey( 'X-GLA-NextPageToken', $headers );
}

public function test_get_campaigns_converted_names() {
Expand Down Expand Up @@ -144,12 +155,23 @@ public function test_get_campaigns_converted_names() {
$this->ads_campaign->expects( $this->once() )
->method( 'get_campaigns' )
->with( false )
->willReturn( $campaigns_data );
->willReturn(
[
'campaigns' => $campaigns_data,
'total_results' => 2,
'next_page_token' => '',
]
);

$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'exclude_removed' => false ] );

$this->assertEquals( $expected, $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 2, $headers['X-WP-Total'] );
$this->assertEquals( 1, $headers['X-WP-TotalPages'] );
$this->assertArrayNotHasKey( 'X-GLA-NextPageToken', $headers );
}

public function test_get_campaigns_with_args() {
Expand Down Expand Up @@ -195,7 +217,7 @@ public function test_get_campaigns_with_args() {
],
];

$matcher = $this->exactly( 2 );
$matcher = $this->exactly( 3 );
$this->ads_campaign->expects( $matcher ) // We will make two requests with different per_page values.
->method( 'get_campaigns' )
->willReturnCallback(
Expand All @@ -213,18 +235,53 @@ function ( $exclude_removed, $fetch_criterion, $args ) use ( $matcher ) {

return true;
}
)->willReturnOnConsecutiveCalls( $campaigns_data, [ $campaigns_data[0] ] );

$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 2 ] );
$response_2 = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 1 ] );
)->willReturnOnConsecutiveCalls(
[
'campaigns' => $campaigns_data,
'total_results' => 2,
'next_page_token' => '',
],
[
'campaigns' => [ $campaigns_data[0] ],
'total_results' => 3,
'next_page_token' => 'pageToken',
],
[
'campaigns' => [],
'total_results' => 0,
'next_page_token' => '',
]
);

// First request.
$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 2 ] );
$this->assertEquals( $expected, $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 2, $headers['X-WP-Total'] );
$this->assertEquals( 1, $headers['X-WP-TotalPages'] );
$this->assertArrayNotHasKey( 'X-GLA-NextPageToken', $headers );

// Second request.
$this->assertEquals( [ $expected[0] ], $response_2->get_data() );
$this->assertEquals( 200, $response_2->get_status() );
$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 1 ] );
$this->assertEquals( [ $expected[0] ], $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 3, $headers['X-WP-Total'] );
$this->assertEquals( 3, $headers['X-WP-TotalPages'] );
$this->assertEquals( 'pageToken', $headers['X-GLA-NextPageToken'] );

// Third request.
$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 1 ] );
$this->assertEquals( [], $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 0, $headers['X-WP-Total'] );
$this->assertEquals( 0, $headers['X-WP-TotalPages'] );
$this->assertArrayNotHasKey( 'X-GLA-NextPageToken', $headers );
}

public function test_get_campaigns_with_api_exception() {
Expand Down

0 comments on commit 6ff5916

Please sign in to comment.