-
Notifications
You must be signed in to change notification settings - Fork 1
/
sfCacheTaggingToolkitTest.php
205 lines (169 loc) · 5.2 KB
/
sfCacheTaggingToolkitTest.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
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
<?php
/*
* This file is part of the sfCacheTaggingPlugin package.
* (c) 2009-2012 Ilya Sabelnikov <fruit.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
include_once realpath(dirname(__FILE__) . '/../../bootstrap/functional.php');
include_once sfConfig::get('sf_symfony_lib_dir') . '/vendor/lime/lime.php';
$optionMicrotimePrecision = sfConfig::get('app_sfCacheTagging_microtime_precision');
class ArrayAsIteratorAggregate implements IteratorAggregate
{
protected $tags;
public function __construct($tags)
{
$this->tags = $tags;
}
public function getIterator()
{
return new ArrayIterator($this->tags);
}
}
$cacheManager = sfContext::getInstance()->getViewCacheManager();
$connection = Doctrine::getConnectionByTableName('BlogPost');
$connection->beginTransaction();
$t = new lime_test();
try
{
$p = new BlogPost();
$p->save();
sfCacheTaggingToolkit::formatTags($p);
$t->pass('sfCacheTaggingToolkit::formatTags() works for Doctrine_Record with "Doctrine_Template_Cachetaggable" template');
}
catch (InvalidArgumentException $e)
{
$t->fail($e->getMessage());
}
try
{
$u = new University();
$u->save();
sfCacheTaggingToolkit::formatTags($u);
$t->fail('sfCacheTaggingToolkit::formatTags() does not works for Doctrine_Record without "Doctrine_Template_Cachetaggable" template');
}
catch (InvalidArgumentException $e)
{
$t->pass($e->getMessage());
}
$precisionToTest = array(
array('value' => -1, 'throwException' => true),
array('value' => 0, 'throwException' => false),
array('value' => 3, 'throwException' => false),
array('value' => 6, 'throwException' => false),
array('value' => 7, 'throwException' => true),
);
# if precision approach to 0, unit tests will be failed
# (0 precision is too small for the current test)
foreach ($precisionToTest as $precisionTest)
{
try
{
sfConfig::set('app_sfCacheTagging_microtime_precision', $precisionTest['value']);
sfCacheTaggingToolkit::getPrecision();
if ($precisionTest['throwException'])
{
$t->fail(sprintf(
'Should be thrown an OutOfRangeException value "%d" no in range 0…6',
$precisionTest['value']
));
}
else
{
$t->pass(sprintf(
'Precision value "%d" in range 0…6, no exception was thrown',
$precisionTest['value']
));
}
}
catch (OutOfRangeException $e)
{
if ($precisionTest['throwException'])
{
$t->pass(sprintf(
'OutOfRangeException catched value "%d" is not in range 0…6',
$precisionTest['value']
));
}
else
{
$t->fail(sprintf(
'Precision value "%d" in range 0…6, exception was thrown',
$precisionTest['value']
));
}
}
}
sfConfig::set('app_sfCacheTagging_microtime_precision', $optionMicrotimePrecision);
include_once sfConfig::get('sf_apps_dir') . '/frontend/modules/blog_post/actions/actions.class.php';
try
{
$e = new sfEvent(
new blog_postActions(sfContext::getInstance(), 'blog_post', 'run'),
'component.method_not_found',
array('method' => 'callMe', 'arguments' => array(1, 2, 3))
);
$v = sfCacheTaggingToolkit::listenOnComponentMethodNotFoundEvent($e);
$t->ok(null === $v, 'Return null if method does not exists');
}
catch (BadMethodCallException $e)
{
$t->pass($e->getMessage());
}
# getTaggingCache
try
{
$t->isa_ok(sfCacheTaggingToolkit::getTaggingCache(), 'sfTaggingCache', 'getTaggingCache return correct object');
$t->pass('No expcetions thrown');
}
catch (Exception $e)
{
$t->fail($e->getMessage());
}
$posts = BlogPostTable::getInstance()->findAll();
$post = $posts->getFirst();
$cleanTags = array('Auto_1' => 127872123, 'Auto_2' => 192768211);
$tests = array(
false, $cleanTags, new ArrayIterator($cleanTags), new ArrayAsIteratorAggregate($cleanTags),
new ArrayObject($cleanTags), $posts, $post, Doctrine::getTable('BlogPost'),
);
foreach ($tests as $index => $tags)
{
try
{
$decoratedTags = sfCacheTaggingToolkit::formatTags($tags);
$typeOfTags = gettype($tags);
$t->is(
gettype($decoratedTags),
'array',
sprintf(
'return array if argument is "%s" %s',
$typeOfTags,
is_object($tags) ? '('.get_class($tags) . ')' : ''
)
);
$t->pass(sprintf('Test #%d - Adding tags as %s', $index, $typeOfTags));
}
catch (InvalidArgumentException $e)
{
$t->fail(sprintf('Filed #%d with message: %s', $index, $e->getMessage()));
}
}
$tests = array(null, true, 2, 2.1E-3, new stdClass(), 'FakeString');
foreach ($tests as $tags)
{
try
{
sfCacheTaggingToolkit::formatTags($tags);
$t->fail(sprintf(
'Not thrown exception when passing %s',
is_object($tags) ? '('.get_class($tags) . ')' : gettype($tags)
));
}
catch (InvalidArgumentException $e)
{
$t->pass($e->getMessage());
}
}
$connection->rollback();