1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Foundation\Tests; |
4
|
|
|
|
5
|
|
|
use Foundation\Abstracts\Tests\TestCase; |
6
|
|
|
use Foundation\Cache\ModelCache; |
7
|
|
|
use Modules\Account\Entities\Account; |
8
|
|
|
|
9
|
|
|
class CacheObserverTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testCache() |
12
|
|
|
{ |
13
|
|
|
$user = $this->getActingUser(); |
14
|
|
|
$user->fresh(); |
15
|
|
|
unset($user->role_ids); |
|
|
|
|
16
|
|
|
unset($user->unreadNotifications); |
|
|
|
|
17
|
|
|
$this->assertEquals($user->toArray(), $user::cache()->find($user->id, false)->toArray()); |
18
|
|
|
$this->assertEquals($user->toArray(), $user::cache()->findBy('identity_id', $user->identity_id, false)->toArray()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testCacheSpeed() |
22
|
|
|
{ |
23
|
|
|
$model = Account::create(['testthisshit' => 5]); |
24
|
|
|
\Cache::forever('testmodel', $model); |
25
|
|
|
|
26
|
|
|
$time_db_start = microtime(true); |
27
|
|
|
for ($i = 0; $i < 1000; $i++) { |
28
|
|
|
Account::find($model->id); |
29
|
|
|
} |
30
|
|
|
$time_db_end = microtime(true); |
31
|
|
|
|
32
|
|
|
$dbTime = $time_db_end - $time_db_start; |
33
|
|
|
|
34
|
|
|
$time_cache_start = microtime(true); |
35
|
|
|
|
36
|
|
|
for ($i = 0; $i < 1000; $i++) { |
37
|
|
|
\Cache::get('testmodel'); |
38
|
|
|
} |
39
|
|
|
$time_cache_end = microtime(true); |
40
|
|
|
|
41
|
|
|
$cacheTime = $time_cache_end - $time_cache_start; |
42
|
|
|
|
43
|
|
|
$this->assertGreaterThan($cacheTime, $dbTime); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testClearModelsCache() |
47
|
|
|
{ |
48
|
|
|
$user = $this->getActingUser(); |
49
|
|
|
|
50
|
|
|
$this->assertNotNull($user::cache()->find($user->id)); |
51
|
|
|
ModelCache::clearAll(); |
52
|
|
|
$this->assertNull($user::cache()->find($user->id)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testClearSpecificModelCache() |
56
|
|
|
{ |
57
|
|
|
$user = $this->getActingUser(); |
58
|
|
|
$this->assertNotNull($user::cache()->find($user->id)); |
59
|
|
|
$user::cache()->clearModelCache(); |
60
|
|
|
$this->assertNull($user::cache()->find($user->id)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.