#636·prophecy

Concerning default function/method parameters

Author: slaffCreated Feb 20, 2025Updated Feb 20, 2025

I am trying to mock a method that has one mandatory and two optional parameters. I was hoping that if I define the mock method specifying only the mandatory parameter and the return value then this library will add also the optional parameters for me.

// public function hasRouteAccess(string $routeName, array $routeParams = [], string $method = 'GET') : bool;


$acl = $prophet->prophesize(AclInterface::class);
$acl->hasRouteAccess('privateRoute')->willReturn(false);

My expectation was that a check like this will also report "false"

$object = $acl->reveal();
var_dump($object->hasRouteAccess('privateRoute', [], 'GET'));

But instead I am getting back "null". Are my expectations wrong and is there a way to set up Prophecy to also auto-fill the default values for me.

A complete example is given below.

php
<?php

require_once __DIR__ .'/vendor/autoload.php';

interface AclInterface
{
    /**
     * Checks if the currently logged in user has access to a given resource
     * 
     * @param string $routeName
     * @param array $routeParams
     * @param string $method
     * @return bool
     */
    public function hasRouteAccess(string $routeName, array $routeParams = [], string $method = 'GET') : bool;
}


$prophet = new Prophecy\Prophet();

$acl = $prophet->prophesize(AclInterface::class);
$acl->hasRouteAccess('privateRoute')->willReturn(false);

$object = $acl->reveal();
var_dump($object->hasRouteAccess('privateRoute', [], 'GET'));