Skip to content

Commit 4f8f328

Browse files
committed
PresenterComponentReflection::convertType() do not change $val on error, added tests
1 parent 8246ddb commit 4f8f328

2 files changed

Lines changed: 112 additions & 3 deletions

File tree

src/Application/UI/PresenterComponentReflection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ public static function convertType(& $val, $type)
153153
return FALSE;
154154

155155
} elseif ($type !== 'NULL') {
156-
$old = $val = ($val === FALSE ? '0' : (string) $val);
157-
settype($val, $type);
158-
if ($old !== ($val === FALSE ? '0' : (string) $val)) {
156+
$old = $tmp = ($val === FALSE ? '0' : (string) $val);
157+
settype($tmp, $type);
158+
if ($old !== ($tmp === FALSE ? '0' : (string) $tmp)) {
159159
return FALSE; // data-loss occurs
160160
}
161+
$val = $tmp;
161162
}
162163
return TRUE;
163164
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/**
4+
* Test: PresenterComponentReflection::convertType()
5+
*/
6+
7+
use Nette\Application\UI\PresenterComponentReflection;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
function testIt($type, $val, $res = NULL)
15+
{
16+
if (func_num_args() === 3) {
17+
Assert::true(PresenterComponentReflection::convertType($val, $type));
18+
} else {
19+
$res = $val;
20+
Assert::false(PresenterComponentReflection::convertType($val, $type));
21+
}
22+
Assert::same($res, $val);
23+
}
24+
25+
$obj = new stdClass;
26+
27+
testIt('string', NULL, NULL);
28+
testIt('string', []);
29+
testIt('string', $obj, $obj);
30+
testIt('string', '', '');
31+
testIt('string', 'a', 'a');
32+
testIt('string', '0', '0');
33+
testIt('string', '1', '1');
34+
testIt('string', '1.0', '1.0');
35+
testIt('string', '1.1', '1.1');
36+
testIt('string', '1a', '1a');
37+
testIt('string', TRUE, '1');
38+
testIt('string', FALSE, '0');
39+
testIt('string', 0, '0');
40+
testIt('string', 1, '1');
41+
testIt('string', 1.0, '1');
42+
testIt('string', 1.2, '1.2');
43+
44+
testIt('int', NULL, NULL);
45+
testIt('int', []);
46+
testIt('int', $obj, $obj);
47+
testIt('int', '');
48+
testIt('int', 'a');
49+
testIt('int', '0', 0);
50+
testIt('int', '1', 1);
51+
testIt('int', '1.0');
52+
testIt('int', '1.1');
53+
testIt('int', '1a');
54+
testIt('int', TRUE, 1);
55+
testIt('int', FALSE, 0);
56+
testIt('int', 0, 0);
57+
testIt('int', 1, 1);
58+
testIt('int', 1.0, 1);
59+
testIt('int', 1.2);
60+
61+
testIt('double', NULL, NULL);
62+
testIt('double', []);
63+
testIt('double', $obj, $obj);
64+
testIt('double', '');
65+
testIt('double', 'a');
66+
testIt('double', '0', 0.0);
67+
testIt('double', '1', 1.0);
68+
testIt('double', '1.0');
69+
testIt('double', '1.1', 1.1);
70+
testIt('double', '1a');
71+
testIt('double', TRUE, 1.0);
72+
testIt('double', FALSE, 0.0);
73+
testIt('double', 0, 0.0);
74+
testIt('double', 1, 1.0);
75+
testIt('double', 1.0, 1.0);
76+
testIt('double', 1.2, 1.2);
77+
78+
testIt('bool', NULL, NULL);
79+
testIt('bool', []);
80+
testIt('bool', $obj, $obj);
81+
testIt('bool', '');
82+
testIt('bool', 'a');
83+
testIt('bool', '1', TRUE);
84+
testIt('bool', '1.0');
85+
testIt('bool', '1.1');
86+
testIt('bool', '1a');
87+
testIt('bool', TRUE, TRUE);
88+
testIt('bool', FALSE, FALSE);
89+
testIt('bool', 0, FALSE);
90+
testIt('bool', 1, TRUE);
91+
testIt('bool', 1.0, TRUE);
92+
testIt('bool', 1.2);
93+
94+
testIt('array', NULL, NULL);
95+
testIt('array', [], []);
96+
testIt('array', $obj, $obj);
97+
testIt('array', '');
98+
testIt('array', 'a');
99+
testIt('array', '1');
100+
testIt('array', '1.0');
101+
testIt('array', '1.1');
102+
testIt('array', '1a');
103+
testIt('array', TRUE);
104+
testIt('array', FALSE);
105+
testIt('array', 0);
106+
testIt('array', 1);
107+
testIt('array', 1.0);
108+
testIt('array', 1.2);

0 commit comments

Comments
 (0)