I’m trying to understand which part of my code I should to test. I have some code. Below is example of this code, just to understand the idea.
Depends of some parametrs I put one or another currency to “Event” and return his serialization in the controller. Which part of code I should to test? Just the final serialization, or only “Event” or every method: getJson, getRows, fillCurrency, setCurrency?
class Controller {
public function getJson()
{
$rows = $eventManager->getRows();
return new JsonResponse($rows);
}
}
class EventManager {
public function getRows()
{
//some code here
if ($parameter == true) {
$this->fillCurrency($event, $currency);
}
}
public function fillCurrency($event, $currency)
{
//some code here
if ($parameters == true) {
$event->setCurrency($currency);
}
}
}
class Event {
public function setCurrency($currency) {
$this->updatedAt = new Datetime();
$this->currency = $currency;
}
}
3
Depends on what kind of testing you want to do. There are unit tests and integration tests.
Unit Tests are tests where you test individual units of your application. Normally, this refers to classes. Integration tests are tests where you test how each class/method/api/interface interacts with each other.
With Unit Tests, As stated in “The Art of Unit Testing” by Roy Osherove
You should test every method that deals with any form of logic. That means you shouldn’t bother testing getters and setters unless there is a chance that calling Object.setValue(x)
does something more than just set the value of Object to x. For example, checking if the value is correct etc. So here is some code example
boolean setValue(Object x) { value = x; }
boolean setValue(Object x) { if(typeof x == Integer) value = x else value = null}
The first one isn’t worth testing since you’re sure that value will always be x. The second on the other hand, is worth testing because there is a chance that the value might be set to null instead of x.
You can watch some of Roy’s classes/talks here: http://artofunittesting.com/
1
I think you should test:
– Event.setCurrency: Are the date and currency in the instance correctly set? (This can probably left out since it is just a setter with no data handling, but in case this changes the test should be in place)
– EventManager.getRows: Does it return proper JSON?
– Controller.getJson: Does it behave correctly if getRows returns NULL or 0?
I would not write an explicit test for EventManager.fillCurrency since in the end it is just passing on values.