File tree 3 files changed +92
-0
lines changed
3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Tipoff \Forms \Commands ;
6
+
7
+ use Illuminate \Console \Command ;
8
+ use Tipoff \Forms \Models \Contact ;
9
+ use Tipoff \TestSupport \Models \User ;
10
+
11
+ class AttachContactUsers extends Command
12
+ {
13
+ /**
14
+ * The name and signature of the console command.
15
+ *
16
+ * @var string
17
+ */
18
+ protected $ signature = 'attach:users ' ;
19
+
20
+ /**
21
+ * The console command description.
22
+ *
23
+ * @var string
24
+ */
25
+ protected $ description = 'Attach contacts to users with matching emails that have not yet been linked. ' ;
26
+
27
+ /**
28
+ * Create a new command instance.
29
+ *
30
+ * @return void
31
+ */
32
+ public function __construct ()
33
+ {
34
+ parent ::__construct ();
35
+ }
36
+
37
+ /**
38
+ * Execute the console command.
39
+ *
40
+ * @return void
41
+ */
42
+ public function handle ()
43
+ {
44
+ $ contacts = Contact::whereNull ('user_id ' )->get ();
45
+ foreach ($ contacts as $ contact ) {
46
+ $ user = User::where ('email ' , '= ' , $ contact ->email )->first ();
47
+ if ($ user ) {
48
+ $ contact ->user_id = $ user ->id ;
49
+ $ contact ->save ();
50
+ }
51
+ }
52
+ }
53
+ }
Original file line number Diff line number Diff line change 4
4
5
5
namespace Tipoff \Forms ;
6
6
7
+ use Tipoff \Forms \Commands \AttachContactUsers ;
7
8
use Tipoff \Forms \Models \Contact ;
8
9
use Tipoff \Forms \Policies \ContactPolicy ;
9
10
use Tipoff \Support \TipoffPackage ;
@@ -14,6 +15,9 @@ class FormsServiceProvider extends TipoffServiceProvider
14
15
public function configureTipoffPackage (TipoffPackage $ package ): void
15
16
{
16
17
$ package
18
+ ->hasCommands ([
19
+ AttachContactUsers::class,
20
+ ])
17
21
->hasPolicies ([
18
22
Contact::class => ContactPolicy::class,
19
23
])
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Tipoff \Forms \Tests \Unit \Commands ;
6
+
7
+ use Illuminate \Foundation \Testing \DatabaseTransactions ;
8
+ use Tipoff \Forms \Models \Contact ;
9
+ use Tipoff \Forms \Tests \TestCase ;
10
+
11
+ class AttachContactUsersCommandTest extends TestCase
12
+ {
13
+ use DatabaseTransactions;
14
+
15
+ /** @test */
16
+ public function no_users_found ()
17
+ {
18
+ $ this ->artisan ('attach:users ' );
19
+ $ this ->assertDatabaseCount ('contacts ' , 0 );
20
+ }
21
+
22
+ /** @test */
23
+ public function user_found ()
24
+ {
25
+ //Create a user and contact with same email, but not attached
26
+ $ user = randomOrCreate (app ('user ' ));
27
+ Contact::factory ()->create ([
28
+ 'email ' => $ user ->email ,
29
+ ]);
30
+
31
+ $ this ->artisan ('attach:users ' );
32
+ $ contact = Contact::where ('email ' , '= ' , $ user ->email )->first ();
33
+ $ this ->assertNotNull ($ contact ->user );
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments