Skip to content

Commit

Permalink
tests: improved, fixed optional parameters before required parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 25, 2021
1 parent 7c965ce commit 65fcbb1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
42 changes: 42 additions & 0 deletions tests/PhpGenerator/Dumper.dump().errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Test: Nette\PhpGenerator\Dumper::dump() errors
*/

declare(strict_types=1);

use Nette\PhpGenerator\Dumper;
use Tester\Assert;


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


Assert::exception(function () {
$dumper = new Dumper;
$dumper->dump(function () {});
}, Nette\InvalidArgumentException::class, 'Cannot dump closure.');


Assert::exception(function () {
$dumper = new Dumper;
$dumper->dump(new class {
});
}, Nette\InvalidArgumentException::class, 'Cannot dump anonymous class.');


Assert::exception(function () {
$rec = [];
$rec[] = &$rec;
$dumper = new Dumper;
$dumper->dump($rec);
}, Nette\InvalidArgumentException::class, 'Nesting level too deep or recursive dependency.');


Assert::exception(function () {
$rec = new stdClass;
$rec->x = &$rec;
$dumper = new Dumper;
$dumper->dump($rec);
}, Nette\InvalidArgumentException::class, 'Nesting level too deep or recursive dependency.');
52 changes: 10 additions & 42 deletions tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require __DIR__ . '/../bootstrap.php';
ini_set('serialize_precision', '14');

$dumper = new Dumper;

// scalars
Assert::same('0', $dumper->dump(0));
Assert::same('1', $dumper->dump(1));
Assert::same('0.0', $dumper->dump(0.0));
Expand All @@ -38,16 +40,19 @@ Assert::same(
Assert::same('"\rHello \$"', $dumper->dump("\rHello $"));
Assert::same("'He\\llo'", $dumper->dump('He\llo'));
Assert::same('\'He\ll\\\\\o \\\'wor\\\\\\\'ld\\\\\'', $dumper->dump('He\ll\\\o \'wor\\\'ld\\'));
Assert::same('[]', $dumper->dump([]));

// internal classes

// literal
Assert::same('[$s]', $dumper->dump([new PhpLiteral('$s')]));
same('[
function () {
return 1;
},
]', $dumper->dump([(new Nette\PhpGenerator\Closure)->setBody('return 1;')]));


// arrays
Assert::same('[]', $dumper->dump([]));
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
Assert::same("['a']", $dumper->dump(['a']));
Assert::same("[2 => 'a']", $dumper->dump([2 => 'a']));
Expand All @@ -56,19 +61,8 @@ Assert::same("[-2 => 'a', -1 => 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
Assert::same("[-2 => 'a', 0 => 'b']", $dumper->dump([-2 => 'a', 0 => 'b']));
Assert::same("[0 => 'a', -2 => 'b', 1 => 'c']", $dumper->dump(['a', -2 => 'b', 1 => 'c']));

$dumper->wrapLength = 100;
same("[
[
'a',
'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
],
]", $dumper->dump([['a', 'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong']]));

Assert::same(
"['a' => 1, 0 => [\"\\r\" => \"\\r\", 0 => 2], 1 => 3]",
$dumper->dump(['a' => 1, ["\r" => "\r", 2], 3])
);

// stdClass
Assert::same(
"(object) [\n\t'a' => 1,\n\t'b' => 2,\n]",
$dumper->dump((object) ['a' => 1, 'b' => 2])
Expand All @@ -80,6 +74,7 @@ Assert::same(
);


// objects
class Test
{
public $a = 1;
Expand Down Expand Up @@ -140,13 +135,8 @@ class Test3 implements Serializable
Assert::same('unserialize(\'C:5:"Test3":0:{}\')', $dumper->dump(new Test3));
Assert::equal(new Test3, eval('return ' . $dumper->dump(new Test3) . ';'));

Assert::exception(function () {
$dumper = new Dumper;
$dumper->dump(function () {});
}, Nette\InvalidArgumentException::class, 'Cannot dump closure.');



// datetime
class TestDateTime extends DateTime
{
}
Expand All @@ -167,25 +157,3 @@ same(
])",
$dumper->dump(new TestDateTime('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague')))
);

Assert::exception(function () {
$dumper = new Dumper;
$dumper->dump(new class {
});
}, Nette\InvalidArgumentException::class, 'Cannot dump anonymous class.');


Assert::exception(function () {
$rec = [];
$rec[] = &$rec;
$dumper = new Dumper;
$dumper->dump($rec);
}, Nette\InvalidArgumentException::class, 'Nesting level too deep or recursive dependency.');


Assert::exception(function () {
$rec = new stdClass;
$rec->x = &$rec;
$dumper = new Dumper;
$dumper->dump($rec);
}, Nette\InvalidArgumentException::class, 'Nesting level too deep or recursive dependency.');
18 changes: 8 additions & 10 deletions tests/PhpGenerator/expected/ClassType.from.expect
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ class Class2 extends Class1 implements Interface2
* Func3
* @return Class1
*/
private function &func3(
array $a = [],
Class2 $b = null,
Unknown $c,
\Xyz\Unknown $d,
callable $e,
$f = Abc\Unknown::ABC,
$g
) {
private function &func3(array $a, Class2 $b, Unknown $c, \Xyz\Unknown $d, ?callable $e, $f)
{
}


private function func4(array $a = [], Class2 $b = null, $c = Abc\Unknown::ABC)
{
}


Expand All @@ -79,7 +77,7 @@ class Class4

class Class5
{
public function func1(\A $a, ?\B $b, \C $c = null, \D $d = null, \E $e, ?int $i = 1, ?array $arr = [])
public function func1(\A $a, ?\B $b, \C $c = null, \D $d = null, ?int $i = 1, ?array $arr = [])
{
}

Expand Down
9 changes: 7 additions & 2 deletions tests/PhpGenerator/fixtures/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ class Class2 extends Class1 implements Interface2
* Func3
* @return Class1
*/
private function &func3(array $a = [], Class2 $b = null, \Abc\Unknown $c, \Xyz\Unknown $d, callable $e, $f = Unknown::ABC, $g)
private function &func3(array $a, Class2 $b, \Abc\Unknown $c, \Xyz\Unknown $d, ?callable $e, $f)
{
}


private function func4(array $a = [], Class2 $b = null, $c = Unknown::ABC)
{
}

Expand All @@ -84,7 +89,7 @@ class Class4
/** */
class Class5
{
public function func1(\A $a, ?\B $b, ?\C $c = null, \D $d = null, \E $e, ?int $i = 1, ?array $arr = [])
public function func1(\A $a, ?\B $b, ?\C $c = null, \D $d = null, ?int $i = 1, ?array $arr = [])
{
}

Expand Down

0 comments on commit 65fcbb1

Please sign in to comment.