Skip to content

Let methods be redefined #12

@stevegrunwell

Description

@stevegrunwell

PHPUnit has mocking tools built in, but they have the following limitations:

⚠️ Limitation: final, private, and static methods

Please note that final, private, and static methods cannot be stubbed or mocked. They are ignored by PHPUnit’s test double functionality and retain their original behavior except for static methods that will be replaced by a method throwing a \PHPUnit\Framework\MockObject\BadMethodCallException exception.

In PHP 7.x, we can get around this by using anonymous classes, but users still supporting PHP 5.6 are stuck.

To get around this, a Methods trait should be added with the following methods:

  • defineMethod(string $class, string $method, callable $function, string $visibility = 'public', bool $static = false)
  • redefineMethod(string $class, string $method, callable $function)
  • deleteMethod(string $class, string $method)

Careful attention should be paid to the visibility of methods, as well as whether or not they should be treated as static — Reflection will likely be used to handle these, as well as streamlining the definition of method arguments within Runkit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions