1
0
mirror of https://github.com/danog/class-finder.git synced 2025-01-22 22:01:19 +01:00

Ensure that the test has a prank composer.json, like a real application would.

This is the result of discovering that ClassFinder will find it's own composer.json and use that
as the application root. Which is kind of hilarious.
This commit is contained in:
Hayden Pierce 2018-08-04 10:20:34 -05:00
parent 22a510724a
commit 90d821c6e8
2 changed files with 41 additions and 29 deletions

View File

@ -3,7 +3,7 @@
namespace TestApp1;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/haydenpierce/ClassFinder.php';
require_once __DIR__ . '/../vendor/haydenpierce/src/ClassFinder.php';
use HaydenPierce\ClassFinder\ClassFinder;
use \PHPUnit\Framework\TestCase;

View File

@ -26,44 +26,56 @@ function findTestApps($rootDir) {
return $testAppPaths;
}
function buildDirectories($path) {
$pathPieces = explode('/', $path);
// Remove the file, this isn't a directory
array_pop($pathPieces);
// On Windows, the first element could be C:/, representing the first directory. Remove that.
// I suspect someone's going to have an issue with this and report a bug.
// ✨ oh well ✨
$isFirstElementWindowsDrive = strpos($pathPieces[0], ':') !== false;
if ($isFirstElementWindowsDrive) {
array_shift($pathPieces);
// https://stackoverflow.com/a/3349792/3000068
function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
$requiredDirectory = '/';
foreach($pathPieces as $piece) {
$requiredDirectory = $requiredDirectory . $piece;
if(!is_dir($requiredDirectory)) {
mkdir($requiredDirectory);
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
deleteDir($file);
} else {
unlink($file);
}
$requiredDirectory = $requiredDirectory . '/';
}
rmdir($dirPath);
}
// https://stackoverflow.com/a/2050909/3000068
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
function copyInCurrentClasses($testApp) {
$classFinderSource = realpath(__DIR__ . '/../src/ClassFinder.php');
$classFinderPath = $testApp . '/vendor/haydenpierce/ClassFinder.php';
$classFinderSource = realpath(__DIR__ . '/../src');
$classFinderPath = $testApp . '/vendor/haydenpierce/src';
$classFinderPath = str_replace('\\', '/', $classFinderPath);
buildDirectories($classFinderPath);
if (file_exists($classFinderPath)) {
unlink($classFinderPath);
if (is_dir($classFinderPath)) {
deleteDir($classFinderPath);
}
copy($classFinderSource, $classFinderPath);
recurse_copy($classFinderSource, $classFinderPath);
$composerSource = realpath(__DIR__ . '/../composer.json');
$composerPath = $testApp . '/vendor/haydenpierce/composer.json';
copy($composerSource, $composerPath);
}
$testApps = findTestApps(__DIR__);