Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Test/revert #915

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions Tests/Unit/inc/classes/ImagifyUser/getError.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public function testShouldReturnFalseWhenFetchedUserData() {
'is_monthly' => true,
];

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

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

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

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

Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'imagify_get_cached_user' )->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,9 +23,8 @@ class Test_GetPercentConsumedQuota extends TestCase {
public function testShouldReturnZeroWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'imagify_get_cached_user' )->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 @@ -49,9 +48,8 @@ public function testShouldReturnQuotaWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'imagify_get_cached_user' )->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: 3 additions & 6 deletions Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class Test_IsOverQuota extends TestCase {
public function testShouldReturnFalseWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

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

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

Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'imagify_get_cached_user' )->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 @@ -101,9 +99,8 @@ public function testShouldReturnTrueWhenFreeOverQuota() {
}

private function createMocks( $userData, $dataPreviousQuotaPercent ) {
Functions\when( 'get_transient' )->justReturn( false );
Functions\when( 'imagify_get_cached_user' )->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: 0 additions & 1 deletion Tests/bootstrap-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ function init_constants( $test_suite_folder ) {

if ( 'Unit' === $test_suite_folder && ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', IMAGIFY_PLUGIN_ROOT );
define( 'MINUTE_IN_SECONDS', 60 );
}
}
38 changes: 0 additions & 38 deletions classes/User/ServiceProvider.php

This file was deleted.

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

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 );
if ( is_wp_error( $user ) ) {
$this->error = $user;
return;
}

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