-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new internal functions bool(), int(), float(), string()
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\DI\Compiler: internal functions. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\DI; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
class Service | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
|
||
public function __call($nm, $args) | ||
{ | ||
$this->args[$nm] = $args; | ||
} | ||
} | ||
|
||
|
||
const NUM = 231; | ||
|
||
$compiler = new DI\Compiler; | ||
$compiler->setDynamicParameterNames(['dynamic']); | ||
$container = createContainer($compiler, ' | ||
parameters: | ||
t: true | ||
f: false | ||
fn: ::constant(NUM) | ||
not: not(%f%) | ||
string: string(%f%) | ||
services: | ||
ok: | ||
factory: Service | ||
setup: | ||
- not( not(%f%), not(%t%), not(%fn%), not(%dynamic%), %not% ) | ||
- string( string(%f%), string(%t%), string(%fn%), string(%dynamic%), %string% ) | ||
- bool( bool(%f%), bool(%t%) ) | ||
- int( int(%f%), int(%t%), int(%fn%), int(%dynamic%) ) | ||
- float( float(%f%), float(%t%), float(%fn%), float(%dynamic%) ) | ||
bad1: Service(bool(123)) | ||
bad2: | ||
factory: Service | ||
setup: | ||
- method(bool(123)) | ||
', ['dynamic' => 123]); | ||
|
||
|
||
$obj = $container->getByName('ok'); | ||
|
||
Assert::same( | ||
[ | ||
'not' => [true, false, false, false, true], | ||
'string' => ['0', '1', '231', '123', '0'], | ||
'bool' => [false, true], | ||
'int' => [0, 1, 231, 123], | ||
'float' => [0.0, 1.0, 231.0, 123.0], | ||
], | ||
$obj->args | ||
); | ||
|
||
|
||
Assert::exception(function () use ($container) { | ||
$container->getByName('bad1'); | ||
}, Nette\InvalidStateException::class, "Cannot convert '123' to bool."); | ||
|
||
Assert::exception(function () use ($container) { | ||
$container->getByName('bad2'); | ||
}, Nette\InvalidStateException::class, "Cannot convert '123' to bool."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\DI\Helpers; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
function testIt(string $type, $val, $res = null) | ||
{ | ||
if (func_num_args() === 3) { | ||
Assert::same($res, Helpers::convertType($val, $type)); | ||
} else { | ||
Assert::exception(function () use ($val, $type) { | ||
Helpers::convertType($val, $type); | ||
}, Nette\InvalidStateException::class); | ||
} | ||
} | ||
|
||
|
||
$obj = new stdClass; | ||
|
||
testIt('string', null); | ||
testIt('string', []); | ||
testIt('string', $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); | ||
testIt('int', []); | ||
testIt('int', $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('float', null); | ||
testIt('float', []); | ||
testIt('float', $obj); | ||
testIt('float', ''); | ||
testIt('float', 'a'); | ||
testIt('float', '0', 0.0); | ||
testIt('float', '1', 1.0); | ||
testIt('float', '1.', 1.0); | ||
testIt('float', '1.0', 1.0); | ||
testIt('float', '1.00', 1.0); | ||
testIt('float', '1..0'); | ||
testIt('float', '1.1', 1.1); | ||
testIt('float', '1a'); | ||
testIt('float', true, 1.0); | ||
testIt('float', false, 0.0); | ||
testIt('float', 0, 0.0); | ||
testIt('float', 1, 1.0); | ||
testIt('float', 1.0, 1.0); | ||
testIt('float', 1.2, 1.2); | ||
|
||
testIt('bool', null); | ||
testIt('bool', []); | ||
testIt('bool', $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); |