[英]PHP require_once not working: no such file in directory
I am having an issue with paths when testing the application using PHPUnit to test. 使用PHPUnit测试应用程序时,路径出现问题。 I have a test called
UserControllerTest
: 我有一个名为
UserControllerTest
的测试:
<?php
require_once __DIR__ . "/../../Controllers/UserController.php";
class UserControllerTest extends \PHPUnit_Framework_TestCase {
private $controller;
protected function setUp() {
$this->controller = new UserController();
}
public function testThatCanGeneratePromoCode() {
$promoCode = $this->controller->generatePromoCode();
$this->assertEquals(20, strlen($promoCode));
}
}
Which is testing UserController
class: 正在测试
UserController
类:
<?php
require_once "../config/DbConnection.php";
require_once "../Model/ConsumerModel.php";
require_once "../Managers/UtilManager.php";
class UserController {
private $dbh;
private $utilManager;
private $model;
/**
* UserController constructor.
* @param null|ConsumerModel $model
* @internal param $dbh
*/
public function __construct(ConsumerModel $model = null) {
$this->dbh = new DbConnection();
$this->utilManager = new UtilManager();
$this->model = $model;
}
public function generatePromoCode() {
$length = 20;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
And I keep getting this error when I run the test: PHP Warning: require_once(../config/DbConnection.php): failed to open stream: No such file or directory in C:\\Users\\MrDan\\Documents\\code\\twinportrait-api\\Controllers\\UserController.php on line 3
运行测试时,我一直收到此错误:
PHP Warning: require_once(../config/DbConnection.php): failed to open stream: No such file or directory in C:\\Users\\MrDan\\Documents\\code\\twinportrait-api\\Controllers\\UserController.php on line 3
However, if I use require_once __DIR__ .
但是,如果我使用
require_once __DIR__ .
for all the required files in the UserController
class, the test works. 对于
UserController
类中所有必需的文件,该测试有效。 But the deployment on Heroku fails throwing a 500 status code
. 但是Heroku上的部署无法抛出
500 status code
。 Any suggestion of how to fix this issue? 关于如何解决此问题的任何建议?
This is my project structure: 这是我的项目结构:
You need to figure out what path you have to begin with. 您需要弄清楚必须开始的路径。 First
第一
var_dump( realpath( __DIR__ . '/../..' ) );
This will show you what parent directory you are trying to navigate to. 这将显示您要导航到的父目录。 Then correct the path to
config/DbConnection.php
from there. 然后从此处更正
config/DbConnection.php
的路径。
The __DIR__
magic constant gives you the same directory that the file it exists in lives in. realpath
will return false
on an invalid path. 该
__DIR__
魔术不断给你的文件它存在于生活在同一目录下。 realpath
将返回false
无效的路径上。 If the above returns false
, it means your webserver user does not have directory permissions to access that folder, because the folder two levels down should definitely exist in your case. 如果上述返回
false
,则意味着您的网络服务器用户没有访问该文件夹的目录权限,因为在您的情况下,肯定应存在两个级别以下的文件夹。 If that is the case, you need to chmod
the config folder. 如果是这种情况,则需要
chmod
config文件夹。 Otherwise your path string is off. 否则,您的路径字符串将关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.