1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/ConstantTest.php

109 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class ConstantTest extends TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'constantInFunction' => [
'<?php
useTest();
const TEST = 2;
2018-01-11 21:50:45 +01:00
function useTest(): int {
return TEST;
2017-05-27 02:05:57 +02:00
}',
],
'constantInClosure' => [
'<?php
const TEST = 2;
2018-01-11 21:50:45 +01:00
$useTest = function(): int {
return TEST;
};
2017-05-27 02:05:57 +02:00
$useTest();',
],
'constantDefinedInFunction' => [
'<?php
/**
* @return void
*/
function defineConstant() {
define("CONSTANT", 1);
}
defineConstant();
2017-05-27 02:05:57 +02:00
echo CONSTANT;',
],
'magicConstant' => [
'<?php
$a = __LINE__;
$b = __file__;',
'assertions' => [
'$a' => 'int',
'$b' => 'string',
],
],
2017-12-19 15:48:01 +01:00
'getClassConstantValue' => [
'<?php
class A {
const B = [0, 1, 2];
}
$a = A::B[1];',
],
'staticConstEval' => [
'<?php
abstract class Enum {
/**
* @var string[]
*/
protected const VALUES = [];
public static function export(): string
{
assert(!empty(static::VALUES));
$values = array_map(
function(string $val): string {
return "\'" . $val . "\'";
},
static::VALUES
);
return join(",", $values);
}
}',
'assertions' => [],
'error_levels' => ['MixedArgument'],
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
'constantDefinedInFunctionButNotCalled' => [
'<?php
/**
* @return void
*/
function defineConstant() {
define("CONSTANT", 1);
}
echo CONSTANT;',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedConstant',
],
];
}
}