CacheObserverTest::testClearSpecificModelCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
The property role_ids does not seem to exist on Modules\User\Entities\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
16
        unset($user->unreadNotifications);
0 ignored issues
show
Bug introduced by
The property unreadNotifications does not seem to exist on Modules\User\Entities\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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