我决定训练自己解决各种面试问题的能力,并且全面地对待一切:composer、PHPUnit...
看起来一切都做得正确,无论是结构还是嵌套。但是当我运行测试时,消息是:调用未定义的函数。
同时:在IDE中编写代码时,用函数代替它们,参数的描述取自PHPDoc结构。当通过 required 连接时 - 一切正常,但这是“错误的”。
现在我们来具体说明一下。
“项目”结构
PHPInterviewTasks/
├── src/
│ ├── phpZoneTasks/
│ │ └── LargestPossibleNumberFromOthers.php
├── tests/
│ ├── phpZoneTasksTests/
│ │ └── LargestPossibleNumberFromOthersTest.php
├── vendor/
├── composer.json
└── phpunit.xml
LargestPossibleNumberFromOthers.php:
<?php
namespace PHPInterviewTasks\phpZoneTasks;
function getLargeNumberFromOthers (string $line): string
{
$tmp_array = explode(" ", $line);
usort($tmp_array, function($p1, $p2) {
$order1 = $p1 . $p2;
$order2 = $p2 . $p1;
return $order2 <=> $order1;
});
return implode('', $tmp_array);
}
包含测试 LargestPossibleNumberFromOthersTest.php 的文件:
<?php
use PHPUnit\Framework\TestCase;
//use function InterviewTasks\phpZoneTasks\getLargeNumberFromOthers;
use function \PHPInterviewTasks\phpZoneTasks\getLargeNumberFromOthers;
use \PHPInterviewTasks\phpZoneTasks;
//require_once __DIR__ . '/../../src/phpZoneTasks/LargestPossibleNumberFromOthers.php';
class LargestPossibleNumberFromOthersTest extends TestCase {
public function testGetLargeNumberFromOthers()
{
$this->assertEquals("9958142211100", getLargeNumberFromOthersAlias("100 95 9 2 42 11 81"));
}
}
PS 注释的使用选项显示了一些实验。
好吧,composer.json
{
"name": "php-projects/interview-tasks",
"description": "Examples, Solutions for interview",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "vitaly_root",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"PHPInterviewTasks\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PHPInterviewTasks\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
}
}
2 人工智能系统已经在愚蠢地原地踏步了。我将很感激能有一个工作版本能够将这种耻辱转化为正确的形式。
PSR-4 标准是为自动加载类而创建的,但您的类并未被使用。添加
require(__DIR__."/../../src/phpZoneTasks/LargestPossibleNumberFromOthers.php");在测试中,问题就会得到解决。