Skip to content

Commit

Permalink
Fix a bunch of tests to not emit issues on echo
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 9, 2016
1 parent 30159d8 commit 5077424
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 76 deletions.
5 changes: 4 additions & 1 deletion tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function run_function(\Closure $fnc) {
// here we have to make sure $data exists as a side-effect of calling `run_function`
// because it could exist depending on how run_function is implemented
/** @return void */
/**
* @return void
* @psalm-suppress MixedArgument
*/
function fn() {
run_function(
/**
Expand Down
54 changes: 39 additions & 15 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,40 @@ public function testDoWhileVar()
public function testAssignmentInIf()
{
$stmts = self::$parser->parse('<?php
class A {
/** @return bool */
public static function bar() {
return true;
}
if (!($row = (rand(0, 10) ? [5] : null))) {
// do nothing
}
else {
echo $row[0];
}
');

/** @return void */
public function baz() {
if (!($a = A::bar())) {
return;
}
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}

echo $a;
}
public function testAssignInElseIf()
{
$stmts = self::$parser->parse('<?php
if (rand(0, 10) > 5) {
echo "hello";
} elseif ($row = (rand(0, 10) ? [5] : null)) {
echo $row[0];
}
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}

public function testIfNotEqualFalse()
{
$this->markTestIncomplete('This currently fails');
$stmts = self::$parser->parse('<?php
if (($row = rand(0,10) ? [] : false) !== false) {
$row[0] = "good";
echo $row[0];
}
');

Expand All @@ -356,7 +376,7 @@ public function testPassByRefInIf()
{
$stmts = self::$parser->parse('<?php
if (preg_match("/bad/", "badger", $matches)) {
echo $matches[0];
echo (string)$matches[0];
}
');

Expand All @@ -370,7 +390,7 @@ public function testPassByRefInIfCheckAfter()
if (!preg_match("/bad/", "badger", $matches)) {
exit();
}
echo $matches[0];
echo (string)$matches[0];
');

$file_checker = new FileChecker('somefile.php', $stmts);
Expand All @@ -382,7 +402,7 @@ public function testPassByRefInIfWithBoolean()
$stmts = self::$parser->parse('<?php
$a = true;
if ($a && preg_match("/bad/", "badger", $matches)) {
echo $matches[0];
echo (string)$matches[0];
}
');

Expand Down Expand Up @@ -432,6 +452,10 @@ public function testNestedPropertyFetchInElseif()
class A {
/** @var A|null */
public $foo;
public function __toString() : string {
return "boop";
}
}
$a = rand(0, 10) === 5 ? new A() : null;
Expand Down
69 changes: 9 additions & 60 deletions tests/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1315,13 +1315,10 @@ public function testTypeAdjustment()
echo $var;
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();

$return_stmt = array_pop($stmts);

$this->assertSame('int|string', (string) $return_stmt->exprs[0]->inferredType);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
}

public function testTypeMixedAdjustment()
Expand All @@ -1337,13 +1334,10 @@ public function testTypeMixedAdjustment()
echo $var;
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();

$return_stmt = array_pop($stmts);

$this->assertSame('int|string', (string) $return_stmt->exprs[0]->inferredType);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
}

public function testTypeAdjustmentIfNull()
Expand All @@ -1357,16 +1351,11 @@ class B {}
if ($var === null) {
$var = new B;
}
echo $var;
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();

$return_stmt = array_pop($stmts);

$this->assertSame('A|B', (string) $return_stmt->exprs[0]->inferredType);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('A|B', (string) $context->vars_in_scope['$var']);
}

public function testWhileTrue()
Expand Down Expand Up @@ -1670,44 +1659,4 @@ public function bar() {
$context = new Context('somefile.php');
$file_checker->check();
}

public function testAssignInIf()
{
$stmts = self::$parser->parse('<?php
if ($row = (rand(0, 10) ? [5] : null)) {
echo $row[0];
}
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}

public function testAssignInElseIf()
{
$stmts = self::$parser->parse('<?php
if (rand(0, 10) > 5) {
echo "hello";
} elseif ($row = (rand(0, 10) ? [5] : null)) {
echo $row[0];
}
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}

public function testIfNotEqualFalse()
{
$this->markTestIncomplete('This currently fails');
$stmts = self::$parser->parse('<?php
if (($row = rand(0,10) ? [] : false) !== false) {
$row[0] = "good";
echo $row[0];
}
');

$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
}

0 comments on commit 5077424

Please sign in to comment.