Skip to content

Commit

Permalink
Test different page size
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemd24 committed Jul 17, 2024
1 parent 79f3b7b commit c104eea
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/Unit/API/Site/Controllers/Ads/CampaignControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,36 @@ public function test_get_campaigns_with_args() {
],
];

$this->ads_campaign->expects( $this->once() )
$matcher = $this->exactly( 2 );
$this->ads_campaign->expects( $matcher ) // We will make two requests with different per_page values.
->method( 'get_campaigns' )
->with(
true,
true,
[
'per_page' => 2,
'exclude_removed' => true,
]
)
->willReturn( $campaigns_data );
->willReturnCallback(
function ( $exclude_removed, $fetch_criterion, $args ) use ( $matcher ) {
$this->assertTrue( $exclude_removed );
$this->assertTrue( $fetch_criterion );

if ( $matcher->getInvocationCount() === 1 ) {
$this->assertEquals( 2, $args['per_page'] ); // First request.
}

$response = $this->do_request( self::ROUTE_CAMPAIGNS, 'GET', [ 'per_page' => 2 ] );
if ( $matcher->getInvocationCount() === 2 ) {
$this->assertEquals( 1, $args['per_page'] ); // Second request.
}

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 ] );

// First request.
$this->assertEquals( $expected, $response->get_data() );
$this->assertEquals( 200, $response->get_status() );

// Second request.
$this->assertEquals( [ $expected[0] ], $response_2->get_data() );
$this->assertEquals( 200, $response_2->get_status() );
}

public function test_get_campaigns_with_api_exception() {
Expand Down

0 comments on commit c104eea

Please sign in to comment.