Reduce, Reuse, and Recycle!
You probably already recognize Laravel as a flagship framework in the modern developer ecosystem. One of the best macro-features that Laravel offers to developers is various abilities for ‘World Building’.
Exceptional world building, or application initialization, is seen in the two spheres of testing and actual data.
Today I will quickly display how world building can vastly improve your application tests. The inspiration for this article is coming from two places: beloved Laracasts, and an article from Tighten ( at https://tighten.co/blog/tidy-up-your-tests-with-class-based-model-factories ). To learn this on a more in-depth level, either follow those two sources or contact me for deeper training.
As the title from the Tighten article says, we are going to be creating our test environment using a Factory Class ( Facade ). The significance of this is as such:
3 step process:
- create and sign-in a user
- create a project, owned by the authenticated user
- give the project a task
Under the hood of the Factory Class, the same operations are being done. However, it is all being abstracted into one location. This is where the real significance lies. Instead of having to repeat the same code in every. single. test. function.. you can dynamically handle creating the necessary test environment. After all, code re-usage and efficiency is a cornerstone of programming.