不会被嘲笑的抽象方法

问题描述 投票:0回答:1

我有一个与此简化示例相似的设置(应该通过直接复制粘贴来工作),但是我要模拟的方法Controller->setModel()没有被模拟。也不会出现错误:

<?php

use PHPUnit\Framework\TestCase;

// Implementation
class Model
{
    // The constructor here would receive a PDO, but that's not important
    public $data = [
        ['id' => 1, 'text' => 'nope'],
        ['id' => 2, 'text' => 'no'],
        ['id' => 3, 'text' => 'not']
    ];

    public function getItem($id)
    {
        // And here we would query with the PDO instead, but again
        // the question is more about mocking the setModel() method
        return $this->data[$id];
    }
}

abstract class Endpoint
{
    public function __construct()
    {
        echo "\n1. Calling abstract class constructor\n";
        $this->model = $this->setModel();
    }

    abstract protected function setModel();
}

class Controller extends Endpoint
{
    public function getStuff($id)
    {
        echo "3. Getting stuff from controller\n";
        $data = $this->model->getItem($id);
        return ['id' => $data['id'], 'text' => $data['text']];
    }

    protected function setModel()
    {
        echo "2. Setting ACTUAL model of controller\n";
        $config = 'sqlite:myfile.sqlite3'; // Suppose this file exists and is valid
        $pdo = new \PDO($config);
        return new Model($pdo);
    }
}

// Test
final class ControllerTest extends TestCase
{
    public function testExample()
    {
        $mockModel = \Mockery::mock('FakeModel')
            ->shouldReceive('getItem')
            ->andReturn(['id' => 123, 'text' => 'myCustomText']);

        $mock = \Mockery::mock('Controller')->makePartial()
            ->shouldAllowMockingProtectedMethods();
        $mock->shouldReceive('setModel')->andReturn($mockModel);

        $controller = new Controller();
        $result = $controller->getStuff(2);
        $this->assertEquals('myCustomText', $result['text']);
    }
}

测试结果如下。 PDOException是因为php5-sqlite驱动程序不再适用于我的本地VM(Ubuntu 18.04)。我正在使用Docker映像进行设置,但是同样发生了模拟问题。但这不是重点-重点是模拟setModel()

1. Calling abstract class constructor
2. Setting ACTUAL model of controller


Time: 110 ms, Memory: 7.25MB

There was 1 error:

1) ControllerTest::testExample
PDOException: could not find driver

/home/juha/koodaus/pastes/backend/test/Foo/FooTest.php:45
/home/juha/koodaus/pastes/backend/test/Foo/FooTest.php:26
/home/juha/koodaus/pastes/backend/test/Foo/FooTest.php:63

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

如从第二个调试打印中看到的-Setting ACTUAL model of controller-模拟被完全忽略。也不出现错误或警告。模拟设置有什么问题?

我也尝试过使用overloadalias,但即使使用:也无法使用]

/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */

结果:

Mockery\Exception\RuntimeException: Could not load mock Controller, class already exists

但是我认为这是一个已知问题。

版本(不支持PHP 7的旧版设置:]

PHP 5.6.40PHPUnit 5.7.27模拟1.3.1

php unit-testing mockery
1个回答
0
投票

我认为您无法以所需的方式设置模拟。调用-> makePartial()时将调用construct方法。所以$ this-> setModel();总是返回null。

我会建议这种解决方案。

public function testExample()
{

    $mock = $this->getMockBuilder(Controller::class)
            ->setMethods(['setModel'])->getMock();

    $mockModel = \Mockery::mock('FakeModel');
    $mockModel->shouldReceive('getItem')
            ->andReturn(['id' => 123, 'text' => 'myCustomText']);

    $mock->method('setModel')->willReturn($mockModel);

    $mock->instuctModel();

    $result = $mock->getStuff(2);
    $this->assertEquals('myCustomText', $result['text']);
}

并且EndPoint抽象类应更改为以下一个

abstract class Endpoint
{
    protected $model;

    public function __construct()
    {
        echo "\n1. Calling abstract class constructor\n";
    }

    public function instuctModel(){

        $this->model = $this->setModel();
    }

    abstract public function setModel();
}
© www.soinside.com 2019 - 2024. All rights reserved.