Skip to content

Commit

Permalink
PresenterComponentReflection::convertType() do not change $val on err…
Browse files Browse the repository at this point in the history
…or, added tests
  • Loading branch information
dg committed Jul 1, 2015
1 parent 8246ddb commit 4f8f328
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Application/UI/PresenterComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ public static function convertType(& $val, $type)
return FALSE;

} elseif ($type !== 'NULL') {
$old = $val = ($val === FALSE ? '0' : (string) $val);
settype($val, $type);
if ($old !== ($val === FALSE ? '0' : (string) $val)) {
$old = $tmp = ($val === FALSE ? '0' : (string) $val);
settype($tmp, $type);
if ($old !== ($tmp === FALSE ? '0' : (string) $tmp)) {
return FALSE; // data-loss occurs
}
$val = $tmp;
}
return TRUE;
}
Expand Down
108 changes: 108 additions & 0 deletions tests/Application/PresenterComponentReflection.convertType.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* Test: PresenterComponentReflection::convertType()
*/

use Nette\Application\UI\PresenterComponentReflection;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


function testIt($type, $val, $res = NULL)
{
if (func_num_args() === 3) {
Assert::true(PresenterComponentReflection::convertType($val, $type));
} else {
$res = $val;
Assert::false(PresenterComponentReflection::convertType($val, $type));
}
Assert::same($res, $val);
}

$obj = new stdClass;

testIt('string', NULL, NULL);
testIt('string', []);
testIt('string', $obj, $obj);
testIt('string', '', '');
testIt('string', 'a', 'a');
testIt('string', '0', '0');
testIt('string', '1', '1');
testIt('string', '1.0', '1.0');
testIt('string', '1.1', '1.1');
testIt('string', '1a', '1a');
testIt('string', TRUE, '1');
testIt('string', FALSE, '0');
testIt('string', 0, '0');
testIt('string', 1, '1');
testIt('string', 1.0, '1');
testIt('string', 1.2, '1.2');

testIt('int', NULL, NULL);
testIt('int', []);
testIt('int', $obj, $obj);
testIt('int', '');
testIt('int', 'a');
testIt('int', '0', 0);
testIt('int', '1', 1);
testIt('int', '1.0');
testIt('int', '1.1');
testIt('int', '1a');
testIt('int', TRUE, 1);
testIt('int', FALSE, 0);
testIt('int', 0, 0);
testIt('int', 1, 1);
testIt('int', 1.0, 1);
testIt('int', 1.2);

testIt('double', NULL, NULL);
testIt('double', []);
testIt('double', $obj, $obj);
testIt('double', '');
testIt('double', 'a');
testIt('double', '0', 0.0);
testIt('double', '1', 1.0);
testIt('double', '1.0');
testIt('double', '1.1', 1.1);
testIt('double', '1a');
testIt('double', TRUE, 1.0);
testIt('double', FALSE, 0.0);
testIt('double', 0, 0.0);
testIt('double', 1, 1.0);
testIt('double', 1.0, 1.0);
testIt('double', 1.2, 1.2);

testIt('bool', NULL, NULL);
testIt('bool', []);
testIt('bool', $obj, $obj);
testIt('bool', '');
testIt('bool', 'a');
testIt('bool', '1', TRUE);
testIt('bool', '1.0');
testIt('bool', '1.1');
testIt('bool', '1a');
testIt('bool', TRUE, TRUE);
testIt('bool', FALSE, FALSE);
testIt('bool', 0, FALSE);
testIt('bool', 1, TRUE);
testIt('bool', 1.0, TRUE);
testIt('bool', 1.2);

testIt('array', NULL, NULL);
testIt('array', [], []);
testIt('array', $obj, $obj);
testIt('array', '');
testIt('array', 'a');
testIt('array', '1');
testIt('array', '1.0');
testIt('array', '1.1');
testIt('array', '1a');
testIt('array', TRUE);
testIt('array', FALSE);
testIt('array', 0);
testIt('array', 1);
testIt('array', 1.0);
testIt('array', 1.2);

0 comments on commit 4f8f328

Please sign in to comment.