Skip to content

Commit

Permalink
Remove unnecessary staff
Browse files Browse the repository at this point in the history
  • Loading branch information
wppunk committed Oct 18, 2020
1 parent fb4b10e commit 3fd4d8d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 431 deletions.
27 changes: 4 additions & 23 deletions src/class-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,16 @@ class Autoload {
* @var string
*/
private $folder;
/**
* Cache
*
* @var Cache
*/
private $cache;

/**
* Autoload constructor.
*
* @param string $prefix Prefix for your namespace.
* @param string $folder Path to folder.
* @param Cache $cache Cache.
*/
public function __construct( $prefix, $folder, Cache $cache ) {
public function __construct( $prefix, $folder ) {
$this->prefix = ltrim( $prefix, '\\' );
$this->folder = $folder;
$this->cache = $cache;
spl_autoload_register( [ $this, 'autoload' ] );
}

Expand All @@ -68,18 +60,12 @@ public function __construct( $prefix, $folder, Cache $cache ) {
*
* @throws Exception Class not found.
*/
private function autoload( $class ) {
public function autoload( $class ) {
if ( 0 !== strpos( $class, $this->prefix ) ) {
return;
}

$path = $this->cache->get( $class );
if ( ! $path ) {
$path = $this->file_path( $class );
$this->cache->update( $class, $path );
}

require_once $path;
require_once $this->file_path( $class );
}

/**
Expand All @@ -88,8 +74,6 @@ private function autoload( $class ) {
* @param string $class Full class name.
*
* @return string
*
* @throws Exception Class not found.
*/
private function file_path( $class ) {
$class = str_replace( $this->prefix, '', $class );
Expand All @@ -102,11 +86,8 @@ private function file_path( $class ) {
$local_path = strtolower( str_replace( [ '\\', '_' ], [ '/', '-' ], $local_path ) );

$path = $this->folder . '/' . $local_path;
if ( file_exists( $path ) ) {
return $path;
}

throw new Exception( $class, $path );
return $path;
}

}
156 changes: 0 additions & 156 deletions src/class-cache.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/class-exception.php

This file was deleted.

83 changes: 8 additions & 75 deletions tests/phpunit/tests/class-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
* @license GPL-2.0+
*/

use Prefix\Autoload_Fail;
use WPPunk\Autoload\Autoload;
use Prefix\Autoload_Success_2;
use Prefix\Autoload_Success_3;
use Prefix\Autoload_Success_4;
use Prefix\Autoload_Success_1;
use Prefix\Autoload_Success_3;
use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/../../../src/class-autoload.php';

/**
* Class Test_Autoload
*/
Expand Down Expand Up @@ -57,15 +53,7 @@ public function tearDown(): void { // phpcs:ignore PHPCompatibility.FunctionDecl
* Test success load class by path in first folder inside cache.
*/
public function test_success_load_by_first_path() {
$cache = Mockery::mock( 'WPPunk\Autoload\Cache' );
$cache->shouldReceive( 'get' )->andReturn( '' );
$cache->shouldReceive( 'update' )->once();

$autoload = new Autoload(
self::PREFIX,
self::FOLDER1,
$cache
);
$autoload = new Autoload( self::PREFIX, self::FOLDER1 );
new Autoload_Success_1();

spl_autoload_unregister( [ $autoload, 'autoload' ] );
Expand All @@ -75,33 +63,18 @@ public function test_success_load_by_first_path() {
* Test success load class by path in second folder inside cache
*/
public function test_success_load_by_second_path() {
$cache = Mockery::mock( 'WPPunk\Autoload\Cache' );
$cache->shouldReceive( 'get' )->andReturn( '' );
$cache->shouldReceive( 'update' )->once();

$autoload = new Autoload(
self::PREFIX,
self::FOLDER2,
$cache
);
$autoload = new Autoload( self::PREFIX, self::FOLDER2 );
new Autoload_Success_2();

spl_autoload_unregister( [ $autoload, 'autoload' ] );
}

/**
* Test success load class from cache.
* Test loading interface
*/
public function test_success_load_from_cache() {
$cache = Mockery::mock( 'WPPunk\Autoload\Cache' );
$cache->shouldReceive( 'get' )->andReturn( __DIR__ . '/../classes/path-1/prefix/class-autoload-success-4.php' );

$autoload = new Autoload(
self::PREFIX,
self::FOLDER1,
$cache
);
new Autoload_Success_4();
public function test_success_load_interface() {
$autoload = new Autoload( self::PREFIX, self::FOLDER1 );
new Autoload_Success_3();

spl_autoload_unregister( [ $autoload, 'autoload' ] );
}
Expand All @@ -110,11 +83,7 @@ public function test_success_load_from_cache() {
* Test invalid namespace.
*/
public function test_invalid_namespace() {
$autoload = new Autoload(
self::PREFIX,
self::FOLDER1,
Mockery::mock( 'WPPunk\Autoload\Cache' )
);
$autoload = new Autoload( self::PREFIX, self::FOLDER1 );
function test_autoload( $class ) {
if ( 'Invalid_Name_Space' !== $class ) {
return;
Expand All @@ -129,40 +98,4 @@ function test_autoload( $class ) {
spl_autoload_unregister( 'test_autoload' );
}

/**
* Test loading interface
*/
public function test_success_load_interface() {
$cache = Mockery::mock( 'WPPunk\Autoload\Cache' );
$cache->shouldReceive( 'get' )->andReturn( '' );
$cache->shouldReceive( 'update' );

$autoload = new Autoload(
self::PREFIX,
self::FOLDER1,
$cache
);
new Autoload_Success_3();

spl_autoload_unregister( [ $autoload, 'autoload' ] );
}

/**
* Test invalid class path.
*
* @noinspection PhpUndefinedClassInspection
*/
public function test_fail_load() {
$cache = Mockery::mock( 'WPPunk\Autoload\Cache' );
$cache->shouldReceive( 'get' )->andReturn( '' );

new Autoload(
self::PREFIX,
self::FOLDER1,
$cache
);
$this->expectException( WPPunk\Autoload\Exception::class );
new Autoload_Fail();
}

}
Loading

0 comments on commit 3fd4d8d

Please sign in to comment.