首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在用phpunit模拟PDOStatement类的单元测试中出现错误

在用phpunit模拟PDOStatement类的单元测试中出现错误
EN

Stack Overflow用户
提问于 2022-01-17 13:10:06
回答 1查看 206关注 0票数 1

由于下面的模型,我在启动单元测试时出错

代码

代码语言:javascript
运行
复制
$mockPDOStatement = $this->createMock(PDOStatement::class);

错误

代码语言:javascript
运行
复制
Error: Call to undefined method ReflectionUnionType::getName()

PHP8.1.1 PHPUnit 8.5.22

完整的例子:

代码语言:javascript
运行
复制
class Connection
{

    public function getPdo()
    {
        return new PDO(
            dbServer,
            dbUsername,
            dbPassword
        );
    }
}

class Events
{

    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    public function getEvent($id)
    {
        $query = 'SELECT * FROM events WHERE id=:id';
        $pdo = $this->dbConnection->getPdo()->prepare($query);
        $pdo->execute(["id" => $id]);
        return $pdo->fetchAll(PDO::FETCH_ASSOC);
    }
}

use PHPUnit\Framework\TestCase;

class EventsTest extends TestCase
{

    private $eventData = [
        'id' => '1',
        'Description' => '',
        'EndTime' => '2021-12-09 18:00:00',
        'IsAllDayEvent' => '0',
        'StartTime' => '2021-12-09 17:00:00',
        'Subject' => 'Prueba Creada desde Google',
    ];

    protected function setUp(): void
    {
        $this->eventd = new Events(
            $this->getConnectionMock(),
        );
    }

    public function testFetchMany()
    {
        $events = $this->eplanEventRepository->getEvent(1);
        $this->assertIsArray($events);
    }

    private function getConnectionMock()
    {

        $mockPDOStatement = $this->createMock(PDOStatement::class);

        $mockPDOStatement->method('fetchAll')
            ->willReturn($this->eventData);

        $mockPDO = $this->createMock(PDO::class);

        $mockPDO->method('prepare')
            ->willReturn($mockPDOStatement);

        $mock = $this->createMock(Connection::class);

        $mock->method('getPdo')
            ->willReturn($mockPDO);

        return $mock;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-01-27 08:47:42

对我来说很管用

私有函数getConnectionMock() {

代码语言:javascript
运行
复制
$mockPDOStatement = $this
   ->getMockBuilder("stdClass" /* or whatever has a fetchAll */)
   ->setMethods(array("fetchAll", "execute", "fetchColumn"))
   ->getMock();

$mockPDOStatement->method('fetchAll')
     ->willReturn($this->eventsData);

$mockPDOStatement->method('fetchColumn')
     ->willReturn(2);

$mockPDO = $this
   ->getMockBuilder("ThePDOObject")
   ->disableOriginalConstructor()
   ->setMethods(array("prepare", "lastInsertId"))
   ->getMock();

$mockPDO->method('prepare')
     ->willReturn($mockPDOStatement);

$mockPDO->method('lastInsertId')
     ->willReturn(1);

$mock = $this->createMock(EplanDBConnectionService::class);

$mock->method('getPdo')
     ->willReturn($mockPDO);
return $mock;

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70741906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档