aboutsummaryrefslogtreecommitdiff
path: root/vendor/flexi/test/lib/template_test.php
blob: e0f2e0b86b5b06a129abc94394bfcdb06690e419 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php

# Copyright (c)  2008 - Marcus Lunzenauer <mlunzena@uos.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require_once dirname(__FILE__) . '/../flexi_tests.php';
Flexi_Tests::setup();

Mock::generate('Flexi_TemplateFactory');
Mock::generatePartial('Flexi_Template', 'MockTemplate', array('_render'));

class AnEmptyTemplate extends UnitTestCase {


  var $factory;


  function setUp() {
    $this->factory = new MockFlexi_TemplateFactory(TEST_DIR . '/templates/template_tests');
    $template = new MockTemplate();
    $template->__construct('foo', $this->factory);
    $this->factory->setReturnValue('open', $template);
  }


  function tearDown() {
    unset($this->factory);
  }


  function test_should_have_no_attributes() {
    $template = $this->factory->open('');
    $this->assertEqual(0, sizeof($template->get_attributes()));
  }


  function test_should_not_be_empty_after_setting_an_attribute() {
    $template = $this->factory->open('');
    $template->set_attribute('foo', 'bar');
    $this->assertNotEqual(0, sizeof($template->get_attributes()));
  }


  function test_should_be_empty_after_clear() {

    $template = $this->factory->open('foo');

    $this->assertEqual(0, sizeof($template->get_attributes()));

    $template->clear_attributes();
    $this->assertEqual(0, sizeof($template->get_attributes()));
  }
}


class ATemplate extends UnitTestCase {


  var $factory;


  function setUp() {
    $this->factory = new MockFlexi_TemplateFactory(TEST_DIR . '/templates/template_tests');
    $template = new MockTemplate();
    $template->__construct('foo', $this->factory);
    $this->factory->setReturnValue('open', $template);
  }


  function tearDown() {
    unset($this->factory);
  }


  function test_should_return_an_previously_set_attribute() {
    $template = $this->factory->open('foo');
    $template->set_attribute('whom', 'bar');
    $this->assertEqual('bar', $template->get_attribute('whom'));
  }


  function test_should_return_previously_set_attributes() {

    $template = $this->factory->open('foo');
    $template->set_attributes(array('whom' => 'bar', 'foo' => 'baz'));

    $attributes = $template->get_attributes();
    $this->assertIsA($attributes, 'array');
    $this->assertEqual(2, sizeof($attributes));
    $this->assertEqual('bar', $attributes['whom']);
    $this->assertEqual('baz', $attributes['foo']);
  }


  function test_should_merge_attributes_with_set_attributes() {

    $template = $this->factory->open('foo');
    $template->set_attributes(array('a' => 1, 'b' => 2));

    $this->assertEqual(2, sizeof($template->get_attributes()));
    $this->assertEqual(1, $template->get_attribute('a'));
    $this->assertEqual(2, $template->get_attribute('b'));

    $template->set_attributes(array('b' => 8, 'c' => 9));

    $this->assertEqual(3, sizeof($template->get_attributes()));
    $this->assertEqual(1, $template->get_attribute('a'));
    $this->assertEqual(8, $template->get_attribute('b'));
    $this->assertEqual(9, $template->get_attribute('c'));
  }


  function test_should_be_empty_after_clear() {

    $template = $this->factory->open('foo');

    $template->set_attributes(array('a' => 1, 'b' => 2));
    $this->assertNotEqual(0, sizeof($template->get_attributes()));

    $template->clear_attributes();
    $this->assertEqual(0, sizeof($template->get_attributes()));
  }
}



class MagicMethodsTemplate extends UnitTestCase {


  var $factory;


  function setUp() {
    $this->factory = new MockFlexi_TemplateFactory(TEST_DIR . '/templates/template_tests');
    $this->template = new MockTemplate();
    $this->template->__construct('foo', $this->factory);
  }


  function tearDown() {
    unset($this->factory);
    unset($this->template);
  }


  function test_should_set_an_attribute_using_the_magic_methods() {
    $this->template->foo = 'bar';
    $this->assertEqual('bar', $this->template->get_attribute('foo'));
  }


  function test_should_not_set_a_protected_member_field_as_an_attribute() {
    $this->template->_layout = 'bar';
    $this->assertNull($this->template->get_attribute('_layout'));
    $this->assertEqual('bar', $this->template->_layout);
  }

  function test_should_overwrite_an_attribute() {
    $this->template->set_attribute('foo', 'bar');
    $this->template->foo = 'baz';
    $this->assertEqual('baz', $this->template->get_attribute('foo'));
  }

  function test_should_return_an_existing_attribute_using_the_magic_methods() {
    $this->template->set_attribute('foo', 'bar');
    $this->assertEqual('bar', $this->template->foo);
  }


  function test_should_return_null_for_a_non_existing_attribute_using_the_magic_methods() {
    $this->assertNull($this->template->foo);
  }


  function test_should_unset_an_attribute_using_the_magic_methods() {
    $this->template->foo = 'bar';
    unset($this->template->foo);
    $this->assertNull($this->template->foo);
  }


  function test_should_return_null_on_unsetting_a_non_attribute() {
    unset($this->template->foo);
    $this->assertNull($this->template->foo);
  }


  function test_should_return_true_on_isset_for_an_attribute() {
    $this->template->foo = 'bar';
    $this->assertTrue(isset($this->template->foo));
  }


  function test_should_return_false_on_isset_for_a_non_existing_attribute() {
    $this->assertFalse(isset($this->template->foo));
  }
}