1

Is there an implemented method to convert an Example to an Array so I can pass it right to a post request as parameters. Right now I have to do:

/**
     * @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'
            ],
        ];
    }

but I would like something like:

public function it_does_not_get_tracked_for_an_invalid_request(ApiTester $I, Example $example)
    {
        $I->sendPOST('/useractivity', $example->toArray());
        $I->seeResponseCodeIs(422);
    }
5
  • Example $example means $example is an object of class Example but you are using it as an array $example['timestamp'] so I have no idea what you're doing here. Commented Oct 23, 2017 at 15:19
  • 1
    However, $I->sendPOST('/useractivity', (array)$example); Commented Oct 23, 2017 at 15:20
  • @AbraCadaver Example allows to access properties this way. Please have a look at: codeception.com/docs/07-AdvancedUsage Commented Oct 23, 2017 at 15:20
  • @AbraCadaver :) nonetheless casting it actually works Commented Oct 23, 2017 at 15:22
  • Yes, I don't know codeception so maybe someone else will have a better idea. Commented Oct 23, 2017 at 15:23

3 Answers 3

1

I don't know Codeception, but casting is an option:

$I->sendPOST('/useractivity', (array)$example);
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry, it works not really!...

Trying to assertEqual an (array) $example and an array results in:

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

Comments

0

From PHP 7.4 you can use array spread operator. Since Codeception example object class (Codeception\Example) implements ArrayAccess interface you can use something like:

$I->sendPOST('/useractivity', [...$example]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.