Here is my PHPUnit test (PHPUnit 10.5.2 Mockery 1.6.11)
public function testMergedDocumentsWithSuccessfulInit() {
$params = new BuanRequestParameters([], [], [], []);
$mockAdminView = m::mock(AdminView::class);
// The mocking of init method will consider the inheritance from IasAdminController.
$controller = m::mock('DocumentsController', [$params])
->makePartial()
->shouldAllowMockingProtectedMethods();
// Mock the 'init' method which is inherited from IasAdminController
$controller->shouldReceive('init')
->with($mockAdminView)
->andReturn(true)
->once(); // Ensure the init method is mocked to return true once
echo "Mock setup complete, testing init calln";
$initResult = $controller->init($mockAdminView); // This should print true as the init method is mocked
echo "init called, result: " . ($initResult ? "true" : "false") . "n";
// Execute the mergedDocuments method and check the result
$result = $controller->mergedDocuments();
This is the start of the method I’m testing on my DocumentsController:
public function mergedDocuments(): AdminView
{
// Init
$view = new AdminView();
$initResult = $this->init($view);
if (!$initResult) {
return $view;
}
The ‘init’ method is defined in the AdminController which the DeveloperController extends e.g.
class DocumentsController extends IasAdminController
The DocumentsController is in the global namespace while the AdminController is in Ias (not sure if this is relevant).
Whatever I set “->andReturn(…)” to, the return $view is always reached i.e. the ‘init’ method is not being intercepted and runs as normal, always returning false.
protected function init(AdminView $view, bool $bypassAuthCheck = false): bool
My question, is why is the init method not being intercepted?
I was expecting by setting the ‘andReturn’ to false that the early return would not be reached in my mergeDocuments method.