Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Modified expectedHeaders from TableNode to be in format expected by asse... #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

victuxbb
Copy link

Explanation here:
#115

…ssertArrayContains.

Response->getHeaders() returns an object, assertArrayContains expect an array, changed for response->getHeaders()->toArray().
@Nek-
Copy link
Contributor

Nek- commented Feb 4, 2015

They are probably going to ask you to follow project code style:

  • $key => $value
  • [$value]

But looks good to me.

@victuxbb
Copy link
Author

victuxbb commented Feb 5, 2015

Great! no problem, if it's necesary I can change the code.
Thanks.

@PedroTroller
Copy link
Member

@victuxbb The problem is that your solution doesn't test header name. assertArrayEquals doesn't care about keys.

Maybe you can implement something like this :

/**
     * @Then /^the response should contains the following headers:?$/
     */
    public function theResponseShouldContainsHeaders(TableNode $headerTable)
    {
        if (null === $this->response) {
            throw new \RuntimeException('You must send a request before testing a response.');
        }

        $expectedHeaders = $headerTable->getRowsHash();
        $existingHeaders = $this->response->getHeaders()->toArray();

        foreach ($expectedHeaders as $key => $value) {
            if (false === array_key_exists($key, $existingHeaders)) {
                throw new \Exception(sprintf(
                    'No header names "%s" found. "%s" available.',
                    $key,
                    implode('", "', array_keys($existingHeaders))
                ));
            }
            $real = $existingHeaders[$key];
            array_map('trim', $real);
            sort($real);

            $expected = explode(';', $value);
            array_map('trim', $expected);
            sort($expected);

            $this->getAsserter()->assertArrayEquals($expected, $real);
        }
    }
@victuxbb
Copy link
Author

@PedroTroller I was expecting that assertArrayContains is checking the header name and the value and I see that it doesn't.

I was trying to understand the code in assertArrayContains but I think it's a little ofuscated :)
Also I was reading the phpspec associated but I can't understand what is really doing...
Maybe you can explain me...

On the other hand If we discard the use of assertArrayContains by assertArrayEquals your implementation is fine with a few modification:

The initial state of the arrays $expectedHeaders and $existingHeaders are for example:

Array
(
    [Content-Disposition] => attachment; filename="export.csv"
)
Array
(
    [Server] => Array
        (
            [0] => nginx/1.6.2
        )

    [Content-Type] => Array
        (
            [0] => application/force-download
        )

    [Transfer-Encoding] => Array
        (
            [0] => chunked
        )

    [Connection] => Array
        (
            [0] => keep-alive
        )

    [X-Powered-By] => Array
        (
            [0] => PHP/5.6.5
        )

    [Cache-Control] => Array
        (
            [0] => no-cache, private
        )

    [Date] => Array
        (
            [0] => Thu, 12 Feb 2015 17:19:24 GMT
        )

    [Content-Disposition] => Array
        (
            [0] => attachment; filename="export.csv"
        )

)

I changed this line because the 2 arrays for comparing with your code results in this format:

$expected = explode(';', $value);

Array
(
    [0] =>  filename="export.csv"
    [1] => attachment
)
Array
(
    [0] => attachment; filename="export.csv"
)

I think there's no need to make an explode of the value...

Finally the function can be:

/**
     * @Then /^the response should contains the following headers:?$/
     */
    public function theResponseShouldContainsHeaders(TableNode $headerTable)
    {
        if (null === $this->response) {
            throw new \RuntimeException('You must send a request before testing a response.');
        }

        $expectedHeaders = $headerTable->getRowsHash();
        $existingHeaders = $this->response->getHeaders()->toArray();

        foreach ($expectedHeaders as $key => $value) {
            if (false === array_key_exists($key, $existingHeaders)) {
                throw new \Exception(sprintf(
                    'No header names "%s" found. "%s" available.',
                    $key,
                    implode('", "', array_keys($existingHeaders))
                ));
            }
            $real = $existingHeaders[$key];
            array_map('trim', $real);
            sort($real);

            $expected = array($value);
            array_map('trim', $expected);
            sort($expected);

            $this->getAsserter()->assertArrayEquals($expected, $real);
        }
    }

For me is working fine with this code, but I see that there's isn't phpspec for the ApiContext for testing theResponseShouldContainsHeaders, it will be ok if I implement a test?

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
3 participants