I am trying to get into the good habit of testing my code, I did some exercise on the subject, but everything was so simple that, when I tried to apply what I learned on a real-world application I was extremely confused.
I'm writing a little PHP framework, using packages like Auryn, Zend-Diactoros and Fast-Route. I wanted to go heavy into interfaces to be able in future to swap dependencies with ease, so I wanted to write, for example, a RouterInterface that has a addRoute() and a match() methods, then what I just need to do is to write an adapter for the choosen package, to adapt it to my interface.
I wanted to use TDD to develop this framework, so I started with tests for my RouterAdapter class and I was totally lost. I can test a simple calculator class but how to test a class that depends heavily on a class that I don't really know how it works? I know that I have to store a RouteCollector object in order to store routes, I know that this object has some methods like getData() that returns an array of arrays, but how do I test a method like this one:
public function addRoute($methods, string $pattern, $handler)
{
$this->routeCollector->addRoute($methods, $pattern, $handler);
}
the class's constructor may be something like that
private $routeCollector;
public function __construct(RouteCollector $rc)
{
$this->routeCollector = $rc;
}
My test should check that in the routeCollector Object a route was added, but this requires to run checks on a private property inside my class. Maybe is easier than I thought, but right now I don't really have a valid solution to this, other than write utility methods to get the data stored in the object, something like this:
public function getRoutes()
{
/*
I want just the array in the first position because that array
is the array where are stored not-named routes.
*/
return $this->routeCollector->getData()[0];
}
Maybe I'm overthinking too much, this way of coding is really new for me