Skip to content

Concerning default function/method parameters #636

@slaff

Description

@slaff

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

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'));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions