-
Notifications
You must be signed in to change notification settings - Fork 46
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
UploadedFileFactory - Failing to create a readable UploadedFile from string #100
UploadedFileFactory - Failing to create a readable UploadedFile from string #100
Conversation
The problem might be, that the temp stream created, fails to be readable in
A
While @l0gicgate Do you have any idea why this happens? |
I just changed Slim-Psr7/src/Factory/UploadedFileFactory.php Lines 32 to 34 in 6e32929
into if (!is_string($file)) {
throw new InvalidArgumentException('File is not readable.');
}
if ($stream instanceof Stream) {
if (!$stream->isReadable()) {
throw new InvalidArgumentException('File is not readable.');
}
} elseif (!is_readable($file)) {
throw new InvalidArgumentException('File is not readable.');
} Now, the contents of the uploaded file seems to be empty. |
In
we would actually pass php://temp as $file to the constructor.
But later on, in Slim-Psr7/src/UploadedFile.php Line 106 in 6e32929
Maybe that's the issue? |
Sorry, I forgot to provide those details: Yes, the file is not readable at the moment of the check, but even if you fix the check the stream content will be empty, that is what I found out. Anyway the same happens in the nyholm implementation (file is not readable in the constructor), but the stream content returns as expected. Tough I have not investigated further. |
So I managed to fix this particular issue by allowing to create an instance of The issue is this:
PHP |
Exactly. Maybe wee need to add the stream as optional param to the uploaded file creator. What are your opinions? Or as a new setter method to avoid BC. |
My opinion? I'd prefer to modify the P.S.: I was wrong with the BC break - the classes that extend The tests that this modification breaks are in fact only those that expect an exception being thrown:
(except for the last one with file size, i'll investigate that one) So if you're asking me, I'd go down this way and finish this PR, possibly altering the tests that expect the exception and/or move the "is readable" check elsewhere (e.g. to the |
Hi @dakujem I agree. Probably we should use Regarding the "get size" test case: It seems that the slim integration of Slim-Psr7/tests/Integration/UploadedFileTest.php Lines 23 to 28 in 6e32929
I changed that in my branch addressing this subject adriansuter@c40aebf |
@adriansuter Shoul I continue to solve this issue? I'd most probably need to update the tests so that they do not expect exception at the time of calling |
@dakujem Why do you postpone those checks? Couldn't we leave them in If you don't like to continue to work on this issue, I could PR my branch https://github.com/adriansuter/Slim-Psr7/tree/patch-uploaded-file-temp-stream instead |
I can continue, I just was not sure you liked the changes... would not want to waste time. For reason I have yet to discover, the "is readable" check will always fail for This makes the test sequence used in the This is why I tried to postpone the test to the moment the stream is actually used. |
See #100 (comment) for infos about the "is readable" check. In the end, the |
Okay, I will try to work it out with your version of the factory. Of course I plan to squash and polish everything before marking the PR ready, obviously no need for nyholm nor the related test case :-) |
So I prepared this, but I have one question before I mark this ready: if ($stream instanceof Stream) {
if (!$stream->isReadable()) {
throw new InvalidArgumentException('File is not readable.');
}
} elseif (!is_readable($file)) {
throw new InvalidArgumentException('File is not readable.');
} ... well should we care about the implementation? I believe passing if (!$stream->isReadable()) {
throw new InvalidArgumentException('File is not readable.');
} However, with this one test ( |
I managed to update the test "prophecy" so the test does not fail. IMHO this is the way the factory should be implemented (independent of the particular implementation of the EDIT: cleaned and updated the code so the CS fixer stays cool. @adriansuter any thoughts? |
This may be due to loosening the UploadedFile parameter type restriction. How can I/we improve the coverage? I believe both cases when a stream and a filename is used are tested. Maybe the case where the exception is thrown is not. Let me see... |
@dakujem Yes, we should add a test case for when the |
Done. |
At a first glance, your PR looks goot to me. Although I have to check it in more detail. Thanks already for your work. |
Got a towel with me all the time... You're welcome. Glad to help. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure that the returned value of getMetadata()
is a string
and if not, throw an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the docblock can be removed.
I may have messed up, I'm not sure how to mark the changes "done". |
- UploadedFile::__construct method's first argument can now be an instance of StreamInterface - UploadedFileFactory now passes the stream into UploadedFile's constructor instead of the file name
While I was updating Mika Tuupola's
tuupola/http-factory
to support this package, I stumbled upon this bug. The tests won't pass forslim/psr7
as can be seen here tuupola/http-factory#10, while they do pass for the other PSR-7 implementations.I was trying to solve it for a couple of hours, but failed, so for now I'm just providing the failing test and a temporary comparison to
nyholm/psr7
that does not fail.The code of the test is taken from
Interop\Http\Factory\UploadedFileFactoryTestCase
(http-interop/http-factory-tests
package).I will try a bit later again, but I suspect the authors will have much better insight into the matter.