Completed
Push — master ( d57f7c...e95de4 )
by Iman
02:08
created

WidgetCacheTest::test_cacheKey_method()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once 'test_stubs.php';
4
5
class WidgetCacheTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    public function test_the_view_and_controller_are_rendered_only_once_when_cache_is_enabled()
8
    {
9
        putenv('CACHE_DRIVER=array');
10
        config(['widgetize.debug_info' => false]);
11
        config(['widgetize.enable_cache' => true]);
12
        config(['widgetize.default_cache_lifetime' => 1]);
13
        app()['env'] = 'production';
14
        //assert
15
        View::shouldReceive('exists')->once()->andReturn(true);
16
        View::shouldReceive('make')->once()->with('hello', ['data' => 'foo'], [])->andReturn(app('view'));
17
        View::shouldReceive('render')->once()->andReturn('<p>some text</p>');
18
        \App::shouldReceive('call')->once()->andReturn('foo');
19
20
        //act
21
        $widget = new Widget1();
22
        $result1 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
        $result2 = render_widget($widget);
24
        $result3 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
        $result4 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $result5 = render_widget($widget);
27
28
        $this->assertEquals('<p>some text</p>', $result2);
29
        $this->assertEquals('<p>some text</p>', $result5);
30
    }
31
32
    public function test_caches_the_result_of_controller_method_and_views()
33
    {
34
        putenv('CACHE_DRIVER=array');
35
        config(['widgetize.enable_cache' => true]);
36
        config(['widgetize.default_cache_lifetime' => 1]);
37
        app()['env'] = 'production';
38
        //assert
39
        Cache::shouldReceive('remember')->times(5)->andReturn('<p>some text</p>');
40
        View::shouldReceive('exists')->once()->andReturn(true);
41
42
        //act
43
        $widget = new Widget1();
44
        $result1 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
        $result2 = render_widget($widget);
46
        $result3 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
        $result4 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
        $result5 = render_widget($widget);
49
50
        $this->assertEquals('<p>some text</p>', $result2);
51
        $this->assertEquals('<p>some text</p>', $result5);
52
    }
53
54
    public function test_caches_the_result_forever_when_lifetime_is_negative()
55
    {
56
        putenv('CACHE_DRIVER=array');
57
        config(['widgetize.enable_cache' => true]);
58
        config(['widgetize.default_cache_lifetime' => 1]);
59
        app()['env'] = 'production';
60
        //assert
61
        Cache::shouldReceive('remember')->times(2)->andReturn('<p>some text</p>');
62
        View::shouldReceive('exists')->times(2)->andReturn(true);
63
64
        //act
65
        $widget = new ForeverWidget();
66
        $widget2 = new ForeverWidget2();
67
//        $widget2 = new Widget2();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
69
        $result1 = render_widget($widget);
70
        $result2 = render_widget($widget2, 'sdfvsf');
0 ignored issues
show
Unused Code introduced by
$result2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
        $this->assertEquals('<p>some text</p>', $result1);
73
        $this->assertEquals($widget->cacheLifeTime, 20000);
74
        $this->assertEquals($widget2->cacheLifeTime, 20000);
75
    }
76
77
    public function test_cacheKey_method()
78
    {
79
        putenv('CACHE_DRIVER=array');
80
        config(['widgetize.enable_cache' => true]);
81
        config(['widgetize.default_cache_lifetime' => 1]);
82
        config(['widgetize.debug_info' => false]);
83
84
        app()['env'] = 'production';
85
86
        View::shouldReceive('exists')->once()->andReturn(true);
87
        View::shouldReceive('make')->once()->with('hello', ['data' => 'foo'], [])->andReturn(app('view'));
88
        View::shouldReceive('render')->once()->andReturn('<p>some text</p>');
89
        \App::shouldReceive('call')->once()->andReturn('foo');
90
91
        $widget = new CustomCacheKeyWidget();
92
        render_widget($widget);
93
94
        $this->assertTrue(cache()->has('abcde'));
95
        $this->assertEquals(cache()->get('abcde'), '<p>some text</p>');
96
    }
97
98
    public function test_the_cache_tags()
99
    {
100
        putenv('CACHE_DRIVER=array');
101
        config(['cache.default'=> 'file']);
102
        config(['widgetize.debug_info' => false]);
103
        config(['widgetize.enable_cache' => true]);
104
        config(['widgetize.default_cache_lifetime' => 1]);
105
        app()['env'] = 'production';
106
107
        //assert
108
        View::shouldReceive('exists')->once()->andReturn(true);
109
        View::shouldReceive('make')->times(3)->with('hello', ['data' => 'foo'], [])->andReturn(app('view'));
110
        View::shouldReceive('render')->times(3)->andReturn('<p>some text</p>');
111
        \App::shouldReceive('call')->times(3)->andReturn('foo');
112
113
        //act
114
        $widget = new TaggedWidget();
115
        $result1 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
116
        $result2 = render_widget($widget);
117
        expire_widgets(['t1']); // causes a re-render
118
        $result3 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
        expire_widgets(['_foo_']); // has no effect
120
        $result4 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
        expire_widgets(['t2']); // causes a re-render
122
        $result5 = render_widget($widget);
123
        $result6 = render_widget($widget);
0 ignored issues
show
Unused Code introduced by
$result6 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
125
        $this->assertEquals('<p>some text</p>', $result2);
126
        $this->assertEquals('<p>some text</p>', $result5);
127
    }
128
}
129