Skip to content

Commit

Permalink
Merge 536b704 into 91424b4
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harris committed Feb 21, 2020
2 parents 91424b4 + 536b704 commit 425342e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
16 changes: 8 additions & 8 deletions src/ClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class ClassFinder
* @throws Exception
* @return array|string[] an array with the name of the defined classes
*/
protected static function getProjectClasses(string $namespace): array
private static function getProjectClasses(string $namespace): array
{
if (in_array($namespace, self::$loadedNamespaces)) {
return get_declared_classes();
Expand All @@ -61,7 +61,7 @@ class_exists($class, true);
* @throws Exception
* @return array|string[] the class map, keyed by Classname values of files
*/
protected static function getClassMap(string $namespace): array
private static function getClassMap(string $namespace): array
{
self::checkState();
if (self::$optimisedClassMap !== false) {
Expand All @@ -84,7 +84,7 @@ protected static function getClassMap(string $namespace): array
* @param string $haystack the input string
* @return bool true if haystack starts with needle, false otherwise
*/
protected static function strStartsWith(string $needle, string $haystack):bool
private static function strStartsWith(string $needle, string $haystack):bool
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
Expand All @@ -94,7 +94,7 @@ protected static function strStartsWith(string $needle, string $haystack):bool
*
* @throws Exception thrown when a combination of components is not available
*/
protected static function checkState() : void
private static function checkState() : void
{
self::initClassMap();
if (false === self::$optimisedClassMap && !class_exists(ClassMapGenerator::class)) {
Expand All @@ -106,7 +106,7 @@ protected static function checkState() : void
/**
* Initializes the optimised class map, if possible.
*/
protected static function initClassMap() :void
private static function initClassMap() :void
{
if (null !== self::$optimisedClassMap) {
return;
Expand All @@ -121,7 +121,7 @@ protected static function initClassMap() :void
*
* @return ClassLoader|null
*/
protected static function getComposerAutoloader(): ?ClassLoader
private static function getComposerAutoloader(): ?ClassLoader
{
$funcs = spl_autoload_functions();
$classLoader = null;
Expand Down Expand Up @@ -168,7 +168,7 @@ public static function getClasses(string $namespace = '', callable $conditional
* @param string $namespace the namespace (without preceding \
* @return array a list of directories containing classes for that namespace
*/
protected static function getProjectSearchDirs(string $namespace): array
private static function getProjectSearchDirs(string $namespace): array
{
$autoloader = self::getComposerAutoloader();
$raw = $autoloader->getPrefixesPsr4();
Expand All @@ -182,7 +182,7 @@ protected static function getProjectSearchDirs(string $namespace): array
* @throws ReflectionException
* @return bool true if in vendor otherwise false
*/
protected static function isClassInVendor(string $className) : bool
private static function isClassInVendor(string $className) : bool
{
$reflection = new ReflectionClass($className);
$filename = $reflection->getFileName();
Expand Down
15 changes: 13 additions & 2 deletions tests/ClassFinderConcrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Composer\Autoload\ClassLoader;
use Cruxinator\ClassFinder\ClassFinder;
use ReflectionClass;

/**
* Class ClassFinderConcrete.
Expand All @@ -29,13 +30,23 @@ public function __construct()
self::$vendorDir = '';
}

public function setOptimisedClassMap($value){
protected static function getMethod($name)
{
$class = new ReflectionClass(self::class);
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}

public function setOptimisedClassMap($value)
{
self::$optimisedClassMap = $value;
}

public function __call($name, $arguments)
{
return call_user_func_array([self::class, $name], $arguments);
$method = self::getMethod($name);
return $method->invokeArgs(null, $arguments);
}

/**
Expand Down
56 changes: 31 additions & 25 deletions tests/Unit/ClassFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Tests\Cruxinator\ClassFinder\Unit;


use Cruxinator\ClassFinder\ClassFinder;
use Tests\Cruxinator\ClassFinder\ClassFinderConcrete;
use Tests\Cruxinator\ClassFinder\TestCase;
Expand All @@ -20,70 +19,77 @@ public function setUp():void
$this->classFinder = new ClassFinderConcrete();
}

public function testSelf(){
$classes = $this->classFinder->getClasses("Cruxinator\\ClassFinder\\");
$this->assertEquals(1,count($classes));
public function testSelf()
{
$classes = $this->classFinder->getClasses('Cruxinator\\ClassFinder\\');
$this->assertEquals(1, count($classes));
$this->assertEquals(ClassFinder::class, $classes[0]);
}

public function testFindPsr(){
$classes = $this->classFinder->getClasses("Psr\\Log\\");
public function testFindPsr()
{
$classes = $this->classFinder->getClasses('Psr\\Log\\');
$this->assertTrue(count($classes) > 0);
foreach($classes as $class){
foreach ($classes as $class) {
$this->assertTrue(class_exists($class));
$this->assertStringStartsWith("Psr\\Log\\",$class);
$this->assertStringStartsWith('Psr\\Log\\', $class);
}
$twoClasses = $this->classFinder->getClasses("Psr\\Log\\");
$twoClasses = $this->classFinder->getClasses('Psr\\Log\\');
$this->assertEquals(count($classes), count($twoClasses));
}

public function testTwoCallsSameFinder(){
public function testTwoCallsSameFinder()
{
$this->testFindPsr();
$this->testSelf();
}

public function testFindPsrNotAbstract(){
$classes = $this->classFinder->getClasses("Psr\\Log\\", function($class){
public function testFindPsrNotAbstract()
{
$classes = $this->classFinder->getClasses('Psr\\Log\\', function ($class) {
$reflectionClass = new \ReflectionClass($class);
return !$reflectionClass->isAbstract();
});
$this->assertTrue(count($classes) > 0);
foreach($classes as $class){
foreach ($classes as $class) {
$this->assertTrue(class_exists($class));
$this->assertStringStartsWith("Psr\\Log\\",$class);
$this->assertStringStartsWith('Psr\\Log\\', $class);
$reflectionClass = new \ReflectionClass($class);
$this->assertFalse($reflectionClass->isAbstract());
}
}

public function testFindPsrOnlyAbstract(){
$classes = $this->classFinder->getClasses("Psr\\Log\\", function($class){
public function testFindPsrOnlyAbstract()
{
$classes = $this->classFinder->getClasses('Psr\\Log\\', function ($class) {
$reflectionClass = new \ReflectionClass($class);
return $reflectionClass->isAbstract();
});
$this->assertTrue(count($classes) > 0);
foreach($classes as $class){
foreach ($classes as $class) {
$this->assertTrue(class_exists($class));
$this->assertStringStartsWith("Psr\\Log\\",$class);
$this->assertStringStartsWith('Psr\\Log\\', $class);
$reflectionClass = new \ReflectionClass($class);
$this->assertTrue($reflectionClass->isAbstract());
}
}

public function testFindPsrNotInVender(){
$classes = $this->classFinder->getClasses("Psr\\Log\\",null,false);
public function testFindPsrNotInVender()
{
$classes = $this->classFinder->getClasses('Psr\\Log\\', null, false);
$this->assertFalse(count($classes) > 0);
}

/**
* @runInSeparateProcess
*/
public function testErrorCheck(){
public function testErrorCheck()
{
$this->classFinder->setOptimisedClassMap(false);
$autoloader = $this->classFinder->getComposerAutoloader();
$classMap = $autoloader->getClassMap();
if(array_key_exists(__CLASS__,$classMap)){
$this->markTestSkipped("Error only works with non optimized autoloader");
if (array_key_exists(__CLASS__, $classMap)) {
$this->markTestSkipped('Error only works with non optimized autoloader');
}
$autoloader->unregister();

Expand All @@ -92,9 +98,9 @@ public function testErrorCheck(){
$autoloader->register();
$this->fail();
return;
}catch(\Exception $e){
} catch (\Exception $e) {
$autoloader->register();
$this->assertInstanceOf(\Exception::class, $e);
}
}
}
}

0 comments on commit 425342e

Please sign in to comment.