22
33namespace BeyondCode \Mailbox \Tests \Console ;
44
5- use Artisan ;
65use BeyondCode \Mailbox \InboundEmail ;
76use BeyondCode \Mailbox \Tests \TestCase ;
87use Carbon \Carbon ;
@@ -23,16 +22,11 @@ public function setUp(): void
2322 /** @test */
2423 public function it_can_clean_the_statistics ()
2524 {
26- Collection::times (60 )->each (function (int $ index ) {
27- InboundEmail::forceCreate ([
28- 'message ' => Str::random (),
29- 'created_at ' => Carbon::now ()->subDays ($ index )->startOfDay (),
30- ]);
31- });
25+ $ this ->makeMailForDays ();
3226
3327 $ this ->assertCount (60 , InboundEmail::all ());
3428
35- Artisan:: call ('mailbox:clean ' );
29+ $ this -> artisan ('mailbox:clean ' );
3630
3731 $ this ->assertCount (31 , InboundEmail::all ());
3832
@@ -41,24 +35,45 @@ public function it_can_clean_the_statistics()
4135 $ this ->assertCount (0 , InboundEmail::where ('created_at ' , '< ' , $ cutOffDate )->get ());
4236 }
4337
38+ /** @test */
39+ public function it_respects_store_incoming_emails_for_days_config ()
40+ {
41+ $ this ->app ['config ' ]->set ('mailbox.store_incoming_emails_for_days ' , 1 );
42+ $ this ->makeMailForDays (3 );
43+
44+ $ this ->artisan ('mailbox:clean ' );
45+
46+ $ this ->assertCount (1 , InboundEmail::all ());
47+
48+ }
49+
50+
4451 /** @test */
4552 public function it_errors_if_max_age_inf ()
4653 {
4754 $ this ->app ['config ' ]->set ('mailbox.store_incoming_emails_for_days ' , INF );
4855
49- Collection::times (60 )->each (function (int $ index ) {
50- InboundEmail::forceCreate ([
51- 'message ' => Str::random (),
52- 'created_at ' => Carbon::now ()->subDays ($ index )->startOfDay (),
53- ]);
54- });
56+ $ this ->makeMailForDays (3 );
5557
56- $ this ->assertCount (60 , InboundEmail::all ());
58+ $ this ->assertCount (3 , InboundEmail::all ());
5759
5860 $ this ->artisan ('mailbox:clean ' )
5961 ->expectsOutput ('mailbox:clean is disabled because store_incoming_emails_for_days is set to INF. ' )
6062 ->assertExitCode (1 );
6163
62- $ this ->assertCount (60 , InboundEmail::all ());
64+ $ this ->assertCount (3 , InboundEmail::all ());
65+ }
66+
67+ /**
68+ * @return void
69+ */
70+ private function makeMailForDays (int $ days = 60 ): void
71+ {
72+ Collection::times ($ days )->each (function (int $ index ) {
73+ InboundEmail::forceCreate ([
74+ 'message ' => Str::random (),
75+ 'created_at ' => Carbon::now ()->subDays ($ index )->startOfDay (),
76+ ]);
77+ });
6378 }
6479}
0 commit comments