-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathOrderSuccessTest.php
More file actions
207 lines (169 loc) · 6.32 KB
/
Copy pathOrderSuccessTest.php
File metadata and controls
207 lines (169 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Tests\Feature\Livewire;
use App\Enums\Subscription;
use App\Livewire\OrderSuccess;
use App\Models\License;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Session;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\Test;
use Stripe\Checkout\Session as CheckoutSession;
use Stripe\Collection;
use Stripe\Exception\InvalidRequestException;
use Stripe\LineItem;
use Stripe\StripeClient;
use Tests\TestCase;
class OrderSuccessTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->mockStripeClient();
}
#[Test]
public function it_renders_successfully()
{
$response = $this->withoutVite()->get('/order/cs_test_123');
$response->assertStatus(200);
}
#[Test]
public function it_displays_loading_state_when_no_license_key_is_available()
{
Session::flush();
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'cs_test_123'])
->assertSet('email', 'test@example.com')
->assertSet('licenseKey', null)
->assertSee('License registration in progress')
->assertSee('check your email');
}
#[Test]
public function it_displays_license_key_when_available_in_database()
{
Session::flush();
$user = User::factory()->create([
'email' => 'test@example.com',
]);
License::factory()->create([
'user_id' => $user->id,
'key' => 'db-license-key-12345',
'policy_name' => 'max',
]);
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'cs_test_123'])
->assertSet('email', 'test@example.com')
->assertSet('licenseKey', 'db-license-key-12345')
->assertSee('db-license-key-12345')
->assertSee('test@example.com')
->assertDontSee('License registration in progress');
}
#[Test]
public function it_uses_session_data_when_available()
{
$checkoutSessionId = 'cs_test_123';
Session::put("$checkoutSessionId.email", 'session@example.com');
Session::put("$checkoutSessionId.license_key", 'session-license-key');
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'cs_test_123'])
->assertSet('email', 'session@example.com')
->assertSet('licenseKey', 'session-license-key')
->assertSee('session-license-key')
->assertSee('session@example.com');
}
#[Test]
public function it_polls_for_updates_from_database()
{
Session::flush();
$component = Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'cs_test_123'])
->assertSet('licenseKey', null)
->assertSee('License registration in progress')
->assertSeeHtml('wire:poll.2s="loadData"');
$user = User::factory()->create([
'email' => 'test@example.com',
]);
License::factory()->create([
'user_id' => $user->id,
'key' => 'db-polled-license-key',
'policy_name' => 'max',
]);
$component->call('loadData')
->assertSet('licenseKey', 'db-polled-license-key')
->assertSee('db-polled-license-key')
->assertDontSee('License registration in progress');
}
#[Test]
public function it_redirects_to_mobile_route_when_checkout_session_is_not_found()
{
$mockStripeClient = $this->createMock(StripeClient::class);
$mockStripeClient->checkout = new class {};
$mockStripeClient->checkout->sessions = new class
{
public function retrieve()
{
throw new InvalidRequestException('No such checkout.session');
}
public function allLineItems()
{
throw new InvalidRequestException('No such checkout.session');
}
};
$this->app->bind(StripeClient::class, function ($app, $parameters) use ($mockStripeClient) {
return $mockStripeClient;
});
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'not_a_real_checkout_session'])
->assertRedirect('/mobile');
}
private function mockStripeClient(): void
{
$mockCheckoutSession = CheckoutSession::constructFrom([
'id' => 'cs_test_123',
'customer_details' => [
'email' => 'test@example.com',
],
]);
$mockCheckoutSessionLineItems = Collection::constructFrom([
'object' => 'list',
'data' => [
LineItem::constructFrom([
'id' => 'li_1RFKPpAyFo6rlwXqAHI9wA95',
'object' => 'item',
'description' => 'Early Access Program (Max)',
'price' => [
'id' => Subscription::Max->stripePriceId(),
'object' => 'price',
'product' => 'prod_S9Z5CgycbP7P4y',
],
]),
],
]);
$mockStripeClient = $this->createMock(StripeClient::class);
$mockStripeClient->checkout = new class($mockCheckoutSession)
{
private $mockCheckoutSession;
public function __construct($mockCheckoutSession)
{
$this->mockCheckoutSession = $mockCheckoutSession;
}
};
$mockStripeClient->checkout->sessions = new class($mockCheckoutSession, $mockCheckoutSessionLineItems)
{
private $mockCheckoutSession;
private $mockCheckoutSessionLineItems;
public function __construct($mockCheckoutSession, $mockCheckoutSessionLineItems)
{
$this->mockCheckoutSession = $mockCheckoutSession;
$this->mockCheckoutSessionLineItems = $mockCheckoutSessionLineItems;
}
public function retrieve()
{
return $this->mockCheckoutSession;
}
public function allLineItems()
{
return $this->mockCheckoutSessionLineItems;
}
};
$this->app->bind(StripeClient::class, function ($app, $parameters) use ($mockStripeClient) {
return $mockStripeClient;
});
}
}