[英]Laminas Unit-test controller-action POST parameter does not work
我有一个 controller extends AbstractHttpControllerTestCase
。
我尝试将参数(some_uuid)传递给 dispatch() function (在我的 testFoo 函数中):
$this->dispatch('/my-special-url', 'POST', ['some_uuid' => '16294469-f531-40d6-89e9-3ab677a47c45']);
$this->assertResponseStatusCode(200);
为什么controller中没有参数?
[编辑] 没有很多代码。 但是,如果有人想看到一切:
class ProductStockAjaxControllerTest extends AbstractHttpControllerTestCase
{
protected function setUp(): void
{
$configOverrides = [];
$this->setApplicationConfig(ArrayUtils::merge(
include __DIR__ . '/../../../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
/** @var UserService $userService */
$userService = $this->getApplication()->getServiceManager()->get(UserService::class);
$userService->setSessionHashManually('151956ad612953e61533b9d2d5844d65df5b5c8c4e35f5e49375ef77711b6676');
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/my-special-url', 'POST'
, ['some_uuid' => '16294469-f531-40d6-89e9-3ab677a47c45', 'ano_uuid' => '0a6cea79-9869-4da4-b8db-02bd21d1488a']);
//$this->assertResponseStatusCode(200);
$this->assertModuleName('lerp');
$this->assertControllerName(ProductStockAjaxController::class);
$this->assertControllerClass('ProductStockAjaxController');
$this->assertMatchedRouteName('my_special_url');
}
}
问题出在我要测试的 controller 中。 我在那里使用filter_input()
来获取 POST 值!
如果我使用$this->getRequest()->getPost('some_uuid')
获取 POST 值,它工作正常,我的单元测试说 OK。
这可能是因为如果在您的 controller 中您有类似的东西:
$some_uuid = $this->params()->fromRoute('some_uuid', 0);
它不起作用,因为它需要路由中的参数,这是一个 GET 参数,而不是一个 POST 参数。 在这种情况下,测试本身可能如下所示:
$this->dispatch('/my-special-url/16294469-f531-40d6-89e9-3ab677a47c45');
bitkorn 的答案通过默认等待 POST 参数来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.