-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathCreateUserFromStripeCustomerTest.php
More file actions
118 lines (92 loc) · 3.21 KB
/
Copy pathCreateUserFromStripeCustomerTest.php
File metadata and controls
118 lines (92 loc) · 3.21 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
<?php
namespace Tests\Feature\Jobs;
use App\Jobs\CreateUserFromStripeCustomer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Stripe\Customer;
use Tests\TestCase;
class CreateUserFromStripeCustomerTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_creates_a_user_with_correct_attributes()
{
$customer = Customer::constructFrom([
'id' => 'cus_minimal123',
'name' => 'Test User',
'email' => 'test@example.com',
]);
$job = new CreateUserFromStripeCustomer($customer);
$job->handle();
$user = User::where('email', 'test@example.com')->first();
$this->assertNotNull($user);
$this->assertEquals('Test User', $user->name);
$this->assertEquals('test@example.com', $user->email);
$this->assertEquals('cus_minimal123', $user->stripe_id);
$this->assertNotNull($user->password);
$this->assertTrue(Hash::isHashed($user->password));
}
/** @test */
public function it_fails_when_a_user_with_the_same_stripe_id_already_exists()
{
$existingUser = User::factory()->create([
'stripe_id' => 'cus_existing123',
]);
$customer = Customer::constructFrom([
'id' => 'cus_existing123',
'name' => 'Another User',
'email' => 'another@example.com',
]);
$job = new CreateUserFromStripeCustomer($customer);
$job->handle();
$this->assertDatabaseCount('users', 1);
$this->assertEquals($existingUser->id, User::first()->id);
}
/** @test */
public function it_fails_when_a_user_with_the_same_email_already_exists()
{
$existingUser = User::factory()->create([
'email' => 'existing@example.com',
]);
$customer = Customer::constructFrom([
'id' => 'cus_existing123',
'name' => 'Another User',
'email' => 'existing@example.com',
]);
$job = new CreateUserFromStripeCustomer($customer);
$job->handle();
$this->assertDatabaseCount('users', 1);
$this->assertEquals($existingUser->id, User::first()->id);
}
/** @test */
public function it_handles_a_null_name_in_stripe_customer()
{
$customer = Customer::constructFrom([
'id' => 'cus_noname123',
'name' => null,
'email' => 'noname@example.com',
]);
$job = new CreateUserFromStripeCustomer($customer);
$job->handle();
$this->assertDatabaseHas('users', [
'name' => null,
'email' => 'noname@example.com',
'stripe_id' => 'cus_noname123',
]);
}
/** @test */
public function it_fails_when_customer_has_no_email()
{
$customer = Customer::constructFrom([
'id' => 'cus_noemail123',
'name' => 'No Email',
'email' => '',
]);
$job = new CreateUserFromStripeCustomer($customer);
$this->expectException(ValidationException::class);
$job->handle();
$this->assertDatabaseCount('users', 0);
}
}