While being in the process of developing multiple packages (inside workbench), I found myself in the situation, where I needed to create unit tests for my Eloquent models - however, this has caused a variety of issues for me.
First, I cannot simple create new instances of my models, because they are unable to connect to a given database (which I have specified inside my "testing" environment)... Naturally, at this point I realised that "laravel" itself wasn't start, during my tests. So, I began to research how Laravel tests such models, etc.
I started using Codeception for creating tests, but found that I couldn't settle for making "functional" tests, for my models, but still needed proper "unit" tests for certain things (Also, there is yet not an official L5 Module for Codeception). Thus, still no clue on how to resolve my need.
Illuminate\Foundation\Testing\TestCase
After a while, I took a closer look at how unit-testing was performed, at the top-application-level, and found that I might be able to extend that given Testcase - Yet, no such luck. The Foundation-namespace, it not available as a separate package, and I was "forced" to include the entire L5 framework, inside my given workbench-package's vendor folder! This didn't seem like any good solution either!
Custom TestingTrait
Still being inspired by how the Foundation\Testing\TestCase worked, I decided to create my own testing-trait, one which could initialise the application and use the environment-specific configuration. However, this resulted in some major issues:
- I still needed the entire L5 framework inside my package's vendor folder
- Initialising the Illuminate\Foundation\Application still didn't read the configuration
- Attempting to access anything from the framework, e.g. Artisan::call(), didn't work either. The Facedes fail in line 208 (in short - they do not understand what/where to find artisan or anything else). This means that neither the aliases or default service providers are loaded / configured correctly.
In other words, anything that was Laravel-specific I couldn't use nor test from inside the workbench. This is most likely because the application has been initialised in the correct order / way. Yet, while this appears to be working in the top-level, it fails inside the workbench.
Other solutions
I searched the web for similar issues, yet it would seem that most people are settled for using Codeception's L4 tests - making use of "functional" tests - or simply stick to tests at the application-level. Neither are good enough solutions for me.
Did also find this package (https://github.com/orchestral/testbench#working-with-workbench). But after a closer look at his implementation, I got scared; the hard-coded application aliases and providers (Orchestra\Testbench\Traits\ApplicationTrait), might not always be in sync with L5. In case anything changes inside Laravel, his packages might not be up-to-date, causing in yet another dependency to worry about.
But how to test Eloquent, when working inside workbench packages
To sum up, I am still confused on how I can unit-test Eloquent models, when local inside my workbench packages.
Is there anyone that have a simple solution, which doesn't entail hacking the living crap out of L5, or having to depend on several other packages, just to gain access to Laravel's core features, such as Facades, Db?