Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPUnit problems with getallheaders() #72

Closed
adriansuter opened this issue May 2, 2019 · 9 comments
Closed

PHPUnit problems with getallheaders() #72

adriansuter opened this issue May 2, 2019 · 9 comments
Assignees
Milestone

Comments

@adriansuter
Copy link
Contributor

In #70 the file tests/Assets/PhpGetAllHeaders.php had been removed. This breaks PHPUnit on my system (Win10, PHP 7.1), because the following condition

if (function_exists('getallheaders')) {

always returns false (even if the function is defined in the Slim\Psr7 namespace via the file tests/Assets/PhpFunctionOverrides.php).

Test Output

There were 4 failures:

1) Slim\Tests\Psr7\Factory\ServerRequestFactoryTest::testCreateFromGlobals
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'application/json'
+''

D:\GitHub\Slim-Psr7\tests\Factory\ServerRequestFactoryTest.php:82

2) Slim\Tests\Psr7\Factory\ServerRequestFactoryTest::testCreateFromGlobalsWithParsedBody
null does not match expected type "array".

D:\GitHub\Slim-Psr7\tests\Factory\ServerRequestFactoryTest.php:110

3) Slim\Tests\Psr7\Factory\ServerRequestFactoryTest::testCreateFromGlobalsParsesBodyWithFragmentedContentType
null does not match expected type "array".

D:\GitHub\Slim-Psr7\tests\Factory\ServerRequestFactoryTest.php:172

4) Slim\Tests\Psr7\HeadersTest::testCreateFromGlobals
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
-    'accept' => Array (...)
 )

D:\GitHub\Slim-Psr7\tests\HeadersTest.php:27

It seems that only failures 1 and 4 are directly connected, as the other failures remain even if I add the function getallheaders() to the root namespace.

@adriansuter
Copy link
Contributor Author

It appears as if the method function_exists() is searching in the root namespace only, if there is no namespace prefix at all.

I made the following test:

<?php

namespace ns;

function test() {
	return '1';
}

if ( function_exists('test') ) {
	echo test();
} else {
	echo '0';
}

if ( function_exists('ns\test') ) {
	echo test();
} else {
	echo '0';
}

The result was 01. So the first condition was false, the seond true. Is this as expected? If so, then we need to make sure that in case php does not define getallheaders, we define it in the root namespace.

@l0gicgate
Copy link
Member

So we should probably append a backslash to the function name like so:

In Headers::createFromGlobals()

    public static function createFromGlobals()
    {
        $headers = null;

        if (function_exists('\getallheaders')) {
            $headers = \getallheaders();
        }

        if (!is_array($headers)) {
            $headers = [];
        }

        return new static($headers);
    }

@adriansuter
Copy link
Contributor Author

adriansuter commented May 2, 2019

But then we cannot override this method for unit testing.

I think we should make sure, that the function does exist in the root namespace. And then use the Slim/Psr7 namespace to override.

@l0gicgate
Copy link
Member

I think we can just do function_exists('\getallheaders') and then use $headers = getallheaders()

@adriansuter
Copy link
Contributor Author

If we check for function_exists('\getallheaders'), then the phpunit tests would not run correctly on systems without getallheaders(), because the condition would be false on these systems (but the test case expects this condition to be true).

@l0gicgate
Copy link
Member

l0gicgate commented May 2, 2019

could do function_exists('\getallheaders') || function_exists('Slim\Psr7\getallheaders')

@adriansuter
Copy link
Contributor Author

Then we insert a condition into the library source code that is actually only used by unit testing. I think there is no other way: we need a polyfill to make sure that the function exists on the root namespace. Either we write our own, or define one in composer require-dev.

@l0gicgate
Copy link
Member

l0gicgate commented May 2, 2019

@adriansuter
Copy link
Contributor Author

That's a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants