Composer文件设置错误?不自动加载我的 class - PHP
Composer file is set up wrong? Not Auto loading my class - PHP
在上图中你可以看到我的composer.json
。此文件属于Freya/Loader/Assets,您可以看到已展开。
您还可以看到已创建供应商目录,并且我有一个 phpunit.xml 文件,如下所示:
<phpunit
bootstrap="bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
当我在此目录中的终端中 运行 phpunit
时,出现错误:
PHP Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
测试看起来像:
<?php
use Freya\Loader\Assets;
class AssetsTest extends WP_UnitTestCase {
public function testAssetsAreRegistered() {
// $assetsToregister = array(
// 'css' => array(
// 'some_file' => 'http://example.com'
// ),
// 'js' => array(
// 'some_file' => 'http://example.com'
// )
// 'front_jquery_version' => '2.7.1'
// );
$assets = new AssetLoader();
var_dump($assets->getAssets());
}
}
我不明白为什么不需要这个文件。我正在使用 psr-4
标准,据我所知,我的作曲家文件没有任何问题。 class 与文档相当长,因此我粘贴 shell 以向您显示名称空间和 class 定义:
AssetLoader.php
<?php
namespace Freya\Loader\Assets;
class AssetLoader { ... }
**我错过了什么吗?输入错误?在我看来一切都是正确的...... **
更新一个
根据要求,请参阅 bootstrap 文件。
<?php
/** ---------------------------------------------------- **/
// Require the vendors autoload file.
/** ---------------------------------------------------- **/
require_once 'vendor/autoload.php';
/** ---------------------------------------------------- **/
// We neeed WordPress Bootstrap files for its test.
/** ---------------------------------------------------- **/
define('WP_TEST_DIR', parse_ini_file('test-config.ini')['test-location']);
// Include the bootstrap file.
require_once WP_TEST_DIR . 'includes/bootstrap.php';
// Include the Functions file
require_once WP_TEST_DIR . 'includes/functions.php';
更新 2
所以有人建议我 运行 composer dump-autoload
我做了,然后我 运行 phpunit 和我遇到了同样的问题:
$ composer dump-autoload
Generating autoload files
$ phpunit
Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml
PHP Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:535
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:105
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:153
PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:406
PHP 6. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
PHP 7. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
PHP 8. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:699
PHP 9. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:609
PHP 10. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:743
PHP 11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
PHP 12. AssetsTest->testAssetsAreRegistered() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
Call Stack:
0.0007 389896 1. {main}() /usr/local/bin/phpunit:0
0.0789 13012000 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:535
0.0789 13012648 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:105
1.6249 38992520 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:153
1.6292 39003688 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:406
1.6294 39007208 6. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
1.6305 39011272 7. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
1.6305 39012816 8. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:699
1.6306 39015248 9. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:609
1.6409 39068824 10. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:743
1.6409 39069592 11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
1.6410 39069672 12. AssetsTest->testAssetsAreRegistered() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
use Freya\Loader\Assets;
这并不意味着 "import everything from this namespace" 您可能习惯于使用其他语言。在 PHP 中,这仅表示:在此文件中将 Freya\Loader\Assets
别名为 Assets
。
这意味着 new AssetsLoader()
仍会尝试从全局范围加载它(这就是为什么您只在 class 未找到消息中看到 AssetsLoader
,而不是 Freya\Loader\Assets\AssetsLoader
).
要完成这项工作,请使用 new Assets\AssetsLoader()
(记住,我们将 Freya\Loader\Assets
别名为 Assets
)或 use Freya\Loader\Assets\AssetsLoader
(这样,AssetsLoader
就是此 FQCN 的别名)。
在上图中你可以看到我的composer.json
。此文件属于Freya/Loader/Assets,您可以看到已展开。
您还可以看到已创建供应商目录,并且我有一个 phpunit.xml 文件,如下所示:
<phpunit
bootstrap="bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
当我在此目录中的终端中 运行 phpunit
时,出现错误:
PHP Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
测试看起来像:
<?php
use Freya\Loader\Assets;
class AssetsTest extends WP_UnitTestCase {
public function testAssetsAreRegistered() {
// $assetsToregister = array(
// 'css' => array(
// 'some_file' => 'http://example.com'
// ),
// 'js' => array(
// 'some_file' => 'http://example.com'
// )
// 'front_jquery_version' => '2.7.1'
// );
$assets = new AssetLoader();
var_dump($assets->getAssets());
}
}
我不明白为什么不需要这个文件。我正在使用 psr-4
标准,据我所知,我的作曲家文件没有任何问题。 class 与文档相当长,因此我粘贴 shell 以向您显示名称空间和 class 定义:
AssetLoader.php
<?php
namespace Freya\Loader\Assets;
class AssetLoader { ... }
**我错过了什么吗?输入错误?在我看来一切都是正确的...... **
更新一个
根据要求,请参阅 bootstrap 文件。
<?php
/** ---------------------------------------------------- **/
// Require the vendors autoload file.
/** ---------------------------------------------------- **/
require_once 'vendor/autoload.php';
/** ---------------------------------------------------- **/
// We neeed WordPress Bootstrap files for its test.
/** ---------------------------------------------------- **/
define('WP_TEST_DIR', parse_ini_file('test-config.ini')['test-location']);
// Include the bootstrap file.
require_once WP_TEST_DIR . 'includes/bootstrap.php';
// Include the Functions file
require_once WP_TEST_DIR . 'includes/functions.php';
更新 2
所以有人建议我 运行 composer dump-autoload
我做了,然后我 运行 phpunit 和我遇到了同样的问题:
$ composer dump-autoload
Generating autoload files
$ phpunit
Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml
PHP Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:535
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:105
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:153
PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:406
PHP 6. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
PHP 7. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
PHP 8. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:699
PHP 9. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:609
PHP 10. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:743
PHP 11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
PHP 12. AssetsTest->testAssetsAreRegistered() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
Fatal error: Class 'AssetLoader' not found in /vagrant/Freya/Loader/Assets/tests/AssetsTest.php on line 18
Call Stack:
0.0007 389896 1. {main}() /usr/local/bin/phpunit:0
0.0789 13012000 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:535
0.0789 13012648 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:105
1.6249 38992520 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:153
1.6292 39003688 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:406
1.6294 39007208 6. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
1.6305 39011272 7. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:722
1.6305 39012816 8. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:699
1.6306 39015248 9. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:609
1.6409 39068824 10. PHPUnit_Framework_TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:743
1.6409 39069592 11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
1.6410 39069672 12. AssetsTest->testAssetsAreRegistered() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:866
use Freya\Loader\Assets;
这并不意味着 "import everything from this namespace" 您可能习惯于使用其他语言。在 PHP 中,这仅表示:在此文件中将 Freya\Loader\Assets
别名为 Assets
。
这意味着 new AssetsLoader()
仍会尝试从全局范围加载它(这就是为什么您只在 class 未找到消息中看到 AssetsLoader
,而不是 Freya\Loader\Assets\AssetsLoader
).
要完成这项工作,请使用 new Assets\AssetsLoader()
(记住,我们将 Freya\Loader\Assets
别名为 Assets
)或 use Freya\Loader\Assets\AssetsLoader
(这样,AssetsLoader
就是此 FQCN 的别名)。