codeception 数组示例

问题描述 投票:0回答:3

是否有一种实现方法可以将示例转换为数组,以便我可以将其作为参数直接传递给发布请求。现在我要做的是:

/**
     * @dataprovider invalidUserActivityRequestProvider
     */
    public function it_does_not_get_tracked_for_an_invalid_request(ApiTester $I, Example $example)
    {
        $userActivity = [
            'timestamp' => isset($example['timestamp']) ? $example['timestamp'] : null ,
            'email' => isset($example['email']) ? $example['email'] : null ,
            'type' => isset($example['type']) ? $example['type'] : null ,
            'duration' => isset($example['duration']) ? $example['duration'] : null ,
            'distance' => isset($example['distance']) ? $example['distance'] : null ,
            'repetitions' => isset($example['repetitions']) ? $example['repetitions'] : null ,
        ];

        $I->sendPOST('/useractivity', $userActivity);
        $I->seeResponseCodeIs(422);
    }

    protected function invalidUserActivityRequestProvider () : array {
        return [
            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => '[email protected]',
                'type' => 'run',
                'duration' => 300,
                'distance' => 1000,
                'repetitions' => 1
            ],

            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => '[email protected]',
                'type' => 'invalidType',
                'duration' => 300,
                'distance' => 1000,
                'repetitions' => 1
            ],

            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => '[email protected]',
                'type' => 'run'
            ],
        ];
    }

但我想要类似的东西:

public function it_does_not_get_tracked_for_an_invalid_request(ApiTester $I, Example $example)
    {
        $I->sendPOST('/useractivity', $example->toArray());
        $I->seeResponseCodeIs(422);
    }
php testing codeception
3个回答
1
投票

我不知道 Codeception,但强制转换是一种选择:

$I->sendPOST('/useractivity', (array)$example);

0
投票

抱歉,它实际上不起作用!...

尝试断言

(array) $example
和数组会导致:

- Expected | + Actual
@@ @@
Array (
-    Binary String: 0x002a0064617461 => Array (...)
+    'id' => 6
+    'unique_id' => '2199033651380'
+    'data' => Array (...)
+    'user_id' => 1
+    'brand_id' => 1
+    'infrastructure' => 'AT'
)

0
投票

PHP 7.4 开始,您可以使用数组扩展运算符。由于 Codeception 示例对象类 (

Codeception\Example
) 实现了
ArrayAccess
接口,您可以使用类似以下内容:

$I->sendPOST('/useractivity', [...$example]);
© www.soinside.com 2019 - 2024. All rights reserved.