Answer the question
In order to leave comments, you need to log in
Test-driven development - how not to break encapsulation?
I want to try to move to test-driven development, and I have a number of questions.
The most basic question is how to respect data encapsulation and tests?
Well, for example, if I write some kind of bot that runs around the site and counts the number of tags on the page. The bot has a constructor and a couple of methods. Well, for example, I do not need to use the methods in the external environment, and I make them private. For example, I have a private method that collects a full url address (for example, it should cut anchors, javascript:, make site.ru/test from /test , etc.), I need to test it, but since it is private, I don’t I can do it.
There is an option to make all the necessary methods for testing public, but there we hit encapsulation, and if we are talking about some kind of harsh api, where you can get hit on the ears for this, then this is not an option.
If you think that you can take a test class and use the class under test as a parent class, then there are 2 problems. Encapsulation doesn't suffer as much (we use protected methods), and in phpynit the test classes already extend the PHPUnit_Framework_TestCase base class .
I would not want to start learning this approach somehow wrong. I read a book about tdd, but there this matter was not particularly touched upon and I could not understand.
So what to do?
Answer the question
In order to leave comments, you need to log in
Well, for good, we should not cover private methods with tests, only public interfaces. But if you really want to, then any private method can be made public in tests:
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question