/
output.php
154 lines (126 loc) · 4.83 KB
/
output.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
test_case("Output");
test_case_describe("Testing limonade output functions.");
if(!defined('URL_FOR_OUTPUT_TEST'))
define('URL_FOR_OUTPUT_TEST', TESTS_DOC_ROOT.'02-outputs.php');
function before_each_test_in_output()
{
env(null);
option('encoding', 'utf-8');
}
function test_output_layout()
{
}
function test_output_render()
{
$lorem = "Lorem ipsum dolor sit amet.";
$q_lorem = preg_quote($lorem);
# Testing standard rendering with sprint string
assert_equal(render($lorem), $lorem);
assert_equal(render($lorem, null, array('unused')), $lorem);
assert_equal(render("Lorem %s dolor sit amet.", null, array('ipsum')), $lorem);
assert_equal(render("Lorem %s dolor sit amet.", null, array('var1' => 'ipsum')), $lorem);
$response = test_request(URL_FOR_OUTPUT_TEST.'/render0', 'GET');
assert_equal($response, $lorem);
$response = test_request(URL_FOR_OUTPUT_TEST.'/render1', 'GET');
assert_equal($response, $lorem);
# Testing rendering with a view (inline function case)
$view = '_test_output_html_hello_world';
$html = render($view);
assert_match("/Hello World/", $html);
assert_no_match("/$q_lorem/", $html);
$html = render($view, null, array($lorem));
assert_no_match("/$q_lorem/", $html);
$html = render($view, null, array('lorem' => $lorem));
assert_match("/$q_lorem/", $html);
# Testing layout option
$layout = '_test_output_html_my_layout';
$html = render($lorem, $layout);
assert_match("/$q_lorem/", $html);
assert_match("/<title>Page title<\/title>/", $html);
# Testing layout + view (inline function case)
$html = render($view, $layout);
assert_match("/<title>Page title<\/title>/", $html);
assert_match("/Hello World/", $html);
assert_no_match("/$q_lorem/", $html);
$html = render($view, $layout, array('lorem' => $lorem));
assert_match("/<title>Page title<\/title>/", $html);
assert_match("/Hello World/", $html);
assert_match("/$q_lorem/", $html);
# Testing layout + view (template files case)
$views_dir = dirname(__FILE__) . '/apps/views/';
option('views_dir', $views_dir);
$view = 'hello_world.html.php';
$layout = 'layouts/default.html.php';
$html = render($view, $layout);
assert_match("/<title>Page title<\/title>/", $html);
assert_match("/Hello World/", $html);
assert_no_match("/$q_lorem/", $html);
$html = render($view, $layout, array('lorem' => $lorem));
assert_match("/<title>Page title<\/title>/", $html);
assert_match("/Hello World/", $html);
assert_match("/$q_lorem/", $html);
}
function test_output_html()
{
}
function test_output_render_file()
{
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/text', 'GET', true);
assert_header($response, 'Content-type', 'text/plain; charset='.option('encoding'));
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/jpeg', 'GET', true);
assert_header($response, 'Content-type', 'image/jpeg');
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/unknown_page', 'GET', true);
assert_header($response, 'Content-type', 'text/html; charset='.option('encoding'));
}
function test_output_before_filter()
{
function before_render($content_or_func, $layout, $locals, $view_path)
{
if(is_callable($content_or_func))
{
}
elseif(file_exists($view_path) && !array_key_exists('content', $locals))
{
// a view file but not a layout
$view_path = file_path(option('views_dir'), basename($content_or_func, ".html.php") . "_filtered.html.php");
}
else
{
# it's a string
$content_or_func .= "∞FILTERED∞";
}
return array($content_or_func, $layout, $locals, $view_path);
}
$lorem = "Lorem ipsum dolor sit amet.";
$html = render("Lorem %s dolor sit amet.", null, array('ipsum'));
assert_match("/$lorem∞FILTERED∞/", $html);
$views_dir = dirname(__FILE__) . '/apps/views/';
option('views_dir', $views_dir);
$view = 'hello_world.html.php';
$layout = 'layouts/default.html.php';
$html = render($view, $layout, array('lorem' => $lorem));
assert_match("/FILTERED/i", $html);
assert_match("/$lorem/", $html);
}
function test_output_autorender()
{
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/autorender', 'GET');
assert_equal($response, 'AUTORENDERED OUTPUT for empty_controller');
}
end_test_case();
# Views and Layouts
function _test_output_html_my_layout($vars){ extract($vars);?>
<html>
<head>
<title>Page title</title>
</head>
<body>
<?php echo $content ?>
</body>
</html>
<?php }
function _test_output_html_hello_world($vars){ extract($vars);?>
<p>Hello World</p>
<?php if(isset($lorem)):?><p><?=$lorem?></p><?php endif;?>
<?php }