Passed
Push — master ( 0185b8...e1b903 )
by Arthur
12:06
created

CacheObserverTest::testCacheSpeed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 22
rs 9.8333
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->createUser();
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
        $this->assertEquals($user->toArray(), $user::cache()->find($user->id, false)->toArray());
17
        $this->assertEquals($user->toArray(), $user::cache()->findBy('identity_id', $user->identity_id, false)->toArray());
18
    }
19
20
    public function testCacheSpeed(){
21
        $model = Account::create(["testthisshit"=>5]);
22
        \Cache::put('testmodel',$model);
0 ignored issues
show
Bug introduced by
The call to Illuminate\Support\Facades\Cache::put() has too few arguments starting with minutes. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        \Cache::/** @scrutinizer ignore-call */ 
23
                put('testmodel',$model);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
23
24
        $time_db_start = microtime(true);
25
        for($i=0; $i<1000;$i++){
26
            Account::find($model->id);
27
        }
28
        $time_db_end = microtime(true);
29
30
        $dbTime = $time_db_end - $time_db_start;
31
32
        $time_cache_start = microtime(true);
33
34
        for($i=0; $i<1000;$i++){
35
            \Cache::get('testmodel');
36
        }
37
        $time_cache_end = microtime(true);
38
39
        $cacheTime = $time_cache_end - $time_cache_start;
40
41
        $this->assertGreaterThan($cacheTime, $dbTime);
42
    }
43
44
    public function testClearModelsCache()
45
    {
46
        $user = $this->createUser();
47
48
        $this->assertNotNull($user::cache()->find($user->id));
49
        ModelCache::clearAll();
50
        $this->assertNull($user::cache()->find($user->id));
51
    }
52
53
    public function testClearSpecificModelCache()
54
    {
55
        $user = $this->createUser();
56
        $this->assertNotNull($user::cache()->find($user->id));
57
        $user::cache()->clearModelCache();
58
        $this->assertNull($user::cache()->find($user->id));
59
    }
60
}
61