Skip to content

Commit

Permalink
Add autoloader for Magento\TestFramework classes
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
shochdoerfer committed May 12, 2020
1 parent a49a5c0 commit 72c49c6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
use bitExpert\PHPStan\Magento\Autoload\TestFrameworkAutoloader;
use Nette\Neon\Neon;
use PHPStan\Cache\Cache;

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

$mockAutoloader = new MockAutoloader();
$testFrameworkAutoloader = new TestFrameworkAutoloader();
$factoryAutoloader = new FactoryAutoloader($cache);
$proxyAutoloader = new ProxyAutoloader($cache);

\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
\spl_autoload_register([$testFrameworkAutoloader, 'autoload'], true, false);
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
})($GLOBALS['argv'] ?? []);
45 changes: 45 additions & 0 deletions src/bitExpert/PHPStan/Magento/Autoload/TestFrameworkAutoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the phpstan-magento package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace bitExpert\PHPStan\Magento\Autoload;

/**
* Autoloader for Magento\TestFramework classes as those are not loaded by Composer by default which makes PHPStan
* not know about them.
*/
class TestFrameworkAutoloader
{
public function autoload(string $class): void
{
$class = str_replace('\\', '/', $class);
$testsBaseDir = __DIR__ . '/../../../../../../../../dev/tests/static';

$directories = [
// try to find Magento\TestFramework classes...
$testsBaseDir . '/framework/',
$testsBaseDir . '/../integration/framework/',
$testsBaseDir . '/../api-functional/framework/',
// try to find Magento classes...
$testsBaseDir . '/testsuite/',
$testsBaseDir . '/framework/',
$testsBaseDir . '/framework/tests/unit/testsuite/',
];

foreach ($directories as $directory) {
$filename = realpath($directory . $class . '.php');
if (!is_bool($filename) && file_exists($filename) && is_readable($filename)) {
include($filename);
break;
}
}
}
}

0 comments on commit 72c49c6

Please sign in to comment.