45130[FEATURE] Mocking of concrete methods in abstract classes
Commit message
Review
-
Code review CR +2
- Helmut Hummel +2
- Daniel Goerz +1
- Georg Ringer +1
-
Verified V +2
- Helmut Hummel +2
- Georg Ringer +1
Issues 1
Files 1
Review log 8
-
Stefan Rotsch uploaded patch set 1 12/04/15, 14:50
-
Markus Klein commented on patch set 1 12/08/15, 00:09
IMO this is not a feature, but rather an internal TASK, which opens up new possibilities. On the other hand I ask the question whether it would be better/easier to achieve things with prophecy now? Do you have a concrete use case for this change? -
Stefan Rotsch commented on patch set 1 12/08/15, 08:38
I must admit I have never heard about prophecy, I still spend my time with 6.2. For the use case, I was asked to extend a class inheriting from an abstract class AbstractLanguageSelectorViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { which contains a couple of concrete functions, like protected function isRootlineOfCountry(array $rootLine, $countryUid) { or protected function getCountryIso($uid) { The latter queries the database, so I had to mock it to be able to test my new code. It was easy to do this manually, but as it is only a small change I thought it would be nice to have it in the core, too. -
Markus Klein commented on patch set 1 12/11/15, 14:48
So you're now seeing how powerful unit testing is: It just revealed a major design flaw in the class structure ;-) Prophecy (supported since phpunit 4.8 or so) is a nicer tool to mock and stub classes, but it does not allow to partially mock a class, which you would need in your case. The reason for this is that one should only test against interfaces, aka public methods, in unit tests. Dependencies on other classes (like classes accessing the DB) can be mocked then without problems. In your case the correct solution would be to have the DB access stuff a dedicated class (eg repository) and you only mock this very class then, making the functions return your test data. Then you don't need to mock your "class under test" (which is not nice anyway) and you can already use prophecy, which is really easy. Still I do not block this patch, since it's a very easy one, but I also do not vote +1 here, since I consider it more important to have a good code structure than a magic test-infrastructure, which again needs internal information to be able to fulfil its purpose. -
Georg Ringer voted on patch set 1 CR +1 V +1 12/12/15, 19:09
-
Daniel Goerz voted on patch set 1 CR +1 12/12/15, 20:41
-
Helmut Hummel voted on patch set 1 CR +2 V +2 12/13/15, 13:44
-
Gerrit merged patch set 1 12/13/15, 13:44