From 5a395333ce0a960cb1db8e2b16e2e188c445e09c Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Wed, 23 Jun 2021 21:09:05 +0200 Subject: [PATCH] Use CarbonImmutable testNow feature for tests (#842) * Use CarbonImmutable testNow feature for tests * Fix StyleCi * Fix StyleCi --- tests/Actions/AuthorizeShopTest.php | 2 +- tests/Http/Middleware/AuthTokenTest.php | 10 +++++----- tests/Services/ChargeHelperTest.php | 7 +++---- tests/Storage/Commands/ChargeTest.php | 3 +-- tests/TestCase.php | 8 ++++++++ tests/Traits/ApiControllerTest.php | 26 ++++++++++++------------- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/tests/Actions/AuthorizeShopTest.php b/tests/Actions/AuthorizeShopTest.php index ea18d02e..13ddc7a1 100644 --- a/tests/Actions/AuthorizeShopTest.php +++ b/tests/Actions/AuthorizeShopTest.php @@ -83,7 +83,7 @@ public function testWithCodeSoftDeletedShop(): void { // Create the shop $shop = factory($this->model)->create([ - 'deleted_at' => time(), + 'deleted_at' => $this->now->getTimestamp(), ]); // Get the current access token diff --git a/tests/Http/Middleware/AuthTokenTest.php b/tests/Http/Middleware/AuthTokenTest.php index 2555c451..12c35a9d 100644 --- a/tests/Http/Middleware/AuthTokenTest.php +++ b/tests/Http/Middleware/AuthTokenTest.php @@ -208,7 +208,7 @@ public function testDenysForValidRegexValidSignatureBadBody(): void public function testDenysForExpiredToken(): void { - $now = time(); + $now = $this->now->getTimestamp(); $expiredBody = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -260,7 +260,7 @@ public function testDenysForExpiredToken(): void public function testDenysForFutureToken(): void { - $now = time(); + $now = $this->now->getTimestamp(); $expiredBody = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -312,7 +312,7 @@ public function testDenysForFutureToken(): void public function testDenysForInvalidUrl(): void { - $now = time(); + $now = $this->now->getTimestamp(); $expiredBody = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -364,7 +364,7 @@ public function testDenysForInvalidUrl(): void public function testDenysForInvalidApiKey(): void { - $now = time(); + $now = $this->now->getTimestamp(); $expiredBody = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -416,7 +416,7 @@ public function testDenysForInvalidApiKey(): void public function testRuns(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', diff --git a/tests/Services/ChargeHelperTest.php b/tests/Services/ChargeHelperTest.php index b453c385..a57ceb86 100644 --- a/tests/Services/ChargeHelperTest.php +++ b/tests/Services/ChargeHelperTest.php @@ -2,7 +2,6 @@ namespace Osiset\ShopifyApp\Test\Services; -use Illuminate\Support\Carbon; use Osiset\BasicShopifyAPI\ResponseAccess; use Osiset\ShopifyApp\Objects\Enums\ChargeStatus; use Osiset\ShopifyApp\Objects\Transfers\PlanDetails; @@ -59,7 +58,7 @@ public function testTrial(): void // Seed $seed = $this->seedData([ 'trial_days' => 7, - 'trial_ends_on' => Carbon::today()->addDays(7)->format('Y-m-d'), + 'trial_ends_on' => $this->now->today()->addDays(7)->format('Y-m-d'), ]); $this->chargeHelper->useCharge($seed->charge->getReference()); @@ -111,11 +110,11 @@ public function testBeginEndPeriod(): void $this->chargeHelper->useCharge($seed->charge->getReference()); $this->assertSame( - Carbon::today()->format('Y-m-d'), + $this->now->today()->format('Y-m-d'), $this->chargeHelper->periodBeginDate() ); $this->assertSame( - Carbon::today()->addDays(30)->format('Y-m-d'), + $this->now->today()->addDays(30)->format('Y-m-d'), $this->chargeHelper->periodEndDate() ); $this->assertSame(30, $this->chargeHelper->remainingDaysForPeriod()); diff --git a/tests/Storage/Commands/ChargeTest.php b/tests/Storage/Commands/ChargeTest.php index a49ccbd6..87c1e5ab 100644 --- a/tests/Storage/Commands/ChargeTest.php +++ b/tests/Storage/Commands/ChargeTest.php @@ -2,7 +2,6 @@ namespace Osiset\ShopifyApp\Test\Storage\Commands; -use Illuminate\Support\Carbon; use Osiset\ShopifyApp\Contracts\Commands\Charge as IChargeCommand; use Osiset\ShopifyApp\Objects\Enums\ChargeStatus; use Osiset\ShopifyApp\Objects\Enums\ChargeType; @@ -61,7 +60,7 @@ public function testMakeUsage(): void $uc = new UsageChargeTransfer(); $uc->shopId = ShopId::fromNative(1); $uc->chargeReference = ChargeReference::fromNative(12345678); - $uc->billingOn = Carbon::today(); + $uc->billingOn = $this->now->today(); $uc->details = $ud; $this->assertInstanceOf( diff --git a/tests/TestCase.php b/tests/TestCase.php index 47c97103..be2eefef 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Osiset\ShopifyApp\Test; +use Carbon\CarbonImmutable; use Closure; use Illuminate\Contracts\Http\Kernel as HttpKernelContract; use Illuminate\Support\Facades\App; @@ -17,10 +18,17 @@ abstract class TestCase extends OrchestraTestCase { protected $model; + /** + * @var \Carbon\CarbonImmutable + */ + protected $now; + public function setUp(): void { parent::setUp(); + CarbonImmutable::setTestNow($this->now = CarbonImmutable::now()); + // Setup database $this->setupDatabase($this->app); $this->withFactories(__DIR__.'/../src/ShopifyApp/resources/database/factories'); diff --git a/tests/Traits/ApiControllerTest.php b/tests/Traits/ApiControllerTest.php index bd852f36..9ee9fb71 100644 --- a/tests/Traits/ApiControllerTest.php +++ b/tests/Traits/ApiControllerTest.php @@ -44,7 +44,7 @@ public function testApiWithoutTokenJson(): void public function testApiWithToken(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -82,7 +82,7 @@ public function testApiWithToken(): void public function testApiWithTokenJson(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -121,7 +121,7 @@ public function testApiWithTokenJson(): void public function testApiWithExpiredToken(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -159,7 +159,7 @@ public function testApiWithExpiredToken(): void public function testApiWithExpiredTokenJson(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -198,7 +198,7 @@ public function testApiWithExpiredTokenJson(): void public function testApiWithMalformedToken(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -235,7 +235,7 @@ public function testApiWithMalformedToken(): void public function testApiWithMalformedTokenJson(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -273,7 +273,7 @@ public function testApiWithMalformedTokenJson(): void public function testApiWithDomainMismatch(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -311,7 +311,7 @@ public function testApiWithDomainMismatch(): void public function testApiWithDomainMismatchJson(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -350,7 +350,7 @@ public function testApiWithDomainMismatchJson(): void public function testApiWithInvalidTokenHeader(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -388,7 +388,7 @@ public function testApiWithInvalidTokenHeader(): void public function testApiWithInvalidTokenHeaderJson(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -427,7 +427,7 @@ public function testApiWithInvalidTokenHeaderJson(): void public function testApiGetSelf(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -465,7 +465,7 @@ public function testApiGetSelf(): void public function testApiGetPlans(): void { - $now = time(); + $now = $this->now->getTimestamp(); $body = base64url_encode(json_encode([ 'iss' => 'https://shop-name.myshopify.com/admin', @@ -505,7 +505,7 @@ public function testApiGetPlans(): void $this->assertNotEmpty($result); $this->assertNotEmpty($result); - $this->assertEquals(sizeof($result), 1); + $this->assertCount(1, $result); $this->assertStringContainsString('RECURRING', $response->getContent()); } }