Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Use CarbonImmutable testNow feature for tests #842

Merged
merged 3 commits into from
Jun 23, 2021
Merged
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
2 changes: 1 addition & 1 deletion tests/Actions/AuthorizeShopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/Http/Middleware/AuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
7 changes: 3 additions & 4 deletions tests/Services/ChargeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions tests/Storage/Commands/ChargeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down
26 changes: 13 additions & 13 deletions tests/Traits/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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());
}
}