1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #881 - skip tests if necessary test classes are missing

This commit is contained in:
Matthew Brown 2018-07-16 20:52:58 -04:00
parent 39870f97c4
commit 6f96c661ef
2 changed files with 112 additions and 71 deletions

View File

@ -6,6 +6,80 @@ class BinaryOperationTest extends TestCase
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return void
*/
public function testGMPOperations()
{
if (class_exists('GMP') === false) {
$this->markTestSkipped('Cannot run test, base class "GMP" does not exist!');
return;
}
$this->addFile(
'somefile.php',
'<?php
$a = gmp_init(2);
$b = gmp_init(4);
$c = $a + $b;
$d = $c + 3;
echo $d;
$f = $a / $b;
$g = $a ** $b;
$h = $a % $b;
$i = 6 + $b;
$j = 6 - $b;
$k = 6 * $b;
$l = 6 / $b;
$m = 6 ** $b;
$n = 6 % $b;
$o = $a + 6;
$p = $a - 6;
$q = $a * 6;
$r = $a / 6;
$s = $a ** 6;
$t = $a % 6;'
);
$assertions = [
'$a' => 'GMP',
'$b' => 'GMP',
'$c' => 'GMP',
'$d' => 'GMP',
'$f' => 'GMP',
'$g' => 'GMP',
'$h' => 'GMP',
'$i' => 'GMP',
'$j' => 'GMP',
'$k' => 'GMP',
'$l' => 'GMP',
'$m' => 'GMP',
'$n' => 'GMP',
'$o' => 'GMP',
'$p' => 'GMP',
'$q' => 'GMP',
'$r' => 'GMP',
'$s' => 'GMP',
'$t' => 'GMP',
];
$context = new \Psalm\Context();
$this->analyzeFile('somefile.php', $context);
$actual_vars = [];
foreach ($assertions as $var => $_) {
if (isset($context->vars_in_scope[$var])) {
$actual_vars[$var] = (string)$context->vars_in_scope[$var];
}
}
$this->assertSame($assertions, $actual_vars);
}
/**
* @return array
*/
@ -61,52 +135,6 @@ class BinaryOperationTest extends TestCase
'$f' => 'string',
],
],
'gmpOperations' => [
'<?php
$a = gmp_init(2);
$b = gmp_init(4);
$c = $a + $b;
$d = $c + 3;
echo $d;
$f = $a / $b;
$g = $a ** $b;
$h = $a % $b;
$i = 6 + $b;
$j = 6 - $b;
$k = 6 * $b;
$l = 6 / $b;
$m = 6 ** $b;
$n = 6 % $b;
$o = $a + 6;
$p = $a - 6;
$q = $a * 6;
$r = $a / 6;
$s = $a ** 6;
$t = $a % 6;',
'assertions' => [
'$a' => 'GMP',
'$b' => 'GMP',
'$c' => 'GMP',
'$d' => 'GMP',
'$f' => 'GMP',
'$g' => 'GMP',
'$h' => 'GMP',
'$i' => 'GMP',
'$j' => 'GMP',
'$k' => 'GMP',
'$l' => 'GMP',
'$m' => 'GMP',
'$n' => 'GMP',
'$o' => 'GMP',
'$p' => 'GMP',
'$q' => 'GMP',
'$r' => 'GMP',
'$s' => 'GMP',
'$t' => 'GMP',
],
],
'booleanXor' => [
'<?php
$a = true ^ false;

View File

@ -6,6 +6,44 @@ class ClassTest extends TestCase
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return void
*/
public function testExtendsMysqli()
{
if (class_exists('mysqli') === false) {
$this->markTestSkipped('Cannot run test, base class "mysqli" does not exist!');
return;
}
$this->addFile(
'somefile.php',
'<?php
class db extends mysqli {
public function close()
{
return true;
}
public function prepare(string $sql)
{
return false;
}
public function commit(?int $flags = null, ?string $name = null)
{
return true;
}
public function real_escape_string(string $string)
{
return "escaped";
}
}'
);
}
/**
* @return array
*/
@ -153,31 +191,6 @@ class ClassTest extends TestCase
return $maybeBaz;
}',
],
'extendsMysqli' => [
'<?php
class db extends mysqli
{
public function close()
{
return true;
}
public function prepare(string $sql)
{
return false;
}
public function commit(?int $flags = null, ?string $name = null)
{
return true;
}
public function real_escape_string(string $string)
{
return "escaped";
}
}',
],
'assignAnonymousClassToArray' => [
'<?php
/**