6
6
use App \Jobs \CreateAnystackLicenseJob ;
7
7
use App \Models \User ;
8
8
use App \Notifications \LicenseKeyGenerated ;
9
+ use Carbon \CarbonImmutable ;
9
10
use Illuminate \Foundation \Testing \RefreshDatabase ;
10
- use Illuminate \Support \Facades \Cache ;
11
11
use Illuminate \Support \Facades \Http ;
12
12
use Illuminate \Support \Facades \Notification ;
13
+ use Illuminate \Support \Str ;
13
14
use Tests \TestCase ;
14
15
15
16
class CreateAnystackLicenseJobTest extends TestCase
16
17
{
17
18
use RefreshDatabase;
18
19
20
+ protected CarbonImmutable $ now ;
21
+
19
22
protected function setUp (): void
20
23
{
21
24
parent ::setUp ();
22
25
26
+ $ this ->now = now ()->toImmutable ();
27
+
23
28
Http::fake ([
24
29
'https://api.anystack.sh/v1/contacts ' => Http::response ([
25
30
'data ' => [
26
31
'id ' => 'contact-123 ' ,
27
32
28
33
'first_name ' => 'John ' ,
29
34
'last_name ' => 'Doe ' ,
35
+ 'created_at ' => $ this ->now ->toIso8601String (),
36
+ 'updated_at ' => $ this ->now ->toIso8601String (),
30
37
],
31
38
], 201 ),
32
39
@@ -36,6 +43,13 @@ protected function setUp(): void
36
43
'key ' => 'test-license-key-12345 ' ,
37
44
'contact_id ' => 'contact-123 ' ,
38
45
'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 (),
39
53
],
40
54
], 201 ),
41
55
]);
@@ -44,7 +58,7 @@ protected function setUp(): void
44
58
}
45
59
46
60
/** @test */
47
- public function it_creates_contact_and_license_on_anystack ()
61
+ public function it_creates_a_contact_and_license_on_anystack_via_api ()
48
62
{
49
63
$ user = User::factory ()->create ([
50
64
@@ -54,6 +68,7 @@ public function it_creates_contact_and_license_on_anystack()
54
68
$ job = new CreateAnystackLicenseJob (
55
69
$ user ,
56
70
Subscription::Max,
71
+ null ,
57
72
'John ' ,
58
73
'Doe '
59
74
);
@@ -83,7 +98,31 @@ public function it_creates_contact_and_license_on_anystack()
83
98
}
84
99
85
100
/** @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
+
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 ()
87
126
{
88
127
$ user = User::factory ()->create ([
89
128
@@ -93,17 +132,55 @@ public function it_stores_license_key_in_cache()
93
132
$ job = new CreateAnystackLicenseJob (
94
133
$ user ,
95
134
Subscription::Max,
135
+ null ,
96
136
'John ' ,
97
137
'Doe '
98
138
);
99
139
100
140
$ job ->handle ();
101
141
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
+
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
+ ]);
103
180
}
104
181
105
182
/** @test */
106
- public function it_sends_license_key_notification ()
183
+ public function it_sends_a_license_key_notification ()
107
184
{
108
185
$ user = User::factory ()->create ([
109
186
@@ -113,6 +190,7 @@ public function it_sends_license_key_notification()
113
190
$ job = new CreateAnystackLicenseJob (
114
191
$ user ,
115
192
Subscription::Max,
193
+ null ,
116
194
'John ' ,
117
195
'Doe '
118
196
);
0 commit comments