fwrite/phpunit/文件权限冲突
我正在编写一个PHP脚本,其中Webinterface在VM机器上运行。开发工作在我的本地机器上进行,在那里我通过PHPUnit(从命令行)运行单元测试。当然,我是这两台ubuntu机器的root用户 当我应用一个函数,fwrite/phpunit/文件权限冲突,php,json,linux,file-io,Php,Json,Linux,File Io,我正在编写一个PHP脚本,其中Webinterface在VM机器上运行。开发工作在我的本地机器上进行,在那里我通过PHPUnit(从命令行)运行单元测试。当然,我是这两台ubuntu机器的root用户 当我应用一个函数,jsonWrite($string),将数组中的数据写入一个.json文件时,就会出现问题。此文件位于VM(服务器)上,具有(这是一个测试环境:)777权限。该函数工作得很好-直到我使用PHP单元,在该单元中,我的测试函数testRun()调用函数run(),该函数使用jsonW
jsonWrite($string)
,将数组中的数据写入一个.json
文件时,就会出现问题。此文件位于VM(服务器)上,具有(这是一个测试环境:)777权限。该函数工作得很好-直到我使用PHP单元,在该单元中,我的测试函数testRun()
调用函数run()
,该函数使用jsonWrite
之后,文件权限更改为664,脚本无法再访问.json
文件,我不理解,因为它最初拥有该文件
问题1:如何防止phpunit在调用函数时更改文件权限
问题2:如何配置fwrite()
或fopen()
来设置写入的特定权限
我提前感谢您提供的有用提示/答案
以下是相关功能的代码:
public function jsonWrite($string)
{
... converting string to array $game ...
$fp = fopen('game.json', 'w'); // var_dump($fp); -> gives false after phpunit run
fwrite($fp, json_encode($game));
fclose($fp);
return $game;
}
public function run()
{
...
$this->calculate($this);
$string= $this->display($this);
$this->jsonWrite($string);
return $this;
}
public function testRun()
{
$actual = new BowlCount();
$actual = $actual->run();
for($i=0;$i<10;$i++) {$this->assertInstanceOf('Frame', $actual->Frames[$i]);}
...
}
公共函数jsonWrite($string)
{
…正在将字符串转换为数组$game。。。
$fp=fopen('game.json','w');//var_dump($fp);->在phpunit运行后给出false
fwrite($fp,json_encode($game));
fclose($fp);
返回$game;
}
公共功能运行()
{
...
$this->calculate($this);
$string=$this->display($this);
$this->jsonWrite($string);
退还$this;
}
公共函数testRun()
{
$actual=新的保龄球计数();
$actual=$actual->run();
对于($i=0;$iassertInstanceOf('Frame',$actual->Frames[$i]);}
...
}
664(rw、rw、r)应该足够了-涉及哪些用户/组(之前和之后)基本上,即使在测试环境中,您也不应该拥有777的文件。664是可以的,只要确保所有权是正确的。之前:game.json-rwxrwxrwx 1 tr0tt3l tr0tt3l 401 Nov 9 00:00之后:game.json-rw-rw-r--1 tr0tt3l tr0tt3l 401 Nov 9 00:00识别用户的问题是:我真的不能说用户是否str0tt3l@VBtest或tr0tt3l@tr0tt3lmachine...如果我能弄清楚,请告诉我怎么做!谢谢!