【问题标题】:Error in a unit test mocking up the PDOStatement class with phpunit使用 phpunit 模拟 PDOStatement 类的单元测试出错
【发布时间】:2022-01-17 13:10:06
【问题描述】:

由于以下模型,我在启动单元测试时遇到错误

代码

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

错误

Error: Call to undefined method ReflectionUnionType::getName()

PHP 8.1.1 PHPUnit 8.5.22

完整示例:

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;
    }
}

【问题讨论】:

  • 首先,你用的是什么框架(好像是Laravel)?其次,你为什么要嘲笑它?看起来这个类是一个核心类,所以你不要模拟核心类......更多地解释你想要实现的模拟。
  • 感谢您的回复,我已经用一个我想做的例子更新了我的问题。

标签: php unit-testing mocking phpunit


【解决方案1】:

它对我有用

私有函数 getConnectionMock() {

$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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2015-05-05
    • 2010-11-14
    • 2011-05-19
    相关资源
    最近更新 更多