Skip to content

Commit

Permalink
Cache user API call for 5 minutes (#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona authored Oct 16, 2024
1 parent 7566f98 commit 25c8bfa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
9 changes: 6 additions & 3 deletions Tests/Unit/inc/classes/ImagifyUser/getError.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function testShouldReturnFalseWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\when( 'set_transient')->justReturn();

$this->assertFalse( ( new User() )->get_error() );
}
Expand All @@ -55,8 +56,9 @@ public function testShouldReturnFromCachedUserDataIfAvailable() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( $userData );
Functions\when( 'get_transient' )->justReturn( $userData );
Functions\expect( 'get_imagify_user' )->never();
Functions\when( 'set_transient')->justReturn();

$this->assertSame( '[email protected]', ( new User() )->email );
}
Expand All @@ -67,8 +69,9 @@ public function testShouldReturnFromCachedUserDataIfAvailable() {
public function testShouldReturnErrorWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );
Functions\when( 'set_transient')->justReturn();

$this->assertSame( $wp_error, ( new User() )->get_error() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class Test_GetPercentConsumedQuota extends TestCase {
public function testShouldReturnZeroWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );
Functions\when( 'set_transient')->justReturn();
Functions\expect( 'imagify_round_half_five' )->never();

$this->assertSame( ( new User() )->get_percent_consumed_quota(), 0 );
Expand All @@ -48,8 +49,9 @@ public function testShouldReturnQuotaWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\when( 'set_transient')->justReturn();
Functions\expect( 'imagify_round_half_five' )
->twice()
->with( 0 ) // extra_quota_consumed.
Expand Down
9 changes: 6 additions & 3 deletions Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class Test_IsOverQuota extends TestCase {
public function testShouldReturnFalseWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );
Functions\when( 'set_transient')->justReturn();

$this->assertFalse( ( new User() )->is_over_quota() );
}
Expand All @@ -46,8 +47,9 @@ public function testShouldReturnFalseWhenPaidAccount() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\when( 'set_transient')->justReturn();

$this->assertFalse( ( new User() )->is_over_quota() );
}
Expand Down Expand Up @@ -99,8 +101,9 @@ public function testShouldReturnTrueWhenFreeOverQuota() {
}

private function createMocks( $userData, $dataPreviousQuotaPercent ) {
Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\when( 'set_transient')->justReturn();
Functions\expect( 'imagify_round_half_five' )
->once()
->with( 0 ) // extra_quota_consumed.
Expand Down
1 change: 1 addition & 0 deletions Tests/bootstrap-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ function init_constants( $test_suite_folder ) {

if ( 'Unit' === $test_suite_folder && ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', IMAGIFY_PLUGIN_ROOT );
define( 'MINUTE_IN_SECONDS', 60 );
}
}
14 changes: 10 additions & 4 deletions classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,17 @@ class User {
* @return void
*/
public function __construct() {
$user = imagify_get_cached_user() ?: get_imagify_user();
$user = get_transient( 'imagify_user_cache' );

if ( is_wp_error( $user ) ) {
$this->error = $user;
return;
if ( ! $user ) {
$user = get_imagify_user();

if ( is_wp_error( $user ) ) {
$this->error = $user;
return;
}

set_transient( 'imagify_user_cache', $user, 5 * MINUTE_IN_SECONDS );
}

$this->id = $user->id;
Expand Down

0 comments on commit 25c8bfa

Please sign in to comment.