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

Add tests for upcoming functionality

This commit is contained in:
Matthew Brown 2016-12-23 23:52:34 +00:00
parent c55870e26c
commit 07c415d588
2 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,133 @@
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
use Psalm\Checker\FileChecker;
use Psalm\Config;
use Psalm\Context;
class BinaryOperationTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
protected static $parser;
public static function setUpBeforeClass()
{
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
}
public function setUp()
{
FileChecker::clearCache();
$config = new TestConfig();
}
public function testRegularAddition()
{
$stmts = self::$parser->parse('<?php
echo 5 + 4;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidOperand
*/
public function testBadAddition()
{
$stmts = self::$parser->parse('<?php
$a = "b" + 5;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testDifferingNumericTypesAdditionInWeakMode()
{
$stmts = self::$parser->parse('<?php
echo 5 + 4.1;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidOperand
*/
public function testDifferingNumericTypesAdditionInStrictMode()
{
Config::getInstance()->strict_binary_operands = true;
$stmts = self::$parser->parse('<?php
echo 5 + 4.1;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testNumericAddition()
{
$stmts = self::$parser->parse('<?php
$a = "5";
if (is_numeric($a)) {
echo $a + 4;
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testConcatenation()
{
$stmts = self::$parser->parse('<?php
echo "Hey " + "Jude,";
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
public function testConcatenationWithNumberInWeakMode()
{
$stmts = self::$parser->parse('<?php
echo "hi" . 5;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidOperand
*/
public function testConcatenationWithNumberInStrictMode()
{
Config::getInstance()->strict_binary_operands = true;
$stmts = self::$parser->parse('<?php
echo "hi" . 5;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
}

View File

@ -182,6 +182,14 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
); );
} }
public function testNumeric()
{
$this->assertEquals(
'string',
(string) TypeChecker::reconcileTypes('numeric', Type::parseString('string'))
);
}
/** /**
* @expectedException \Psalm\Exception\CodeException * @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TypeDoesNotContainType * @expectedExceptionMessage TypeDoesNotContainType
@ -339,4 +347,25 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase
$this->assertEquals('int', (string) $context->vars_in_scope['$b']); $this->assertEquals('int', (string) $context->vars_in_scope['$b']);
$this->assertEquals('string', (string) $context->vars_in_scope['$c']); $this->assertEquals('string', (string) $context->vars_in_scope['$c']);
} }
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage TypeDoesNotContainType
*/
public function testTypeTransformation()
{
$stmts = self::$parser->parse('<?php
$a = "5";
if (is_numeric($a)) {
if (is_int($a)) {
echo $a;
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
} }