1
0
mirror of https://github.com/danog/class-finder.git synced 2024-11-26 12:04:42 +01:00

Merge branch 'undefined-property-autoload' into 'master'

Undefined property autoload

Closes #8

See merge request hpierce1102/ClassFinder!9
This commit is contained in:
Hayden Pierce 2019-03-02 17:57:41 +00:00
commit cd422f26e1
9 changed files with 1352 additions and 2 deletions

View File

@ -1,4 +1,6 @@
composer install --working-dir=$1/test/app1 --quiet || exit 1
composer install --working-dir=$1/test/app2 --quiet || exit 1
composer install --working-dir=$1 --quiet || exit 1
php --version
php $1/vendor/bin/phpunit --testsuite all
php $1/vendor/bin/phpunit --testsuite all
php $1/vendor/bin/phpunit --testsuite noAutoload

View File

@ -1,4 +1,6 @@
composer install --working-dir=$1/test/app1 --quiet || exit 1
composer install --working-dir=$1/test/app2 --quiet || exit 1
composer install --working-dir=$1 --quiet || exit 1
php --version
php $1/vendor/bin/phpunit --testsuite all
php $1/vendor/bin/phpunit --testsuite all
php $1/vendor/bin/phpunit --testsuite noAutoload

View File

@ -25,5 +25,9 @@
<file>test/app1/src/FilesTest.php</file>
<directory>test/unit/Files</directory>
</testsuite>
<testsuite name="noAutoload">
<file>test/app2/PSR4NoAutoloadTest.php</file>
<file>test/app2/ClassmapNoAutoloadTest.php</file>
</testsuite>
</testsuites>
</phpunit>

View File

@ -45,6 +45,10 @@ class PSR4NamespaceFactory
$composerJsonPath = $appRoot . 'composer.json';
$composerConfig = json_decode(file_get_contents($composerJsonPath));
if (!isset($composerConfig->autoload)) {
return array();
}
//Apparently PHP doesn't like hyphens, so we use variable variables instead.
$psr4 = "psr-4";
return (array)$composerConfig->autoload->$psr4;

View File

@ -0,0 +1,95 @@
<?php
namespace TestApp2;
require_once __DIR__ . '/vendor/autoload.php';
use HaydenPierce\ClassFinder\ClassFinder;
class ClassmapNoAutoloadTest extends \PHPUnit_Framework_TestCase
{
public function setup()
{
// Reset ClassFinder back to normal.
ClassFinder::setAppRoot(null);
}
/**
* @dataProvider classFinderDataProvider
*/
public function testClassFinder($namespace, $expected, $message)
{
try {
$classes = ClassFinder::getClassesInNamespace($namespace);
} catch (Exception $e) {
$this->assertFalse(true, 'An exception occurred: ' . $e->getMessage());
$classes = array();
}
$this->assertEquals($expected, $classes, $message);
}
public function classFinderDataProvider()
{
return array(
array(
'HaydenPierce\Classmap',
array(
'HaydenPierce\Classmap\Classmap2ClassmapINC',
'HaydenPierce\Classmap\Classmap2ClassmapPHP',
'HaydenPierce\Classmap\Classmap3ClassesPHP',
'HaydenPierce\Classmap\ClassmapClassmap2PHP'
),
'Classfinder should be able to load classes based on a classmap from 3rd party packages.'
),
array(
'HaydenPierce\Classmap2',
array(
'HaydenPierce\Classmap2\Classmap2ClassmapINC',
'HaydenPierce\Classmap2\Classmap2ClassmapPHP',
'HaydenPierce\Classmap2\Classmap3ClassesPHP',
'HaydenPierce\Classmap2\ClassmapClassmap2PHP'
),
'Classfinder should be able to handle multiple namespaces in a single file for a classmap.'
)
);
}
/**
* @dataProvider classesInNamespaceRecursivelyDataProvider
*/
public function testClassesInNamespaceRecursively($namespace, $expected, $message)
{
ClassFinder::disablePSR4Support();
try {
$classes = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE);
} catch (Exception $e) {
$this->assertFalse(true, 'An exception occurred: ' . $e->getMessage());
$classes = array();
}
ClassFinder::enablePSR4Support();
$this->assertEquals($expected, $classes, $message);
}
public function classesInNamespaceRecursivelyDataProvider()
{
return array(
array(
'HaydenPierce',
array(
'HaydenPierce\Classmap2\Classmap2ClassmapINC',
'HaydenPierce\Classmap2\Classmap2ClassmapPHP',
'HaydenPierce\Classmap2\Classmap3ClassesPHP',
'HaydenPierce\Classmap2\ClassmapClassmap2PHP',
'HaydenPierce\Classmap\Classmap2ClassmapINC',
'HaydenPierce\Classmap\Classmap2ClassmapPHP',
'HaydenPierce\Classmap\Classmap3ClassesPHP',
'HaydenPierce\Classmap\ClassmapClassmap2PHP',
),
'Classfinder should be able to load third party classes recursively based on a classmap.'
)
);
}
}

View File

@ -0,0 +1,101 @@
<?php
namespace TestApp2;
require_once __DIR__ . '/vendor/autoload.php';
use HaydenPierce\ClassFinder\ClassFinder;
class PSR4NoAutoloadTest extends \PHPUnit_Framework_TestCase
{
public function setup()
{
// Reset ClassFinder back to normal.
ClassFinder::setAppRoot(null);
}
/**
* @dataProvider getClassesInNamespaceDataProvider
*/
public function testGetClassesInNamespace($namespace, $expected, $message)
{
try {
$classes = ClassFinder::getClassesInNamespace($namespace);
} catch (\Exception $e) {
$this->assertFalse(true, 'An exception occurred: ' . $e->getMessage());
$classes = array();
}
$this->assertEquals($expected, $classes, $message);
}
public function getClassesInNamespaceDataProvider()
{
return array(
array(
'HaydenPierce\SandboxApp',
array(
'HaydenPierce\SandboxApp\Foy'
),
'ClassFinder should be able to find 3rd party classes'
),
array(
'HaydenPierce\SandboxApp\Foo\Bar',
array(
'HaydenPierce\SandboxApp\Foo\Bar\Barc',
'HaydenPierce\SandboxApp\Foo\Bar\Barp'
),
'ClassFinder should be able to find 3rd party classes multiple namespaces deep.'
),
array(
'HaydenPierce\SandboxAppMulti',
array(
'HaydenPierce\SandboxAppMulti\Zip',
'HaydenPierce\SandboxAppMulti\Zop',
'HaydenPierce\SandboxAppMulti\Zap',
'HaydenPierce\SandboxAppMulti\Zit'
),
'ClassFinder should be able to find 3rd party classes when a provided namespace root maps to multiple directories (Example: "HaydenPierce\\SandboxAppMulti\\": ["multi/Bop", "multi/Bot"] )'
)
);
}
public function testGetClassesInNamespaceRecursively()
{
$namespace = 'HaydenPierce';
$expected = array(
'HaydenPierce\SandboxApp\Foy',
'HaydenPierce\SandboxApp\Fob\Soz',
'HaydenPierce\SandboxApp\Foo\Larc',
'HaydenPierce\SandboxApp\Foo\Bar\Barc',
'HaydenPierce\SandboxApp\Foo\Bar\Barp',
'HaydenPierce\SandboxAppMulti\Zip',
'HaydenPierce\SandboxAppMulti\Zop',
'HaydenPierce\SandboxAppMulti\Zap',
'HaydenPierce\SandboxAppMulti\Zit'
);
$message = 'ClassFinder should be able to find 3rd party classes';
ClassFinder::disableClassmapSupport();
try {
$classes = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE);
} catch (\Exception $e) {
$this->assertFalse(true, 'An exception occurred: ' . $e->getMessage());
$classes = array();
}
// ClassFinder has the ability to find itself. This ability, while intended, is incontinent for tests
// because of the 'HaydenPierce' test case. Whenever ClassFinder would be updated, we would need to update the
// test. To prevent the flakiness, we just remove ClassFinder's classes.
$classes = array_filter($classes, function($class) {
return strpos($class, 'HaydenPierce\ClassFinder') !== 0;
});
ClassFinder::enableClassmapSupport();
$this->assertEquals($expected, $classes, $message);
}
}

3
test/app2/README.md Normal file
View File

@ -0,0 +1,3 @@
This test app tests that ClassFinder is working in applications that do't specify their own autoload configs in composer.json.
This can occur when ClassFinder is used only to retrieve 3rd party dependencies.

16
test/app2/composer.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "HaydenPierce/testApp",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Hayden Pierce",
"email": "hayden@haydenpierce.com"
}
],
"require-dev": {
"phpunit/phpunit": "4.8.36",
"haydenpierce/class-finder": "0.0.1",
"haydenpierce/sandbox-app":"0.3.0"
}
}

1123
test/app2/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff