55use App \Mail \RecoveryPasswordMail ;
66use App \Mail \VerifiedMail ;
77use App \Models \User ;
8+ use App \Notifications \RecoveryPasswordNotification ;
9+ use App \Notifications \VerifiedNotification ;
10+ use Illuminate \Auth \Events \Verified ;
811use Illuminate \Foundation \Testing \DatabaseMigrations ;
12+ use Illuminate \Support \Facades \Event ;
13+ use Illuminate \Support \Facades \Mail ;
14+ use Illuminate \Support \Facades \Notification ;
915use Illuminate \Support \Str ;
1016use Laravel \Sanctum \Sanctum ;
1117use Tests \TestCase ;
@@ -76,17 +82,21 @@ public function test_cambiar_el_password_del_user(): void
7682
7783 public function test_llamar_al_endpoint_restablecer_password (): void
7884 {
85+ Notification::fake ();
7986 $ data = ['email ' => 'user@email.com ' ];
80- Sanctum::actingAs ($ this ->createNewUser ());
87+ $ user = $ this ->createNewUser ();
88+ Sanctum::actingAs ($ user );
8189
8290 $ res = $ this ->postJson (self ::PATH . 'recovery/password ' , $ data );
8391
92+ Notification::assertSentTo ([$ user ], RecoveryPasswordNotification::class, fn ($ notification , $ channels , $ notifiable ) => (in_array ('mail ' , $ channels )));
8493 $ res ->assertStatus (200 );
8594 $ res ->assertExactJson (['message ' => __ ('In a few moments you will receive an email to reset your password. Check your mailbox ' )]);
8695 }
8796
8897 public function test_email_restablecer_password (): void
8998 {
99+ Mail::fake ();
90100 $ data = ['email ' => 'user@email.com ' ];
91101 $ user = $ this ->createNewUser ();
92102
@@ -99,26 +109,33 @@ public function test_email_restablecer_password(): void
99109
100110 public function test_llamar_al_endpoint_verificacion_usuario_ya_verificado (): void
101111 {
102- Sanctum::actingAs ($ this ->createNewUser ());
112+ Notification::fake ();
113+ $ user = $ this ->createNewUser ();
114+ Sanctum::actingAs ($ user );
103115
104116 $ res = $ this ->postJson (self ::PATH_USER . 'verification/email/notification ' );
105117
118+ Notification::assertSentTo ([$ user ], VerifiedNotification::class, fn ($ notification , $ channels , $ notifiable ) => (in_array ('broadcast ' , $ channels ) && $ notification ->isVerified ));
106119 $ res ->assertStatus (200 );
107120 $ res ->assertExactJson (['message ' => __ ('Email already verified ' )]);
108121 }
109122
110123 public function test_llamar_al_endpoint_verificacion_usuario_no_verificado (): void
111124 {
112- Sanctum::actingAs ($ this ->createNewUser (false , false ));
125+ Notification::fake ();
126+ $ user = $ this ->createNewUser (false , false );
127+ Sanctum::actingAs ($ user );
113128
114129 $ res = $ this ->postJson (self ::PATH_USER . 'verification/email/notification ' );
115130
131+ Notification::assertNotSentTo ([$ user ], VerifiedNotification::class);
116132 $ res ->assertStatus (200 );
117133 $ res ->assertExactJson (['message ' => __ ('Email forwarded successfully ' )]);
118134 }
119135
120136 public function test_email_verificar_usuario (): void
121137 {
138+ Mail::fake ();
122139 $ data = ['email ' => 'user@email.com ' ];
123140 $ url = config ('app.DOMAIN_FRONTEND ' ) . "/auth/verification/email " . Str::random (10 ) . "&token= " . Str::random (10 );
124141 $ user = $ this ->createNewUser (false , false );
@@ -133,26 +150,34 @@ public function test_email_verificar_usuario(): void
133150
134151 public function test_verificar_usuario (): void
135152 {
153+ Notification::fake ();
154+ Event::fake ();
136155 $ data = ['email ' => 'user@email.com ' ];
137156 $ user = $ this ->createNewUser (false , false );
138157 Sanctum::actingAs ($ user );
139158
140159 $ res = $ this ->getJson (self ::PATH_USER . 'verification/email/ ' . $ user ->id . '/ ' . sha1 ($ data ['email ' ]));
141160
161+ Notification::assertSentTo ([$ user ], VerifiedNotification::class, fn ($ notification , $ channels , $ notifiable ) => (in_array ('broadcast ' , $ channels ) && $ notification ->isVerified ));
162+ Event::assertDispatched (Verified::class);
142163 $ this ->assertNotNull ($ user ->email_verified_at );
143164 $ res ->assertStatus (200 );
144165 $ res ->assertExactJson (['message ' => __ ('Email has been verified ' )]);
145166 }
146167
147168 public function test_verificar_usuario_ya_verificado (): void
148169 {
170+ Notification::fake ();
171+ Event::fake ();
149172 $ data = ['email ' => 'user@email.com ' ];
150173 $ user = $ this ->createNewUser (false );
151174 $ emailVerifiedAt = $ user ->email_verified_at ;
152175 Sanctum::actingAs ($ user );
153176
154177 $ res = $ this ->getJson (self ::PATH_USER . 'verification/email/ ' . $ user ->id . '/ ' . sha1 ($ data ['email ' ]));
155178
179+ Notification::assertSentTo ([$ user ], VerifiedNotification::class, fn ($ notification , $ channels , $ notifiable ) => (in_array ('broadcast ' , $ channels ) && $ notification ->isVerified ));
180+ Event::assertNotDispatched (Verified::class);
156181 $ this ->assertEquals ($ emailVerifiedAt , $ user ->email_verified_at );
157182 $ res ->assertStatus (200 );
158183 $ res ->assertExactJson (['message ' => __ ('Email already verified ' )]);
0 commit comments