Skip to content

Commit

Permalink
Merge pull request #283 from nanasess/fix-autoloader
Browse files Browse the repository at this point in the history
SC_ClassAutoloader を Composer の autoloader より優先するよう修正
  • Loading branch information
kazumiiiiiiiiiii authored Jul 2, 2019
2 parents b1932bf + f54834b commit a097589
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ before_script:

script:
- mkdir -p reports/coverage
- if [ ! $COVERAGE ] ; then php data/vendor/bin/phpunit -c phpunit.xml.dist ; fi
- if [ $COVERAGE ] ; then phpdbg -qrr data/vendor/bin/phpunit -c phpunit.xml.dist ; fi
- if [ ! $COVERAGE ] ; then php data/vendor/bin/phpunit -c phpunit.xml.dist --exclude-group classloader ; fi
- if [ ! $COVERAGE ] ; then php data/vendor/bin/phpunit -c phpunit.xml.dist --group classloader ; fi
- if [ $COVERAGE ] ; then phpdbg -qrr data/vendor/bin/phpunit -c phpunit.xml.dist --exclude-group classloader ; fi
- if [ ! $COVERAGE ] ; then php data/vendor/bin/codecept run --env chrome --steps ; fi

after_script:
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ build: off

before_test:
- bash eccube_install.sh mysql

test_script:
- data/vendor/bin/phpunit
- data/vendor/bin/phpunit --exclude-group classloader
2 changes: 1 addition & 1 deletion data/app_initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

// クラスのオートローディングを定義する
require_once(CLASS_EX_REALDIR . 'SC_ClassAutoloader_Ex.php');
spl_autoload_register(array('SC_ClassAutoloader_Ex', 'autoload'));
spl_autoload_register(array('SC_ClassAutoloader_Ex', 'autoload'), true, true);

SC_Helper_HandleError_Ex::load();

Expand Down
137 changes: 137 additions & 0 deletions tests/class/plugin/LoadClassFileChangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* @group classloader
*/
class LoadClassFileChangeTest extends Common_TestCase
{
protected function setUp()
{
parent::setUp();
$this->createPlugin();
if (PHP_VERSION_ID < 70200) {
$objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
$objPlugin->load();
}
}

protected function tearDown()
{
$plugins = ['AutoloadingPlugin'];
foreach ($plugins as $plugin) {
$dir = PLUGIN_UPLOAD_REALDIR.$plugin;
if (!file_exists($dir)) break;
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
/** @var SplFileInfo $file */
$file->isDir() ? rmdir($file->getPathname()) : unlink($file->getRealPath());
}
rmdir($dir);
}
parent::tearDown();
}

/**
* loadClassFileChange で拡張したクラスのテストケース.
*/
public function testLoadExtendedClass()
{
$objProduct = new SC_Product_Ex();
$this->assertTrue(constant('SC_Product_Ex::AUTOLOAD'), 'loadClassFileChange で拡張したクラス定数にアクセスできる');

$refclass = new ReflectionClass($objProduct);
$this->assertTrue($refclass->hasProperty('autoloaded'), 'loadClassFileChange で拡張したプロパティが存在する');

$refProp = $refclass->getProperty('autoloaded');
$this->assertTrue($refProp->getValue($objProduct), 'loadClassFileChange で拡張したプロパティにアクセスできる');

$this->assertTrue($refclass->hasMethod('isExtended'), 'loadClassFileChange で拡張したメソッドが存在する');
$refMethod = $refclass->getMethod('isExtended');
$this->assertTrue($refMethod->invoke($objProduct), 'loadClassFileChange で拡張したメソッドにアクセスできる');
}

/**
* ダミーのプラグインをインストールする.
*/
private function createPlugin()
{
$realdir = PLUGIN_UPLOAD_REALDIR;
$plugin_info = <<< __EOS__
<?php
class plugin_info {
static \$PLUGIN_CODE = 'AutoloadingPlugin';
static \$PLUGIN_NAME = 'AutoloadingPlugin';
static \$CLASS_NAME = 'AutoloadingPlugin';
static \$PLUGIN_VERSION = '0.0.0';
static \$COMPLIANT_VERSION = '2.17';
static \$AUTHOR = 'dummy';
static \$DESCRIPTION = 'dummy';
static \$HOOK_POINTS = 'loadClassFileChange';
}
__EOS__;
$autoloadingPlugin = <<< __EOS__
<?php
class AutoloadingPlugin extends SC_Plugin_Base
{
public function loadClassFileChange(&\$classname, &\$classpath) {
if (\$classname === "SC_Product_Ex") {
\$classpath = "${realdir}/AutoloadingPlugin/Autoloading_SC_Product.php";
\$classname = "Autoloading_SC_Product";
}
}
}
__EOS__;
$Autoloading_SC_Product = <<< __EOS__
<?php
class Autoloading_SC_Product extends SC_Product
{
const AUTOLOAD = true;
public \$autoloaded = true;
public function isExtended() {
return true;
}
}
__EOS__;

$files = [
'plugin_info' => $plugin_info,
'AutoloadingPlugin' => $autoloadingPlugin,
'Autoloading_SC_Product' => $Autoloading_SC_Product
];

$dir = PLUGIN_UPLOAD_REALDIR.'AutoloadingPlugin';
if (!file_exists($dir)) {
mkdir($dir);
}
foreach ($files as $name => $content) {
file_put_contents($dir.'/'.$name.'.php', $content);
}

$plugin_id = $this->objQuery->nextVal('dtb_plugin_plugin_id');
$pluginValues = [
'plugin_id' => $plugin_id,
'plugin_name' => 'AutoloadingPlugin',
'plugin_code' => 'AutoloadingPlugin',
'class_name' => 'AutoloadingPlugin',
'plugin_version' => '0.0.0',
'compliant_version' => '2.17',
'enable' => 1,
'create_date' => 'CURRENT_TIMESTAMP',
'update_date' => 'CURRENT_TIMESTAMP'
];
$this->objQuery->insert('dtb_plugin', $pluginValues);

$plugin_hookpoint_id = $this->objQuery->nextVal('dtb_plugin_hookpoint_plugin_hookpoint_id');
$hookpointValues = [
'plugin_hookpoint_id' => $plugin_hookpoint_id,
'plugin_id' => $plugin_id,
'hook_point' => 'loadClassFileChange',
'callback' => 'loadClassFileChange',
'update_date' => 'CURRENT_TIMESTAMP'
];
$this->objQuery->insert('dtb_plugin_hookpoint', $hookpointValues);
}
}

0 comments on commit a097589

Please sign in to comment.