Laminas 单元测试控制器操作 POST 参数不起作用

时间:2021-04-12 14:09:30

标签: unit-testing phpunit zend-framework3 laminas

我有一个控制器 extends AbstractHttpControllerTestCase

我尝试将参数 (some_uuid) 传递给 dispatch() 函数(在我的 testFoo 函数中):

$this->dispatch('/my-special-url', 'POST', ['some_uuid' => '16294469-f531-40d6-89e9-3ab677a47c45']);
$this->assertResponseStatusCode(200);

为什么参数不在控制器中?

[编辑] 没有很多代码。但是,如果有人想看到一切:

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');
    }
}

1 个答案:

答案 0 :(得分:0)

问题出在我要测试的控制器中。我使用 filter_input() 来获取 POST 值!

如果我使用 $this->getRequest()->getPost('some_uuid') 获取 POST 值,它工作正常,我的单元测试显示 OK。

相关问题