Skip to content

Commit 34ee7e6

Browse files
committed
update tests
1 parent 446c98d commit 34ee7e6

File tree

3 files changed

+114
-18
lines changed

3 files changed

+114
-18
lines changed

phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<env name="APP_ENV" value="testing"/>
2222
<env name="BCRYPT_ROUNDS" value="4"/>
2323
<env name="CACHE_DRIVER" value="array"/>
24-
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
25-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
24+
<env name="DB_CONNECTION" value="sqlite"/>
25+
<env name="DB_DATABASE" value=":memory:"/>
2626
<env name="MAIL_MAILER" value="array"/>
2727
<env name="QUEUE_CONNECTION" value="sync"/>
2828
<env name="SESSION_DRIVER" value="array"/>

tests/Feature/Jobs/CreateAnystackLicenseJobTest.php

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,34 @@
66
use App\Jobs\CreateAnystackLicenseJob;
77
use App\Models\User;
88
use App\Notifications\LicenseKeyGenerated;
9+
use Carbon\CarbonImmutable;
910
use Illuminate\Foundation\Testing\RefreshDatabase;
10-
use Illuminate\Support\Facades\Cache;
1111
use Illuminate\Support\Facades\Http;
1212
use Illuminate\Support\Facades\Notification;
13+
use Illuminate\Support\Str;
1314
use Tests\TestCase;
1415

1516
class CreateAnystackLicenseJobTest extends TestCase
1617
{
1718
use RefreshDatabase;
1819

20+
protected CarbonImmutable $now;
21+
1922
protected function setUp(): void
2023
{
2124
parent::setUp();
2225

26+
$this->now = now()->toImmutable();
27+
2328
Http::fake([
2429
'https://api.anystack.sh/v1/contacts' => Http::response([
2530
'data' => [
2631
'id' => 'contact-123',
2732
'email' => '[email protected]',
2833
'first_name' => 'John',
2934
'last_name' => 'Doe',
35+
'created_at' => $this->now->toIso8601String(),
36+
'updated_at' => $this->now->toIso8601String(),
3037
],
3138
], 201),
3239

@@ -36,6 +43,13 @@ protected function setUp(): void
3643
'key' => 'test-license-key-12345',
3744
'contact_id' => 'contact-123',
3845
'policy_id' => 'policy-123',
46+
'name' => null,
47+
'activations' => 0,
48+
'max_activations' => 10,
49+
'suspended' => false,
50+
'expires_at' => $this->now->addYear()->toIso8601String(),
51+
'created_at' => $this->now->toIso8601String(),
52+
'updated_at' => $this->now->toIso8601String(),
3953
],
4054
], 201),
4155
]);
@@ -44,7 +58,7 @@ protected function setUp(): void
4458
}
4559

4660
/** @test */
47-
public function it_creates_contact_and_license_on_anystack()
61+
public function it_creates_a_contact_and_license_on_anystack_via_api()
4862
{
4963
$user = User::factory()->create([
5064
'email' => '[email protected]',
@@ -54,6 +68,7 @@ public function it_creates_contact_and_license_on_anystack()
5468
$job = new CreateAnystackLicenseJob(
5569
$user,
5670
Subscription::Max,
71+
null,
5772
'John',
5873
'Doe'
5974
);
@@ -83,7 +98,31 @@ public function it_creates_contact_and_license_on_anystack()
8398
}
8499

85100
/** @test */
86-
public function it_stores_license_key_in_cache()
101+
public function it_does_not_create_a_contact_when_the_user_already_has_a_contact_id()
102+
{
103+
$user = User::factory()->create([
104+
'email' => '[email protected]',
105+
'name' => 'John Doe',
106+
'anystack_contact_id' => 'contact-123',
107+
]);
108+
109+
$job = new CreateAnystackLicenseJob(
110+
$user,
111+
Subscription::Max,
112+
null,
113+
'John',
114+
'Doe'
115+
);
116+
117+
$job->handle();
118+
119+
Http::assertNotSent(function ($request) {
120+
return Str::contains($request->url(), 'https://api.anystack.sh/v1/contacts');
121+
});
122+
}
123+
124+
/** @test */
125+
public function it_stores_the_license_key_in_database()
87126
{
88127
$user = User::factory()->create([
89128
'email' => '[email protected]',
@@ -93,17 +132,55 @@ public function it_stores_license_key_in_cache()
93132
$job = new CreateAnystackLicenseJob(
94133
$user,
95134
Subscription::Max,
135+
null,
96136
'John',
97137
'Doe'
98138
);
99139

100140
$job->handle();
101141

102-
$this->assertEquals('test-license-key-12345', Cache::get('[email protected]_key'));
142+
$this->assertDatabaseHas('licenses', [
143+
'user_id' => $user->id,
144+
'subscription_item_id' => null,
145+
'policy_name' => 'max',
146+
'key' => 'test-license-key-12345',
147+
'expires_at' => $this->now->addYear(),
148+
'created_at' => $this->now,
149+
'updated_at' => $this->now,
150+
]);
151+
}
152+
153+
/** @test */
154+
public function the_subscription_item_id_is_filled_when_provided()
155+
{
156+
$user = User::factory()->create([
157+
'email' => '[email protected]',
158+
'name' => 'John Doe',
159+
]);
160+
161+
$job = new CreateAnystackLicenseJob(
162+
$user,
163+
Subscription::Max,
164+
123,
165+
'John',
166+
'Doe'
167+
);
168+
169+
$job->handle();
170+
171+
$this->assertDatabaseHas('licenses', [
172+
'user_id' => $user->id,
173+
'subscription_item_id' => 123,
174+
'policy_name' => 'max',
175+
'key' => 'test-license-key-12345',
176+
'expires_at' => $this->now->addYear(),
177+
'created_at' => $this->now,
178+
'updated_at' => $this->now,
179+
]);
103180
}
104181

105182
/** @test */
106-
public function it_sends_license_key_notification()
183+
public function it_sends_a_license_key_notification()
107184
{
108185
$user = User::factory()->create([
109186
'email' => '[email protected]',
@@ -113,6 +190,7 @@ public function it_sends_license_key_notification()
113190
$job = new CreateAnystackLicenseJob(
114191
$user,
115192
Subscription::Max,
193+
null,
116194
'John',
117195
'Doe'
118196
);

tests/Feature/Livewire/OrderSuccessTest.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use App\Enums\Subscription;
66
use App\Livewire\OrderSuccess;
7-
use Illuminate\Support\Facades\Cache;
7+
use App\Models\License;
8+
use App\Models\User;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
810
use Illuminate\Support\Facades\Session;
911
use Livewire\Livewire;
1012
use PHPUnit\Framework\Attributes\Test;
@@ -16,6 +18,8 @@
1618

1719
class OrderSuccessTest extends TestCase
1820
{
21+
use RefreshDatabase;
22+
1923
protected function setUp(): void
2024
{
2125
parent::setUp();
@@ -44,25 +48,31 @@ public function it_displays_loading_state_when_no_license_key_is_available()
4448
}
4549

4650
#[Test]
47-
public function it_displays_license_key_when_available()
51+
public function it_displays_license_key_when_available_in_database()
4852
{
4953
Session::flush();
5054

51-
Cache::put('[email protected]_key', 'test-license-key-12345');
55+
$user = User::factory()->create([
56+
'email' => '[email protected]',
57+
]);
58+
59+
License::factory()->create([
60+
'user_id' => $user->id,
61+
'key' => 'db-license-key-12345',
62+
'policy_name' => 'max',
63+
]);
5264

5365
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'cs_test_123'])
5466
->assertSet('email', '[email protected]')
55-
->assertSet('licenseKey', 'test-license-key-12345')
56-
->assertSee('test-license-key-12345')
67+
->assertSet('licenseKey', 'db-license-key-12345')
68+
->assertSee('db-license-key-12345')
5769
->assertSee('[email protected]')
5870
->assertDontSee('License registration in progress');
5971
}
6072

6173
#[Test]
6274
public function it_uses_session_data_when_available()
6375
{
64-
Cache::flush();
65-
6676
$checkoutSessionId = 'cs_test_123';
6777

6878
Session::put("$checkoutSessionId.email", '[email protected]');
@@ -76,7 +86,7 @@ public function it_uses_session_data_when_available()
7686
}
7787

7888
#[Test]
79-
public function it_polls_for_updates()
89+
public function it_polls_for_updates_from_database()
8090
{
8191
Session::flush();
8292

@@ -85,11 +95,19 @@ public function it_polls_for_updates()
8595
->assertSee('License registration in progress')
8696
->assertSeeHtml('wire:poll.2s="loadData"');
8797

88-
Cache::put('[email protected]_key', 'polled-license-key');
98+
$user = User::factory()->create([
99+
'email' => '[email protected]',
100+
]);
101+
102+
License::factory()->create([
103+
'user_id' => $user->id,
104+
'key' => 'db-polled-license-key',
105+
'policy_name' => 'max',
106+
]);
89107

90108
$component->call('loadData')
91-
->assertSet('licenseKey', 'polled-license-key')
92-
->assertSee('polled-license-key')
109+
->assertSet('licenseKey', 'db-polled-license-key')
110+
->assertSee('db-polled-license-key')
93111
->assertDontSee('License registration in progress');
94112
}
95113

0 commit comments

Comments
 (0)