symfony phpunit 未收到预期值
symfony phpunit not receive expected value
我正在尝试在 Symfony 中测试我的服务中的一个简单函数。
函数获取 post 值(如果存在)并设置一个多维数组。
这是我在服务中的功能:
public function setValue(Request $request)
{
for($i = 1; $i <= 3; $i++){
for($j = 1; $j <= 3; $j++){
if(($request->request->get('cell_' . $i . '_' . $j) != null){
$value = $request->request->get('cell_' . $i . '_' . $j);
echo('Value: ' . $value . '|');
$this->tiles[$i][$j] = $value;
}
else{
$this->tiles[$i][$j] = '0';
}
}
}
}
这是我的测试部分(不是全部测试,只是一个简单的部分)
public function testGetSatusGameEnd()
{
$this->requestMock = $this
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$this->requestMock->request = $this
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
->disableOriginalConstructor()
->getMock();
$this->requestMock->request->expects($this->at(0))
->method('get')
->with('cell_1_1')
->will($this->returnValue('1'));
$board = new Board();
$board->setValue($this->requestMock);
var_dump($board->getArrayResult());
}
在这种情况下,理论上我只设置了一个值为 1 的单元格,但是当我转储所有结果时,我得到了这个
array(3) {
[1] =>
array(3) {
[1] =>
NULL
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
[2] =>
array(3) {
[1] =>
string(1) "0"
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
[3] =>
array(3) {
[1] =>
string(1) "0"
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
}
因为在 cell_1_1
里面我检查了一个空值,但我希望 1
不为空!
我怎么 return 1
里面 cell_1_1
,我的模拟错误是什么?
谢谢
您没有填充数组的索引 0
。
看看你的循环:
...
for($i = 1; $i <= 3; $i++){
for($j = 1; $j <= 3; $j++){
...
两者都从索引 1
开始,而不是从索引 0
开始。这样第一个 array-element 永远不会被设置,这就是为什么它是 null
.
你必须这样改变你的for
。
for($i = 0; $i < 3; $i++){
for($j = 0; $j < 3; $j++){
get
为 return 的原因是 at()
的索引已经在第一次调用(在 if 语句中)时递增。
我在 this article 上发现了一些关于它具有误导性的有用信息。
您可能根本不想理会 expects()
和 at()
,因为它并没有真正反映实际参数包的行为,return 是相同的不管它是什么时候调用的。
只有当名称为 cell_1_1 时,您才可以使用带有回调 return '1' 的模拟对象,如下所示:
$this
->requestMock
->request
->method('get')
->will($this->returnCallback(function ($name) {
return $name === 'cell_1_1' ? '1' : null;
}));
或者你可以只使用 Symfony 请求本身,并且自己也有很多复杂的事情:-)
使用真实请求进行测试的示例代码:
$request = new Request([], ['cell_1_1' => '1']);
$board = new Board();
$board->setValue($request);
我正在尝试在 Symfony 中测试我的服务中的一个简单函数。
函数获取 post 值(如果存在)并设置一个多维数组。 这是我在服务中的功能:
public function setValue(Request $request)
{
for($i = 1; $i <= 3; $i++){
for($j = 1; $j <= 3; $j++){
if(($request->request->get('cell_' . $i . '_' . $j) != null){
$value = $request->request->get('cell_' . $i . '_' . $j);
echo('Value: ' . $value . '|');
$this->tiles[$i][$j] = $value;
}
else{
$this->tiles[$i][$j] = '0';
}
}
}
}
这是我的测试部分(不是全部测试,只是一个简单的部分)
public function testGetSatusGameEnd()
{
$this->requestMock = $this
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();
$this->requestMock->request = $this
->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
->disableOriginalConstructor()
->getMock();
$this->requestMock->request->expects($this->at(0))
->method('get')
->with('cell_1_1')
->will($this->returnValue('1'));
$board = new Board();
$board->setValue($this->requestMock);
var_dump($board->getArrayResult());
}
在这种情况下,理论上我只设置了一个值为 1 的单元格,但是当我转储所有结果时,我得到了这个
array(3) {
[1] =>
array(3) {
[1] =>
NULL
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
[2] =>
array(3) {
[1] =>
string(1) "0"
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
[3] =>
array(3) {
[1] =>
string(1) "0"
[2] =>
string(1) "0"
[3] =>
string(1) "0"
}
}
因为在 cell_1_1
里面我检查了一个空值,但我希望 1
不为空!
我怎么 return 1
里面 cell_1_1
,我的模拟错误是什么?
谢谢
您没有填充数组的索引 0
。
看看你的循环:
...
for($i = 1; $i <= 3; $i++){
for($j = 1; $j <= 3; $j++){
...
两者都从索引 1
开始,而不是从索引 0
开始。这样第一个 array-element 永远不会被设置,这就是为什么它是 null
.
你必须这样改变你的for
。
for($i = 0; $i < 3; $i++){
for($j = 0; $j < 3; $j++){
get
为 return 的原因是 at()
的索引已经在第一次调用(在 if 语句中)时递增。
我在 this article 上发现了一些关于它具有误导性的有用信息。
您可能根本不想理会 expects()
和 at()
,因为它并没有真正反映实际参数包的行为,return 是相同的不管它是什么时候调用的。
只有当名称为 cell_1_1 时,您才可以使用带有回调 return '1' 的模拟对象,如下所示:
$this
->requestMock
->request
->method('get')
->will($this->returnCallback(function ($name) {
return $name === 'cell_1_1' ? '1' : null;
}));
或者你可以只使用 Symfony 请求本身,并且自己也有很多复杂的事情:-)
使用真实请求进行测试的示例代码:
$request = new Request([], ['cell_1_1' => '1']);
$board = new Board();
$board->setValue($request);