Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify the mocking of table classes #18704

Open
@bancer

Description

@bancer

Description

At the present time there is a method
public function getMockForModel(string $alias, array $methods = [], array $options = [])

Too much boilerplate code, the method name is repeated, the alias is a magic string:

$mock = $this->getMockForModel('My', ['delete']);
$mock->expects($this->once())
        ->method('delete')
        ->willReturn(false);

I would suggest something like this:
public function mockTable(string $className, array $options = [])
so that it would be possible to simplify the mocking:

$mock = $this->mockTable(MyTable::class)
    ->method('save')
    ->willReturn(false);

and in case of multiple methods to be mocked:

$mock = $this->mockTable(MyTable::class);
$mock->method('save')->willReturn(false);
$mock->method('delete')->willReturn(false);

or

$mock = $this->mockTable(MyTable::class)
    ->method('save')
    ->willReturn(false)
    ->method('delete')
    ->willReturn(false);

or something similar but as simple as this.

If the alias or the table class name violates naming conventions:

$mock = $this->mockTable(MyTable::class, ['alias' => 'MyAlias'])
    ->method('save')
    ->willReturn(false);

CakePHP Version

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions