-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
IsAllowedFileHelperTest.php
100 lines (84 loc) · 2.53 KB
/
IsAllowedFileHelperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types = 1);
namespace Spaze\PHPStan\Rules\Disallowed;
use Generator;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\File\FileHelper;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Testing\PHPStanTestCase;
use Traits\TestClass;
use Traits\TestTrait;
class IsAllowedFileHelperTest extends PHPStanTestCase
{
/** @var IsAllowedFileHelper */
private $isAllowedHelper;
/** @var IsAllowedFileHelper */
private $isAllowedHelperWithRootDir;
/** @var ScopeFactory */
private $scopeFactory;
/** @var ReflectionProvider */
private $reflectionProvider;
protected function setUp(): void
{
$this->isAllowedHelper = new IsAllowedFileHelper(new FileHelper(__DIR__));
$this->isAllowedHelperWithRootDir = new IsAllowedFileHelper(new FileHelper(__DIR__), '/foo/bar');
$this->reflectionProvider = $this->createReflectionProvider();
$this->scopeFactory = $this->createScopeFactory($this->reflectionProvider, self::getContainer()->getService('typeSpecifier'));
}
/**
* @dataProvider pathProvider
*/
public function testMatches(string $allowedPath, string $file, string $fileWithRootDir): void
{
$context = ScopeContext::create($file);
$this->assertTrue($this->isAllowedHelper->matches($this->scopeFactory->create($context), $allowedPath));
$context = ScopeContext::create($fileWithRootDir);
$this->assertTrue($this->isAllowedHelperWithRootDir->matches($this->scopeFactory->create($context), $allowedPath));
}
public function pathProvider(): Generator
{
yield [
'src',
__DIR__ . '/src',
'/foo/bar/src',
];
yield [
'src/*',
__DIR__ . '/src/waldo.php',
'/foo/bar/src/waldo.php',
];
yield [
'../src/*',
str_replace(basename(__DIR__) . '/../', '', __DIR__ . '/../src/waldo.php'),
'/foo/src/waldo.php',
];
yield [
'src/foo/../*',
__DIR__ . '/src/waldo.php',
'/foo/bar/src/waldo.php',
];
yield [
'*/src',
'*/src',
'*/src',
];
yield [
'*/../src',
'*/../src',
'*/../src',
];
yield [
'\\src\\foo\\bar\\',
__DIR__ . '/src/foo/bar',
'/foo/bar/src/foo/bar',
];
}
public function testMatchesInTraits(): void
{
$classReflection = $this->reflectionProvider->getClass(TestClass::class);
$traitReflection = $this->reflectionProvider->getClass(TestTrait::class);
$context = ScopeContext::create($classReflection->getFileName())->enterClass($classReflection)->enterTrait($traitReflection);
$this->assertTrue($this->isAllowedHelper->matches($this->scopeFactory->create($context), $traitReflection->getFileName()));
}
}