Skip to content

Simplify the mocking of table classes #18704

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy