Using tools files to test models
On my latest project, I'm building an advertisement system. Normally, I create my models, but then test them using the dashboard pages and blocks. On my current project, I used a tools file to test out all of the models before trying to build a GUI. It's made development much smoother so far.
For the most part, the applications that I write (beyond simple blocks) involve list and object models. My Custom Objects Demo is a good example of how this works. It's kind of the basic work flow for doing any kind of 'thing' management in concrete5.
Separating working on the data models from the GUI for editing them is the hard part. Keep reading to learn how everything worked.
What I'm building now is an advertisement system, so I have Section, Advertisement, and Advertisement List models. Ads are tagged to sections, and then filtered. The section has a getNextAd() function that makes sure that the ads all display equally. All of your typical things that need to happen. Then there will be dashboard pages to manage the ads and sections, and a block for the front end that will output the ads.
In the past, what I had done is to stub in all of the pages, models, and blocks first. Trying to keep them all straight, realizing I need to refactor something, so many places where things fit together... It's a whole lot of code just to start working. But it's not easy to do unit tests in c5, basically because of where all those pieces fit together.
There is a solution!
One of the really useful things in concrete5 is tools files. You can hit a URL and do pretty much anything that you want to do. Typically they are used for ajax requests.
One thing about having time off from working because of this injury has been that I've had a lot of time to think about how to build things. Stewing things over instead of just writing code and being caught up in actual programming was a good escape. It's kind of like how I was actually a stronger rider and had less pain after 2 months away from the bike last winter. They say that training is not about just pushing yourself. It's about rest, it's about recovery.
After a month not really doing anything, I pretty much had everything planned out. I knew what each model needed to do, and I'd come up with the idea of making sure I could do all the data first. It made a whole lot of sense to me, as I'd looked back on my previous work, I realized where a lot of my errors were coming up. It was from things like a syntax error calling a model from a view or a controller, typos in database tables, etc. Re-factoring code in the database and the model would also cause cascading errors in the controllers and views.
I ended up with something that looks a bit like this:
And then to run the code, I just check it against the local domain:
Basically, I'm just going through and testing every case in my models. All the way through creation, filtering, and outputting. I used manual database adjustments to check things for the output. The advertisement and section models either return their class, or an error object depending on result.
You want to make sure that you comment out the call to create something each time that you test another variable. The $error object is a singleton, so all the errors from every time it's called show up across your script.
It was really great to be able to just go through and systematically test each point, and getting all of the data objects working together took a lot less time. I need to do some more testing on the list filter functions, making sure that I can filter by date and a few other things. But the process would be just the same for that. I can make sure that all of my data is working together before building the rest of it.
I'm sure that this is kind of stupid stuff to some developers, but this change to the process should help me out a lot. To have sample code for every function I need, and know they all work?
Everything else will be kind of an after thought.
It's great to have this idea of how to make a fully integrated system like this. It ties into the Best Suite - Core system in a way that really focuses how I want everything to look from a dashboard side. Every project that I've been doing is a step further forward towards the final. I can't wait to see what the next few months bring.