|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padosoft\SuperCacheInvalidate\Test\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
use Padosoft\SuperCacheInvalidate\Console\ProcessCacheInvalidationEventsCommand; |
|
9
|
|
|
use Padosoft\SuperCacheInvalidate\Helpers\SuperCacheInvalidationHelper; |
|
10
|
|
|
use Carbon\Carbon; |
|
11
|
|
|
|
|
12
|
|
|
class ProcessCacheInvalidationEventsTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
protected ProcessCacheInvalidationEventsCommand $command; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp(): void |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
$helper = new SuperCacheInvalidationHelper(); |
|
20
|
|
|
$this->command = new ProcessCacheInvalidationEventsCommand($helper); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testProcessEventsWithAssociatedIdentifiersWithinWindow(): void |
|
24
|
|
|
{ |
|
25
|
|
|
// Mock data |
|
26
|
|
|
$events = collect([ |
|
27
|
|
|
(object)[ |
|
28
|
|
|
'id' => 1, |
|
29
|
|
|
'type' => 'tag', |
|
30
|
|
|
'identifier' => 'article_ID:7', |
|
31
|
|
|
'event_time' => Carbon::now()->subSeconds(10), |
|
32
|
|
|
], |
|
33
|
|
|
]); |
|
34
|
|
|
|
|
35
|
|
|
$associations = collect([ |
|
36
|
|
|
(object)[ |
|
37
|
|
|
'event_id' => 1, |
|
38
|
|
|
'associated_type' => 'tag', |
|
39
|
|
|
'associated_identifier' => 'plp:sport', |
|
40
|
|
|
], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
// Mock DB queries |
|
44
|
|
|
DB::shouldReceive('table->where->where->where->where->orderBy->limit->get') |
|
45
|
|
|
->andReturn($events) |
|
46
|
|
|
; |
|
47
|
|
|
|
|
48
|
|
|
DB::shouldReceive('table->whereIn->get') |
|
49
|
|
|
->andReturn($associations) |
|
50
|
|
|
; |
|
51
|
|
|
|
|
52
|
|
|
// Mock last invalidation times |
|
53
|
|
|
DB::shouldReceive('select')->andReturn([ |
|
54
|
|
|
(object)[ |
|
55
|
|
|
'identifier_type' => 'tag', |
|
56
|
|
|
'identifier' => 'article_ID:7', |
|
57
|
|
|
'last_invalidated' => Carbon::now()->subSeconds(40)->toDateTimeString(), |
|
58
|
|
|
], |
|
59
|
|
|
(object)[ |
|
60
|
|
|
'identifier_type' => 'tag', |
|
61
|
|
|
'identifier' => 'plp:sport', |
|
62
|
|
|
'last_invalidated' => Carbon::now()->subSeconds(20)->toDateTimeString(), |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
// Mock Cache |
|
67
|
|
|
Cache::shouldReceive('tags->flush')->never(); |
|
68
|
|
|
|
|
69
|
|
|
// Mock update or insert |
|
70
|
|
|
DB::shouldReceive('table->updateOrInsert')->never(); |
|
71
|
|
|
|
|
72
|
|
|
// Mock event update |
|
73
|
|
|
DB::shouldReceive('table->whereIn->update')->once(); |
|
74
|
|
|
|
|
75
|
|
|
// Run the command |
|
76
|
|
|
$this->command->handle(); |
|
77
|
|
|
|
|
78
|
|
|
// Assertions are handled by Mockery expectations |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|