1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require_once 'test_stubs.php'; |
4
|
|
|
|
5
|
|
|
class WidgetCacheTest extends TestCase |
|
|
|
|
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); |
|
|
|
|
23
|
|
|
$result2 = render_widget($widget); |
24
|
|
|
$result3 = render_widget($widget); |
|
|
|
|
25
|
|
|
$result4 = render_widget($widget); |
|
|
|
|
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); |
|
|
|
|
45
|
|
|
$result2 = render_widget($widget); |
46
|
|
|
$result3 = render_widget($widget); |
|
|
|
|
47
|
|
|
$result4 = render_widget($widget); |
|
|
|
|
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(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$result1 = render_widget($widget); |
70
|
|
|
$result2 = render_widget($widget2, 'sdfvsf'); |
|
|
|
|
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); |
|
|
|
|
116
|
|
|
$result2 = render_widget($widget); |
117
|
|
|
expire_widgets(['t1']); // causes a re-render |
118
|
|
|
$result3 = render_widget($widget); |
|
|
|
|
119
|
|
|
expire_widgets(['_foo_']); // has no effect |
120
|
|
|
$result4 = render_widget($widget); |
|
|
|
|
121
|
|
|
expire_widgets(['t2']); // causes a re-render |
122
|
|
|
$result5 = render_widget($widget); |
123
|
|
|
$result6 = render_widget($widget); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$this->assertEquals('<p>some text</p>', $result2); |
126
|
|
|
$this->assertEquals('<p>some text</p>', $result5); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.