1+ <?php
2+
3+ use Illuminate \Support \Facades \Gate ;
4+ use Redberry \MailboxForLaravel \CaptureService ;
5+
6+ describe ('API Inbox Controller ' , function () {
7+ beforeEach (function () {
8+ config ()->set ('inbox.public ' , true );
9+ config ()->set ('inbox.enabled ' , true );
10+
11+ // Clear any existing messages
12+ $ captureService = $ this ->app ->make (CaptureService::class);
13+ $ captureService ->clearAll ();
14+ });
15+
16+ it ('clears all messages via API ' , function () {
17+ $ captureService = $ this ->app ->make (CaptureService::class);
18+
19+ // Store some test messages
20+ $ captureService ->store ([
21+ 'raw ' => 'test email 1 ' ,
22+ 'subject ' => 'Test Subject 1 ' ,
23+ 'timestamp ' => time (),
24+ ]);
25+
26+ $ captureService ->store ([
27+ 'raw ' => 'test email 2 ' ,
28+ 'subject ' => 'Test Subject 2 ' ,
29+ 'timestamp ' => time () + 1 ,
30+ ]);
31+
32+ // Verify messages exist
33+ $ messages = $ captureService ->all ();
34+ expect (count ($ messages ))->toBe (2 );
35+
36+ $ response = $ this ->postJson ('/mailbox/api/clear ' );
37+
38+ $ response ->assertStatus (200 )
39+ ->assertJson (['message ' => 'Inbox cleared successfully ' ]);
40+
41+ // Verify all messages are cleared
42+ $ messagesAfter = $ captureService ->all ();
43+ expect (count ($ messagesAfter ))->toBe (0 );
44+ });
45+
46+ it ('returns inbox statistics via API ' , function () {
47+ $ captureService = $ this ->app ->make (CaptureService::class);
48+
49+ // Store some test messages - some read, some unread
50+ $ messageId1 = $ captureService ->store ([
51+ 'raw ' => 'test email 1 ' ,
52+ 'subject ' => 'Test Subject 1 ' ,
53+ 'timestamp ' => time (),
54+ ]);
55+
56+ $ messageId2 = $ captureService ->store ([
57+ 'raw ' => 'test email 2 ' ,
58+ 'subject ' => 'Test Subject 2 ' ,
59+ 'timestamp ' => time () + 1 ,
60+ ]);
61+
62+ $ messageId3 = $ captureService ->store ([
63+ 'raw ' => 'test email 3 ' ,
64+ 'subject ' => 'Test Subject 3 ' ,
65+ 'timestamp ' => time () + 2 ,
66+ ]);
67+
68+ // Mark one as seen
69+ $ captureService ->update ($ messageId1 , ['seen_at ' => now ()]);
70+
71+ $ response = $ this ->getJson ('/mailbox/api/stats ' );
72+
73+ $ response ->assertStatus (200 )
74+ ->assertJsonStructure ([
75+ 'total ' ,
76+ 'unread ' ,
77+ 'read '
78+ ]);
79+
80+ $ data = $ response ->json ();
81+ expect ($ data ['total ' ])->toBe (3 )
82+ ->and ($ data ['unread ' ])->toBe (2 )
83+ ->and ($ data ['read ' ])->toBe (1 );
84+ });
85+
86+ it ('returns correct stats when no messages exist ' , function () {
87+ $ response = $ this ->getJson ('/mailbox/api/stats ' );
88+
89+ $ response ->assertStatus (200 );
90+ $ data = $ response ->json ();
91+
92+ expect ($ data ['total ' ])->toBe (0 )
93+ ->and ($ data ['unread ' ])->toBe (0 )
94+ ->and ($ data ['read ' ])->toBe (0 );
95+ });
96+ });
0 commit comments