2019-02-26 01:24:44 +01:00
< ? php
2019-03-02 18:49:31 +01:00
namespace TestApp2 ;
2019-02-26 01:24:44 +01:00
require_once __DIR__ . '/vendor/autoload.php' ;
2020-10-11 18:24:12 +02:00
use danog\ClassFinder\ClassFinder ;
2019-02-26 01:24:44 +01:00
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 )
{
2019-02-26 02:03:24 +01:00
2019-02-26 01:24:44 +01:00
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 (
2020-10-11 18:24:12 +02:00
'danog\SandboxApp' ,
2019-02-26 01:24:44 +01:00
array (
2020-10-11 18:24:12 +02:00
'danog\SandboxApp\Foy'
2019-02-26 01:24:44 +01:00
),
'ClassFinder should be able to find 3rd party classes'
),
array (
2020-10-11 18:24:12 +02:00
'danog\SandboxApp\Foo\Bar' ,
2019-02-26 01:24:44 +01:00
array (
2020-10-11 18:24:12 +02:00
'danog\SandboxApp\Foo\Bar\Barc' ,
'danog\SandboxApp\Foo\Bar\Barp'
2019-02-26 01:24:44 +01:00
),
'ClassFinder should be able to find 3rd party classes multiple namespaces deep.'
),
array (
2020-10-11 18:24:12 +02:00
'danog\SandboxAppMulti' ,
2019-02-26 01:24:44 +01:00
array (
2020-10-11 18:24:12 +02:00
'danog\SandboxAppMulti\Zip' ,
'danog\SandboxAppMulti\Zop' ,
'danog\SandboxAppMulti\Zap' ,
'danog\SandboxAppMulti\Zit'
2019-02-26 01:24:44 +01:00
),
2020-10-11 18:24:12 +02:00
'ClassFinder should be able to find 3rd party classes when a provided namespace root maps to multiple directories (Example: "danog\\SandboxAppMulti\\": ["multi/Bop", "multi/Bot"] )'
2019-02-26 01:24:44 +01:00
)
);
}
public function testGetClassesInNamespaceRecursively ()
{
2020-10-11 18:24:12 +02:00
$namespace = 'danog' ;
2019-02-26 01:24:44 +01:00
$expected = array (
2020-10-11 18:24:12 +02:00
'danog\SandboxApp\Foy' ,
'danog\SandboxApp\Fob\Soz' ,
'danog\SandboxApp\Foo\Larc' ,
'danog\SandboxApp\Foo\Bar\Barc' ,
'danog\SandboxApp\Foo\Bar\Barp' ,
'danog\SandboxAppMulti\Zip' ,
'danog\SandboxAppMulti\Zop' ,
'danog\SandboxAppMulti\Zap' ,
'danog\SandboxAppMulti\Zit'
2019-02-26 01:24:44 +01:00
);
$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
2020-10-11 18:24:12 +02:00
// because of the 'danog' test case. Whenever ClassFinder would be updated, we would need to update the
2019-02-26 01:24:44 +01:00
// test. To prevent the flakiness, we just remove ClassFinder's classes.
$classes = array_filter ( $classes , function ( $class ) {
2020-10-11 18:24:12 +02:00
return strpos ( $class , 'danog\ClassFinder' ) !== 0 ;
2019-02-26 01:24:44 +01:00
});
ClassFinder :: enableClassmapSupport ();
$this -> assertEquals ( $expected , $classes , $message );
}
}