2017-01-12 03:37:53 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class MethodMutationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
protected static $parser;
|
|
|
|
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$config = new TestConfig();
|
|
|
|
FileChecker::clearCache();
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function testControllerMutation()
|
|
|
|
{
|
|
|
|
$this->project_checker->registerFile(
|
|
|
|
getcwd() . '/somefile.php',
|
|
|
|
'<?php
|
|
|
|
class User {
|
|
|
|
/** @var string */
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/** @return User|null */
|
|
|
|
public static function loadUser(int $id) {
|
|
|
|
if ($id === 3) {
|
|
|
|
$user = new User();
|
|
|
|
$user->name = "Bob";
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserViewData {
|
|
|
|
/** @var string|null */
|
|
|
|
public $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Response {
|
|
|
|
public function __construct (UserViewData $viewdata) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnauthorizedException extends Exception { }
|
|
|
|
|
|
|
|
class Controller {
|
|
|
|
/** @var UserViewData */
|
|
|
|
public $user_viewdata;
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
public $title;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->user_viewdata = new UserViewData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUser() : void
|
|
|
|
{
|
|
|
|
$user_id = (int)$_GET["id"];
|
|
|
|
|
|
|
|
if (!$user_id) {
|
|
|
|
throw new UnauthorizedException("No user id supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::loadUser($user_id);
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw new UnauthorizedException("User not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->user_viewdata->name = $user->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooController extends Controller {
|
|
|
|
public function barBar() : Response {
|
|
|
|
$this->setUser();
|
|
|
|
|
|
|
|
return new Response($this->user_viewdata);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-12 03:37:53 +01:00
|
|
|
$file_checker->visit($context);
|
|
|
|
$file_checker->analyze(false, true);
|
2017-01-17 00:33:04 +01:00
|
|
|
$method_context = new Context();
|
2017-01-12 06:54:41 +01:00
|
|
|
$this->project_checker->getMethodMutations('FooController::barBar', $method_context);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
$this->assertEquals('UserViewData', (string)$method_context->vars_in_scope['$this->user_viewdata']);
|
|
|
|
$this->assertEquals('string', (string)$method_context->vars_in_scope['$this->user_viewdata->name']);
|
|
|
|
}
|
|
|
|
}
|