After much looking around on Stack and searching around for the past 2 hours, I'm completely lost. None of the questions I find describe my issue in the slightest.
These questions include these:
PHPUnit mocked method called 0 times while it should be called once
PHP Mockery not calling mocked method from within mock
Laravel 4 Mocking Issue : should be called exactly 1 times but called 0 times
I'm working on a project that must reserve IP addresses with a given network. When a network is created, all IP addresses within the given range must also be created. For this I'm using a repository.
To test if the implementation works as expected, I'm mocking the instances, which are the injected into my controller.
During the tests, 2 methods of the repository are called. The relevant methods are shown below:
/**
* Create a new IP Address
*
* @param array $properties
* @return IP
*/
public function create(array $properties)
{
return IP::create($properties);
}
/**
* Create an IP range
*
* @param array $range
* @param array $baseProperties
* @throws DuplicateIPException
*/
public function createRange(array $range, array $baseProperties)
{
$properties = [];
try {
foreach ($range as $ip) {
$properties = array_merge($baseProperties, [ 'ip' => $ip ]);
$this->create($properties);
}
} catch (\PDOException $e) {
throw new DuplicateIPException($properties['ip']);
}
}
These methods are mocked like so:
$IPRepoMock = $this->mock(IIPRepository::class);
$IPRepoMock->shouldReceive('createRange')->once();
$IPRepoMock->shouldReceive('create')->times(256);
And the controller method that's being tested is the following:
public function store(NetworkFormRequest $formRequest, IPRangeCreator $rangeCreator)
{
$redirect = redirect('/network');
try {
$network = $this->networkRepo->create($formRequest->all());
$range = $rangeCreator->createRange($network->networkaddress, $network->netmaskbits);
$this->IPRepo->createRange($range, [
'network' => $network->network,
'zone' => null,
]);
$redirect->with('success', 'Network has been created.');
} catch (DuplicateIPException $e) {
$redirect->withErrors([ 'ip_overlap' => $e->getMessage() ]);
}
return $redirect;
}
The expected result is that the test would pass without any issues. This is not the case however, as I'm getting the following error:
Method create() from Mockery_1_IIPRepository should be called exactly 256 times but called 0 times.
This is what I find strange, considering that, during this test, the create() method is called exactly 256 times. (I figured this out because it actually inserted the data once by accident due to a wrong setup)
Aucun commentaire:
Enregistrer un commentaire