1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/IncludeTest.php

130 lines
3.9 KiB
PHP
Raw Normal View History

2017-01-08 01:07:58 +01:00
<?php
namespace Psalm\Tests;
use Psalm\Checker\FileChecker;
class IncludeTest extends TestCase
2017-01-08 01:07:58 +01:00
{
2017-01-13 20:07:23 +01:00
/**
* @dataProvider providerTestValidIncludes
* @param string $file
* @param string $code
* @param array<string,string> $includes
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testBasicRequire($file, $code, $includes = [])
2017-01-08 01:33:33 +01:00
{
foreach ($includes as $filename => $contents) {
$this->project_checker->registerFile($filename, $contents);
}
2017-01-08 01:33:33 +01:00
$file_checker = new FileChecker(
$file,
2017-01-08 01:33:33 +01:00
$this->project_checker,
self::$parser->parse($code)
2017-01-08 01:33:33 +01:00
);
$file_checker->visitAndAnalyzeMethods();
2017-01-08 01:33:33 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return array
2017-01-13 20:07:23 +01:00
*/
public function providerTestValidIncludes()
{
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 01:07:58 +01:00
}