2
2
3
3
namespace Padosoft \SuperCacheInvalidate \Test \Unit ;
4
4
5
- use PHPUnit \Framework \TestCase ;
6
5
use Illuminate \Support \Facades \DB ;
7
6
use Illuminate \Support \Facades \Cache ;
8
- use Padosoft \SuperCacheInvalidate \Console \ProcessCacheInvalidationEventsCommand ;
9
- use Padosoft \SuperCacheInvalidate \Helpers \SuperCacheInvalidationHelper ;
10
7
use Carbon \Carbon ;
8
+ use Mockery ;
9
+ use Padosoft \SuperCacheInvalidate \Helpers \SuperCacheInvalidationHelper ;
11
10
12
11
class ProcessCacheInvalidationEventsTest extends TestCase
13
12
{
14
- protected ProcessCacheInvalidationEventsCommand $ command ;
13
+ protected SuperCacheInvalidationHelper $ helper ;
15
14
16
15
protected function setUp (): void
17
16
{
18
17
parent ::setUp ();
19
- $ helper = new SuperCacheInvalidationHelper ();
20
- $ this ->command = new ProcessCacheInvalidationEventsCommand ($ helper );
18
+ $ this ->helper = new SuperCacheInvalidationHelper ();
21
19
}
22
20
23
21
public function testProcessEventsWithAssociatedIdentifiersWithinWindow (): void
@@ -26,9 +24,12 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
26
24
$ events = collect ([
27
25
(object )[
28
26
'id ' => 1 ,
29
- 'type ' => 'tag ' ,
27
+ 'type ' => 'key ' ,
30
28
'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
32
33
],
33
34
]);
34
35
@@ -37,44 +38,90 @@ public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void
37
38
'event_id ' => 1 ,
38
39
'associated_type ' => 'tag ' ,
39
40
'associated_identifier ' => 'plp:sport ' ,
41
+ 'connection_name ' => 'default ' ,
40
42
],
41
43
]);
42
44
43
45
// 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 ' )
45
47
->andReturn ($ events )
46
48
;
47
49
48
- DB ::shouldReceive ('table->whereIn->get ' )
50
+ DB ::shouldReceive ('table->whereIn->get->groupBy ' )
49
51
->andReturn ($ associations )
50
52
;
51
53
52
54
// Mock last invalidation times
53
- DB ::shouldReceive ('select ' )->andReturn ([
55
+ DB ::shouldReceive ('select ' )
56
+ ->andReturn ([
54
57
(object )[
55
- 'identifier_type ' => 'tag ' ,
58
+ 'identifier_type ' => 'key ' ,
56
59
'identifier ' => 'article_ID:7 ' ,
57
- 'last_invalidated ' => Carbon::now ()->subSeconds (40 )->toDateTimeString (),
60
+ 'last_invalidated ' => Carbon::now ()->subSeconds (120 )->toDateTimeString (),
58
61
],
59
62
(object )[
60
63
'identifier_type ' => 'tag ' ,
61
64
'identifier ' => 'plp:sport ' ,
62
- 'last_invalidated ' => Carbon::now ()->subSeconds (20 )->toDateTimeString (),
65
+ 'last_invalidated ' => Carbon::now ()->subSeconds (180 )->toDateTimeString (),
63
66
],
64
67
]);
65
68
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
66
75
// 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
+ }));
68
90
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
+ }));
71
111
72
112
// Mock event update
73
113
DB ::shouldReceive ('table->whereIn->update ' )->once ();
74
114
75
- // Run the command
76
- $ this ->command ->handle ();
115
+ DB ::shouldReceive ('commit ' )->once ();
77
116
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
+ ]);
79
126
}
80
127
}
0 commit comments