mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
11bc153deb
* 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.
127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
namespace Psalm\Tests;
|
|
|
|
class SwitchTypeTest extends TestCase
|
|
{
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerValidCodeParse()
|
|
{
|
|
return [
|
|
'getClassArg' => [
|
|
'<?php
|
|
class A {
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function fooFoo() {
|
|
|
|
}
|
|
}
|
|
|
|
class B {
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function barBar() {
|
|
|
|
}
|
|
}
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
switch (get_class($a)) {
|
|
case "A":
|
|
$a->fooFoo();
|
|
break;
|
|
|
|
case "B":
|
|
$a->barBar();
|
|
break;
|
|
}'
|
|
],
|
|
'getTypeArg' => [
|
|
'<?php
|
|
function testInt(int $var) : void {
|
|
|
|
}
|
|
|
|
function testString(string $var) : void {
|
|
|
|
}
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
switch (gettype($a)) {
|
|
case "string":
|
|
testString($a);
|
|
break;
|
|
|
|
case "int":
|
|
testInt($a);
|
|
break;
|
|
}'
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
{
|
|
return [
|
|
'getClassArgWrongClass' => [
|
|
'<?php
|
|
class A {
|
|
/** @return void */
|
|
public function fooFoo() {
|
|
|
|
}
|
|
}
|
|
|
|
class B {
|
|
/** @return void */
|
|
public function barBar() {
|
|
|
|
}
|
|
}
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
switch (get_class($a)) {
|
|
case "A":
|
|
$a->barBar();
|
|
break;
|
|
}',
|
|
'error_message' => 'UndefinedMethod'
|
|
],
|
|
'getTypeArgWrongArgs' => [
|
|
'<?php
|
|
function testInt(int $var) : void {
|
|
|
|
}
|
|
|
|
function testString(string $var) : void {
|
|
|
|
}
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
switch (gettype($a)) {
|
|
case "string":
|
|
testInt($a);
|
|
|
|
case "int":
|
|
testString($a);
|
|
}',
|
|
'error_message' => 'InvalidScalarArgument'
|
|
]
|
|
];
|
|
}
|
|
}
|