-
I was wondering if you had some advice on how you structure your containers when you are unit testing something. Where we are at, we run an MVC type application, but we also have a "business" layer that we delegate things to. So for instance if this is overly simplified, we might have something like: class Company_Controller extends Controller {
public function showDashboard(ViewFactory $view, DatabaseFactory $db) {
// database calls to get some data for the dashboard
$data = $db->fetchAll("SELECT .....");
// maybe a method call to get some template variables
$template_vars = $this->getSomeTemplateVars();
// then call something that renders the view
$this->response->setBody($view->render('somefile.html', $template_vars));
}
} We like to delegate things to a sub layer to handle the business logic or rules for the application so those rules can be easily shared across controllers, so we might have something more like: class Company_Controller extends Controller {
public function showDashboard(ViewFactory $view, Company_Rule $Company_Rule) {
// get some template variables from the rule
$template_vars = $Company_Rule->getDashboardTemplateData();
$this->response->setBody($view->render('somefile.html', $template_vars));
}
}
// and the rule class
class Company_Rule extends Rule {
use ContainerAwareTrait;
public function getDashboardTemplateData() {
$data = $this->doDatabaseCalls();
return [ 'var1' => $data['some_count'], /* etc */ ];
}
public function doDatabaseCalls(DatabaseFactory $db) {
// database calls to get some data for the dashboard
return $db->fetchAll("SELECT...");
}
} So with that context, my question really becomes what your best recommendation is for handling the injection of these business rules that we have. Obviously putting a couple business rules in the container with Container::register() will work just fine, but what about 100 business rule classes? Or any mappers/orm that are assigned to the controller/business rule themselves? I'm assuming it would not be wise to basically load all of the business logic into the container, just so that it's easier to unit test in the long run with the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We were having some similar issues so we're currently testing a combination of unit/integration testing and end-to-end testing. We have unit and integration tests for critical components such as our access checking library while we do full end-to-end testing on api endpoints using either a in memory SQLite database or a dummy database. For our frontend (react/nextjs) we're using playwright for end-to-end testing and react testing library + mock service worker (intercepts and mocks backend requests) for unit/integration testing components. |
Beta Was this translation helpful? Give feedback.
-
Yep. Auto-wiring is supported as long as it's able to resolve the dependencies. |
Beta Was this translation helpful? Give feedback.
Yep. Auto-wiring is supported as long as it's able to resolve the dependencies.