We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

ee('CP/Pagination') Requires second parameter for total, no way to set total later

Developer Preview

TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

ee('CP/Pagination') requires a second parameter of total, which makes it hard to inject it as a dependency into one of my classes where I don’t know the total yet. It basically means I have to call ee('CP/Pagination')right in one of my class methods, which isn’t great and makes testing hard.

Basically, ee('CP/Pagination')needs to not require the total parameter and needs a method to set it.

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

I take it the class you are trying to inject it into is not a controller, can you elaborate? Pagination is the type of thing I expect the controller class to do, after gathering whatever bits and bobs it will need for the presentation layer.

       
TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

It is a controller. In this case, I’m using the MCP file as essentially a router. So the method in my MCP file looks like this:

public function index()
{
 return ee('myaddon:MyController')->get();
}

My services key in addon.setup looks something like this:

'MyController' => function () {
 return new MyController(
  ee('CP/Sidebar'),
  ee('Model'),
  ee('CP/URL'),
  ee()->input,
  ee('CP/Table')
 );
}

And there in addon.setup is where I would like to inject the pagination service. I’m striving for everything in my classes to be injected so that they can be mocked and tested. Up until this I have been successful.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
8 years ago
Kevin Cupp's avatar Kevin Cupp

Typically, part of class design is requiring things the class absolutely needs to function so that you don’t instantiate it and have it be useless. Pagination is useless without knowing a total count. And mocking this many things is typically considered an anti-pattern, you may find that you’re spending more time wiring up and maintaining your mocks than testing the actual class logic, plus it’s a maintenance nightmare down the road. This is typically referred to as “mock hell.” Maybe an integration test is more in order here, or an MVVM architecture.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.