22
33namespace Padosoft \SuperCacheInvalidate \Test \Unit ;
44
5- use PHPUnit \Framework \TestCase ;
65use Illuminate \Support \Facades \DB ;
76use Illuminate \Support \Facades \Cache ;
8- use Padosoft \SuperCacheInvalidate \Console \ProcessCacheInvalidationEventsCommand ;
9- use Padosoft \SuperCacheInvalidate \Helpers \SuperCacheInvalidationHelper ;
107use Carbon \Carbon ;
8+ use Mockery ;
9+ use Padosoft \SuperCacheInvalidate \Helpers \SuperCacheInvalidationHelper ;
1110
1211class ProcessCacheInvalidationEventsTest extends TestCase
1312{
14- protected ProcessCacheInvalidationEventsCommand $ command ;
13+ protected SuperCacheInvalidationHelper $ helper ;
1514
1615 protected function setUp (): void
1716 {
1817 parent ::setUp ();
19- $ helper = new SuperCacheInvalidationHelper ();
20- $ this ->command = new ProcessCacheInvalidationEventsCommand ($ helper );
18+ $ this ->helper = new SuperCacheInvalidationHelper ();
2119 }
2220
2321 public function testProcessEventsWithAssociatedIdentifiersWithinWindow (): void
@@ -26,9 +24,12 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
2624 $ events = collect ([
2725 (object )[
2826 'id ' => 1 ,
29- 'type ' => 'tag ' ,
27+ 'type ' => 'key ' ,
3028 'identifier ' => 'article_ID:7 ' ,
31- 'event_time ' => Carbon::now ()->subSeconds (10 ),
29+ 'reason ' => 'Article 7 removed ' ,
30+ 'connection_name ' => 'default ' ,
31+ 'shard ' => 1 ,
32+ 'priority ' => 1
3233 ],
3334 ]);
3435
@@ -37,44 +38,90 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
3738 'event_id ' => 1 ,
3839 'associated_type ' => 'tag ' ,
3940 'associated_identifier ' => 'plp:sport ' ,
41+ 'connection_name ' => 'default ' ,
4042 ],
4143 ]);
4244
4345 // Mock DB queries
44- DB ::shouldReceive ('table->where->where->where->where->orderBy->limit->get ' )
46+ DB ::shouldReceive ('table->where->where->where->where->where-> orderBy->limit->get ' )
4547 ->andReturn ($ events )
4648 ;
4749
48- DB ::shouldReceive ('table->whereIn->get ' )
50+ DB ::shouldReceive ('table->whereIn->get->groupBy ' )
4951 ->andReturn ($ associations )
5052 ;
5153
5254 // Mock last invalidation times
53- DB ::shouldReceive ('select ' )->andReturn ([
55+ DB ::shouldReceive ('select ' )
56+ ->andReturn ([
5457 (object )[
55- 'identifier_type ' => 'tag ' ,
58+ 'identifier_type ' => 'key ' ,
5659 'identifier ' => 'article_ID:7 ' ,
57- 'last_invalidated ' => Carbon::now ()->subSeconds (40 )->toDateTimeString (),
60+ 'last_invalidated ' => Carbon::now ()->subSeconds (120 )->toDateTimeString (),
5861 ],
5962 (object )[
6063 'identifier_type ' => 'tag ' ,
6164 'identifier ' => 'plp:sport ' ,
62- 'last_invalidated ' => Carbon::now ()->subSeconds (20 )->toDateTimeString (),
65+ 'last_invalidated ' => Carbon::now ()->subSeconds (180 )->toDateTimeString (),
6366 ],
6467 ]);
6568
69+ // Mock update or insert
70+ DB ::shouldReceive ('table->updateOrInsert ' )->twice (); //1 per la chiave e 1 per il tag
71+
72+ DB ::shouldReceive ('beginTransaction ' )->once ();
73+
74+ // CHIAVE
6675 // Mock Cache
67- Cache::shouldReceive ('tags->flush ' )->never ();
76+ Cache::shouldReceive ('store ' )
77+ ->with ('redis-store-1 ' ) // Nome dello store
78+ ->once ()
79+ ->andReturn (Mockery::mock (\Illuminate \Contracts \Cache \Repository::class, function ($ mock ) {
80+ // Mockiamo il comportamento del repository cache
81+ $ mock ->shouldReceive ('forget ' )
82+ ->withArgs (function ($ key ) {
83+ // controllo che la chiave sia proprio quella
84+ $ this ->assertEquals ('article_ID:7 ' , $ key );
85+ return true ; // Indica che l'argomento è accettabile
86+ })
87+ ->once ()
88+ ->andReturn (true );
89+ }));
6890
69- // Mock update or insert
70- DB ::shouldReceive ('table->updateOrInsert ' )->never ();
91+ // TAG
92+ // Mock Cache
93+ Cache::shouldReceive ('store ' )
94+ ->with ('redis-store-1 ' ) // Nome dello store
95+ ->once ()
96+ ->andReturn (Mockery::mock (\Illuminate \Contracts \Cache \Repository::class, function ($ mock ) {
97+ // Mockiamo il comportamento del repository cache
98+ $ mock ->shouldReceive ('tags ' )
99+ ->withArgs (function ($ tags ) {
100+ // controllo che la chiave sia proprio quella
101+ $ this ->assertEquals (['plp:sport ' ], $ tags );
102+ return true ; // Indica che l'argomento è accettabile
103+ })
104+ ->once ()
105+ ->andReturn (Mockery::mock (\Illuminate \Cache \TaggedCache::class, function ($ taggedCacheMock ) {
106+ $ taggedCacheMock ->shouldReceive ('flush ' )
107+ ->once ()
108+ ->andReturn (true );
109+ }));
110+ }));
71111
72112 // Mock event update
73113 DB ::shouldReceive ('table->whereIn->update ' )->once ();
74114
75- // Run the command
76- $ this ->command ->handle ();
115+ DB ::shouldReceive ('commit ' )->once ();
77116
78- // Assertions are handled by Mockery expectations
117+ // Run the command
118+ // Run the command
119+ $ this ->artisan ('supercache:process-invalidation ' , [
120+ '--shard ' => 0 ,
121+ '--priority ' => 0 ,
122+ '--limit ' => 1 ,
123+ '--tag-batch-size ' => 1 ,
124+ '--connection_name ' => 'default ' ,
125+ ]);
79126 }
80127}
0 commit comments