Background
I have a system with a microservices setup. A few of these microservices run a laravel installation. In order to share some key models, a repo was shared using git/packagist.
Here is a diagram:
Microservice A
Microservice B
- …
These both share Library C
. This library has the shared models. This is outside of a normal laravel installation, but the composer includes "laravel/framework": "^9.0"
.
Note: There good external reasons to share the functionality – the microservices have come out of a monolith and are still developing fluidly and are not mature enough for a complete decoupling. This will come in time.
I wish to unit test these models.
Specifics
The requirement is that several models (User
, Customer
.. etc) all require addresses. Normalising these out would introduce complexity elsewhere that is not appropriate yet, so a trait is good for now. These have UK postcodes that require a specific validation against a database. Postcodes are modelled using a Postcode
model.
I created a trait : AddressTrait
. This offers some useful functionality. Included in this is a Postcode validation. This intercepts a set request in laravel (eg: $user->postcode = 'AB10 1AB
)
/**
* Automatically updates the log/lat from the postcode
* @param $value
*/
public function setPostcodeAttribute($value): void
{
// update postcode
$this->attributes['postcode'] = strtoupper($value);
// now update lat/long
$postcode = Postcode::where('pcd', '=', str_replace(' ', '', $value))
->orWhere('pcd', '=', $value)
->first();
if ($postcode) {
$this->attributes['latitude'] = $postcode->latitude;
$this->attributes['longitude'] = $postcode->longitude;
}
}
This works as expected.
Note – it is to be extended quite a bit further with much more complexity, but this is step 1 and completely represents the problem.
Testing
If I interact with the postcode attribute, such as $user->postcode = 'AB10 1AB
, this attempts to load the Postcode
from the database, and the following error occurs:
Error : Call to a member function connection() on null
^ This is expected.
I would like to unit test this: ie. no reaching out the class and mocking system/functional elements. Thus, I need to mock the Postcode
load (Postcode::where(..) ..
).
As this is a static call, I have used mockery ("mockery/mockery": "dev-master"
).
Here is the current attempt:
// ...
use Mockery;
use PHPUnitFrameworkTestCase;
// ...
public function testPostcodeProcessing(): void
{
$postcode_value = 'AB10 1AB';
$postcode_content = [
'pcd' => $postcode_value,
'latitude' => '0.1',
'longitude' => '0.2'
];
$mock_postcode = Mockery::mock(Postcode::class);
$mock_postcode->shouldReceive('where')->once()->andReturn($mock_postcode);
$mock_postcode->shouldReceive('orWhere')->once()->andReturn($mock_postcode);
$mock_postcode->shouldReceive('first')->once()->andReturn($postcode_content);
$model = $this->createTraitImplementedClass();
$model->postcode = $postcode_value;
}
protected function createTraitImplementedClass(): Model
{
return new class extends Model {
use AddressTrait;
};
}
TLDR question
I would like to unit test this function: ie. no reaching out the class and mocking.
- How do I mock a laravel/eloquent static call, given that:
- this is to be tested outside laravel
- there is no database connection
OR
- How do I refactor this to allow it to be more testable
Super TLDR;
How do I mock the load in:
public function tldr(): void
{
// this eloquent lookup needs to be mocked (not moved, refactored etc etc..)
$postcode = Postcode::where('pcd', '=', 'AB10 1AB')->first();
}
Notes:
- These are unit tests
- I would prefer to do this "the laravel way", but given the unusual circumstances things such as
mockery
might make sense - May be a gotcha: I am using the phpunit
PHPUnitFrameworkTestCase
– not the usual PHP test case. This is not a "requirement", but I imagined a mock shouldn’t need the extended features.
Any help with this would be appreciated!
3
Answers
After some extensive looking into this, I've found the answer using mockery aliases. This is done as follows:
Isolate this class/test from the remainder of the tests
If you create an alias, this overwrites the class globally for the rest of the current process. It's risky, but this can be done and many of the problems sidestepped by running the test/class in a separate process.
This can be done using the docblock:
Mock the class as an
alias
Aliases mock static classes. This is the key point I was missing during my question - I missed the
alias:
part.The above mock will override ALL Postcode classes in this test/test class. Thus, it should be declared first.
Add your responses and assertions
This is entirely up to you, but here is the example and assertions I created.
Note - these are "step 1" tests. The real class is more complex, and the test will be more complex. However, this gives the core solution to the instantiation issue.
TLDR;
alias:SomeClass
)What if you abstracted away the part where you get the postcode?
If you do it like this, you no longer need to mock Eloquent Query builder at all. Partially mocking a class that uses that Address trait should give you what you need. I’m not sure if this works for anonymous classes though
Although I wouldn’t recommend it, if you had a static method inside the Postcode model.
You could mock it with
I’m not sure if
expects()
works, but if it does, you can also write this asImportant: for this to work, the
Postcode
class should not have been loaded (by this or any previous tests). It’s that fragile.You can make your method more test friendly
like this