Ik heb een class die een HTTP verzoek moet parsen: Request. Ik ben aan het testen met SimpleTest (http://www.simpletest.org).
Ik heb een aantal nette functies er in zitten, het voorbeeld is: $this->parseUrl($this->requestUri);
Dit is een functie die vanalles netjes uit de url haalt. Zo heb ik een stuk of 8 losse functie die duidelijk zijn in wat ze doen, duidelijk te testen op juist return.
De vraag:Die process() functie is slecht te testen, het doet van alles. Dus geen duidelijke specifieke taak. Eigenlijk vind ik dat dus maar niks. Aan de andere kant is het wel logisch $request->process() is te begrijpen.
Wat is de juiste manier: Of ik roep die functies allen gewoon los aan. Of ik behoud de process() functie maar test deze niet of alleen op Exceptions.
Het los aanroepen van de methodes lijkt me mooier maar dan krijg je het Sequential Coupling probleem. Daarnaast wordt de class erg public waardoor je bijna moet gaan snappen hoe je een Request verwerkt als je deze class aan gaat roepen. Dat is ook weer niet de bedoeling.
Wikipedia: Sequential coupling
Er zal niet echt 1 antwoord zijn maar wat is de best practice?
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| class Request { /* * Handles all needed processing for urls, methods etc */ public function process() { //Process HTTP accept so we know how to responds if(!$this->processHttpAccept()) { throw new HttpAcceptException('Authorisation failed.'); } //parse url if(!$this->parseUrl($this->requestUri)) { throw new UrlParsingException('Url could not be parsed.'); } //Process HTTP accept so we know how to responds if(!$this->processAuthorisation($_SERVER['HTTP_AUTHORISATION'])) { throw new AuthorisationFailedException('Authorisation failed.'); } //Process the request method and the data so we get it all in our request object return $this->processMethodAndData(); } //overige methoden even weggelaten. } |
PHP:
1
2
3
4
5
6
7
8
9
| //Aanroep van de class $request=new Request($_SERVER['REQUEST_URI']); //Configure the Request $request->setBasePath(BASE_PATH); //process whatever came in and get the response data back $request->process(); |
Ik heb een aantal nette functies er in zitten, het voorbeeld is: $this->parseUrl($this->requestUri);
Dit is een functie die vanalles netjes uit de url haalt. Zo heb ik een stuk of 8 losse functie die duidelijk zijn in wat ze doen, duidelijk te testen op juist return.
De vraag:Die process() functie is slecht te testen, het doet van alles. Dus geen duidelijke specifieke taak. Eigenlijk vind ik dat dus maar niks. Aan de andere kant is het wel logisch $request->process() is te begrijpen.
Wat is de juiste manier: Of ik roep die functies allen gewoon los aan. Of ik behoud de process() functie maar test deze niet of alleen op Exceptions.
Het los aanroepen van de methodes lijkt me mooier maar dan krijg je het Sequential Coupling probleem. Daarnaast wordt de class erg public waardoor je bijna moet gaan snappen hoe je een Request verwerkt als je deze class aan gaat roepen. Dat is ook weer niet de bedoeling.
Wikipedia: Sequential coupling
Er zal niet echt 1 antwoord zijn maar wat is de best practice?