zendframework / zend-expressive-skeleton

Begin developing PSR-7 middleware applications in seconds!
BSD 3-Clause "New" or "Revised" License
136 stars 90 forks source link

Suggestion for HomePageActionTest #86

Closed RalfEggert closed 8 years ago

RalfEggert commented 8 years ago

I wonder if it would make sense to use prophesized mocks for Zend\Diactoros\ServerRequest und Zend\Diactoros\Response as well in the HomePageActionTest?

https://github.com/zendframework/zend-expressive-skeleton/blob/master/test/AppTest/Action/HomePageActionTest.php#L23

A reason for this would be that the unit test for the action middleware should test its class functionality only and do not depend on request and response instances. What do others think?

geerteltink commented 8 years ago

Requests and responses are like the heart of Expressive and other PSR7 apps. Sure you can mock it, but requests can get complicated with forms and PSR7Sessions. I have wrapped it in a WebTestCase.

https://github.com/xtreamwayz/xtreamwayz.com/blob/master/test/WebTestCase.php#L71-L105

If you start mocking requests, you need to take care of all other middleware that messes with the request.

Also what happens if you mock the request and some middleware changes the request. Because requests are immutable, you have copy of the request and not the original mocked request.

RalfEggert commented 8 years ago

I understand. Well, I just want to write tests that test a single middleware. So it shouldn't matter for this test case when other middleware mess with the request. Mostly I need to test a middleware that access the request attributes. But they can be set in the test case without mocking as well.

weierophinney commented 8 years ago

@RalfEggert You can definitely use mocks. One thing that's a bit problematic, however, when mocking the PSR-7 instances is that the with*() methods are technically supposed to return new instances. I find it's generally okay to have them return the same instance, and that simplifies the test setup, but it can be confusing to newcomers, as they might then think those methods return the same instance, vs a new one.

The Diactoros instances, since they're value objects, are fairly cheap for testing against, and often easier to use than mocking. YMMV.