1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/VariadicTest.php
Jon Ursenbach 11bc153deb Rewriting and streamlining every unit test with data providers. (#147)
* Rewriting and streamlining every unit test with data providers.

All unit tests have been rewritten into PHPUnit data providers
to reduce the amount of unnecessary code-reuse through out the
test suite.
2017-04-24 23:45:02 -04:00

99 lines
2.6 KiB
PHP

<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use Psalm\Checker\FileChecker;
use Psalm\Context;
class VariadicTest extends TestCase
{
/**
* @dataProvider providerTestValidVariadic
* @param string $code
* @return void
*/
public function testVariadic($code)
{
$stmts = self::$parser->parse($code);
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidScalarArgument
* @return void
*/
public function testVariadicArrayBadParam()
{
$stmts = self::$parser->parse('<?php
/**
* @param array<int, int> $a_list
* @return void
*/
function f(int ...$a_list) {
}
f(1, 2, "3");
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return array
*/
public function providerTestValidVariadic()
{
return [
'variadic' => [
'<?php
/**
* @return array<mixed>
*/
function f($req, $opt = null, ...$params) {
return $params;
}
f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
f(1, 2, 3, 4, 5);'
],
'variadicArray' => [
'<?php
/**
* @param array<int, int> $a_list
* @return array<int, int>
*/
function f(int ...$a_list) {
return array_map(
/**
* @return int
*/
function (int $a) {
return $a + 1;
},
$a_list
);
}
f(1);
f(1, 2);
f(1, 2, 3);
/**
* @param string ...$a_list
* @return void
*/
function g(string ...$a_list) {
}'
]
];
}
}