Question

I've got some view helpers (crossmodule), which need access to the database. A common example for this could be a NotificationHelper, which prints out a users notifications across many pages:

<div class="notifications">
<?php echo $this->notifications()->render(); ?>
</div>

While this is very handy, it seems very hard to do smokescreen testing / unit testing on controllers using AbstractHttpControllerTestCase, because the Helper would always want to access the database (which he obviously shouldn't, should he?). So in order to prevent this, I would need to mock the NotificationHelper. But that isn't enough, because a mock would return for $this->notifications(): null, and render() would therefore fail.

The only methods I can come up with, is to:

  • Mock all dependencies of NotificationHelper
  • Define return values of NotificationHelper, like (in this case) __invoke()
  • Detach the RendererListener, but this would cause that views wouldn't get tested in any way (it's sometimes usefull to see, if e.g. all <? echo $this->url('route'); ?> still work.

The first two methods seem very bloated and somehow wrong, as I would need to do this with every Helper in every ControllerTest.

The third method takes away some comfort, because view templates wouldn't get tested any more.

Is there a better way of doing this? Or is the whole process wrong? Or did I get the idea completly wrong?

Was it helpful?

Solution

AbstractHttpControllerTestCase can be used for functional testing, but is not supposed to help with real unit tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top