2017-01-08 01:07:58 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class IncludeTest extends TestCase
|
2017-01-08 01:07:58 +01:00
|
|
|
{
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestValidIncludes
|
|
|
|
* @param string $file
|
|
|
|
* @param string $code
|
|
|
|
* @param array<string,string> $includes
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testBasicRequire($file, $code, $includes = [])
|
2017-01-08 01:33:33 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ($includes as $filename => $contents) {
|
|
|
|
$this->project_checker->registerFile($filename, $contents);
|
|
|
|
}
|
2017-01-08 01:33:33 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$file_checker = new FileChecker(
|
|
|
|
$file,
|
2017-01-08 01:33:33 +01:00
|
|
|
$this->project_checker,
|
2017-04-25 05:45:02 +02:00
|
|
|
self::$parser->parse($code)
|
2017-01-08 01:33:33 +01:00
|
|
|
);
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2017-01-08 01:33:33 +01:00
|
|
|
}
|
2017-01-08 17:24:01 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerTestValidIncludes()
|
2017-01-08 17:24:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'basicRequire' => [
|
|
|
|
'file' => getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
'code' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function foo() : void {
|
|
|
|
(new A);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'includes' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
class A{
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'nestedRequire' => [
|
|
|
|
'file' => getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
|
|
|
|
'code' => '<?php
|
|
|
|
require("file2.php");
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'includes' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
class A{
|
|
|
|
public function fooFoo() : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B extends A{
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'requireNamespace' => [
|
|
|
|
'file' => getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
'code' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function foo() : void {
|
|
|
|
(new Foo\A);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'includes' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A{
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'requireFunction' => [
|
|
|
|
'file' => getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
'code' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
fooFoo();',
|
|
|
|
'includes' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
function fooFoo() : void {
|
|
|
|
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'requireNamespacedWithUse' => [
|
|
|
|
'file' => getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
'code' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
use Foo\A;
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function foo() : void {
|
|
|
|
(new A);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'includes' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A{
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2017-01-08 17:24:01 +01:00
|
|
|
}
|
2017-01-08 01:07:58 +01:00
|
|
|
}
|