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

[5.4] Use composition to create TestResponses instead of inheritance #18089

Merged
merged 4 commits into from
Feb 24, 2017
Merged

[5.4] Use composition to create TestResponses instead of inheritance #18089

merged 4 commits into from
Feb 24, 2017

Conversation

adamwathan
Copy link
Contributor

Fixes #18049 by allowing a TestResponse to be created from any type of response, instead of being coupled to responses that support getContent().

The real underlying cause for that issue is that Symfony's BinaryFileResponse extends their base response, even though it's not actually substitutable. Inheritance is stupid 👍🏻

This does introduce one potential problem (although it seems unlikely...)

Since TestResponse no longer extends Response, it won't pass any Response type hints. I find it extremely unlikely that this will affect anyone, because it seems very odd to get the $response variable back as a result in a test, and then pass it into something that's type hinting the base response, but this change would break that code if it exists anywhere.

I've only implemented __get and __call here because again it seems odd that anyone would try to mutate a property the returned response, but can add that if there's some reasonable justification for it.

I've left the $baseResponse property public, so it would be trivial for end users to grab that directly and manipulate it themselves if needed for whatever reason.

Fixes #18049 by allowing a TestResponse to be created from any type
of response, instead of being coupled to responses that support
`getContent()`.
@adamwathan adamwathan changed the title Aw test response composition [5.4] Use composition to create TestResponses instead of inheritance Feb 23, 2017
@taylorotwell taylorotwell merged commit 3f4bec6 into laravel:5.4 Feb 24, 2017
@REBELinBLUE
Copy link
Contributor

@adamwathan is this change likely to be the cause of my tests now failing? https://twitter.com/stephendball/status/837055367565701120 I am using https://github.com/robclancy/presenter so the controllers return \Robbo\Presenter\View\View which extend View

@adamwathan
Copy link
Contributor Author

Hey @REBELinBLUE I hope not! Is there any chance you could push up some sort of public repo that reproduces your issue? Happy to take a look and see if I can help track down the problem.

@REBELinBLUE
Copy link
Contributor

No worries, didn't think it would be from looking at the diff of the changes. It was just the most obvious thing from the change log so I thought I'd maybe misunderstood it. I'll just continue to play around. Thanks

@REBELinBLUE
Copy link
Contributor

I'm thinking it may be related because ensureResponseHasView in TestResponse checks that $this->original is set, which was previously set in fromBaseResponse and now doesn't seem to be set anywhere that I can find? The particular test I was running is https://github.com/REBELinBLUE/deployer/blob/refactor/cleanup_console_commands/tests/Integration/Resources/CommandControllerTest.php#L25-L46

@REBELinBLUE
Copy link
Contributor

REBELinBLUE commented Mar 1, 2017

https://github.com/REBELinBLUE/laravel-bug yeah exactly the same on a base install just adding assertViewHas to the example test. I'll start a fresh issue

@adamwathan
Copy link
Contributor Author

Thanks for that @REBELinBLUE ! PR is up:

#18182

@REBELinBLUE
Copy link
Contributor

REBELinBLUE commented Mar 2, 2017 via email

@osteel
Copy link

osteel commented Apr 19, 2017

Hi,

Just to give you a use case for which this update breaks the tests: when using third party packages such as Swagger Assertions, which type-hints response objects and expects them to either extend Symfony's response object or at least implement PSR-7's response interface.

Cheers

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 this pull request may close these issues.

4 participants