forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheManagerTest.php
137 lines (103 loc) · 3.66 KB
/
CacheManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\Tenant;
class CacheManagerTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
config(['tenancy.bootstrappers' => [
CacheTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
}
/** @test */
public function default_tag_is_automatically_applied()
{
tenancy()->initialize(Tenant::create());
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
}
/** @test */
public function tags_are_merged_when_array_is_passed()
{
tenancy()->initialize(Tenant::create());
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
}
/** @test */
public function tags_are_merged_when_string_is_passed()
{
tenancy()->initialize(Tenant::create());
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
}
/** @test */
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
{
tenancy()->initialize(Tenant::create());
$this->expectException(\Exception::class);
cache()->tags();
}
/** @test */
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
{
tenancy()->initialize(Tenant::create());
$this->expectException(\Exception::class);
cache()->tags(1, 2);
}
/** @test */
public function tags_separate_cache_well_enough()
{
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache()->put('foo', 'bar', 1);
$this->assertSame('bar', cache()->get('foo'));
$tenant2 = Tenant::create();
tenancy()->initialize($tenant2);
$this->assertNotSame('bar', cache()->get('foo'));
cache()->put('foo', 'xyz', 1);
$this->assertSame('xyz', cache()->get('foo'));
}
/** @test */
public function invoking_the_cache_helper_works()
{
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 1);
$this->assertSame('bar', cache('foo'));
$tenant2 = Tenant::create();
tenancy()->initialize($tenant2);
$this->assertNotSame('bar', cache('foo'));
cache(['foo' => 'xyz'], 1);
$this->assertSame('xyz', cache('foo'));
}
/** @test */
public function cache_is_persisted()
{
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->end();
tenancy()->initialize($tenant1);
$this->assertSame('bar', cache('foo'));
}
/** @test */
public function cache_is_persisted_when_reidentification_is_used()
{
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->initialize($tenant2);
tenancy()->end();
tenancy()->initialize($tenant1);
$this->assertSame('bar', cache('foo'));
}
}