Skip to content

Commit 72c49c6

Browse files
committed
Add autoloader for Magento\TestFramework classes
Fixes #31
1 parent a49a5c0 commit 72c49c6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

autoload.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
1414
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
1515
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
16+
use bitExpert\PHPStan\Magento\Autoload\TestFrameworkAutoloader;
1617
use Nette\Neon\Neon;
1718
use PHPStan\Cache\Cache;
1819

@@ -52,10 +53,12 @@
5253
$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));
5354

5455
$mockAutoloader = new MockAutoloader();
56+
$testFrameworkAutoloader = new TestFrameworkAutoloader();
5557
$factoryAutoloader = new FactoryAutoloader($cache);
5658
$proxyAutoloader = new ProxyAutoloader($cache);
5759

5860
\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
61+
\spl_autoload_register([$testFrameworkAutoloader, 'autoload'], true, false);
5962
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
6063
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
6164
})($GLOBALS['argv'] ?? []);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
/**
16+
* Autoloader for Magento\TestFramework classes as those are not loaded by Composer by default which makes PHPStan
17+
* not know about them.
18+
*/
19+
class TestFrameworkAutoloader
20+
{
21+
public function autoload(string $class): void
22+
{
23+
$class = str_replace('\\', '/', $class);
24+
$testsBaseDir = __DIR__ . '/../../../../../../../../dev/tests/static';
25+
26+
$directories = [
27+
// try to find Magento\TestFramework classes...
28+
$testsBaseDir . '/framework/',
29+
$testsBaseDir . '/../integration/framework/',
30+
$testsBaseDir . '/../api-functional/framework/',
31+
// try to find Magento classes...
32+
$testsBaseDir . '/testsuite/',
33+
$testsBaseDir . '/framework/',
34+
$testsBaseDir . '/framework/tests/unit/testsuite/',
35+
];
36+
37+
foreach ($directories as $directory) {
38+
$filename = realpath($directory . $class . '.php');
39+
if (!is_bool($filename) && file_exists($filename) && is_readable($filename)) {
40+
include($filename);
41+
break;
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)