Respect / Rest

Thin controller for RESTful applications
http://respect.github.io/Rest
Other
604 stars 102 forks source link

Unit Tests #67

Open alganet opened 12 years ago

alganet commented 12 years ago

We currently don't have real unit tests, our tests are more functional/BBDish. I'm gonna write the proper units and keep the old tests as legacy, already started!

alganet commented 12 years ago

I'll write up some possible fixes while writing unit tests, just reminders for ourselves in the future. My goal is to fix them after the tests for the current code.

alganet commented 12 years ago

Oops, didn't noticed @nickl- already started testing some things. Hey @nickl-, feel free to move any tests you want from tests/legacy/ to tests/library/.

I've started the tests using some conventions on tests, we should discuss them:

 [x] Is possible to construct using values from superglobals
 [x] Is possible to construct with custom method
 [x] Is possible to construct with custom uri
 [x] Absolute uris should be parsed to extract the path on constructor
 [x] Response is null without setting a route
 [x] Request is able to deliver a response without setting path params
 [x] Request is able to deliver a response using previously set path params

The idea is that each line on the testdox output should be descriptive enough to say how a specific unit behaves and what is being tested.

I've also added messages to all $this->assert lines, so when they fail the developer can understand better what the test is, instead of just blind-fix it to make it pass. Making the tests more understandable is a top priority.

I'm also exploring a nice idea from last night. We should be able to use test dependencies and data providers to explore combinatory tests (seems like the right choice for testing Routines, for example). It's a simple idea: a test is populated by a data provider, then another test is declared as dependent of the first one. For each provided data on the first, the second should run. I hope PHPUnit allows that someday, experiments so far on PHPUnit 3.6 were unsuccessful.

alganet commented 12 years ago

BTW, I'm starting with the tests for the Request class:

https://github.com/Respect/Rest/blob/develop/tests/library/Respect/Rest/RequestTest.php

alganet commented 12 years ago

Found these notes on PHPunit manual:

When a test receives input from both a @dataProvider method and from one or more tests it @depends on, the arguments from the data provider will come before the ones from depended-upon tests.

When a test depends on a test that uses data providers, the depending test will be executed when the test it depends upon is successful for at least one data set. The result of a test that uses data providers cannot be injected into a depending test.

All data providers are executed before the first call to the setUp function. Because of that you can't access any variables you create there from within a data provider

http://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html

augustohp commented 12 years ago

Great things going on! Let's try and focus on better tests as @alganet and @nickl- are already doing, let's get the points to refactor here on this issue while we decide what to do with them.

Go go go unit tests! I will try to focus on some Routines tests.

alganet commented 12 years ago

Finished the tests for the Request class with 100% code coverage (no side effects, always using mocks).

I've added a lot of helpers to create mocks and generators. Maybe these mocks could be reused in another tests, perhaps we should create something that extends the PHPUnit_Framework_Testcase.

alganet commented 12 years ago

I've commited some annotations for PHPunit, phpDocumentor (maybe, not sure) and Respect\Doc. Please see this issue guys: https://github.com/Respect/Doc/issues/16

nickl- commented 12 years ago

@alganet quoted phpunit documentation referring to @depends.

I also had exactly the same idea as you when I stumbled on this @depends:

a test is populated by a data provider, then another test is declared as dependent of the first one. For each provided data on the first, the second should run.

But alas it doesn't do any of that if anything at all as I couldn't even be sure that it actually runs test b before a. Also set-up and tear down does not work as previously advertised anymore and as far as the data providers go they seem to be loaded even before setup so you cannot even rely on the state.

I think wrapping is a good idea:

perhaps we should create something that extends the PHPUnit_Framework_Testcase.

We will need to ensure that it still runs as standard or configurable to include the custom wrappers from within the standard library I would imagine but the more control we can get for phpunit the better. I was also unable to make the --filter option work, for running a single test. I am not sure what is going on there. Differences between the text and html reports etc are the rewriting the tool or what?