1

I'm having an issue stubbing out PDO::execute using PHPSpec and prophecy, but I keep getting an error that:

29  ! it should perform PDO queries
  method `Double\PDO\P3::execute()` is not defined.

   0 vendor/phpspec/prophecy/src/Prophecy/Prophecy/MethodProphecy.php:48
     throw new Prophecy\Exception\Doubler\MethodNotFoundException("Method `Double\PDO\P3::ex"...)
   1 vendor/phpspec/prophecy/src/Prophecy/Prophecy/ObjectProphecy.php:242
     Prophecy\Prophecy\MethodProphecy->__construct([obj:Prophecy\Prophecy\ObjectProphecy], "execute", [obj:Prophecy\Argument\ArgumentsWildcard])
   2 [internal]
     Prophecy\Prophecy\ObjectProphecy->__call("execute", [array:1])
   3 [internal]
     spec\Devtools\MysqlModelSpec->it_should_perform_PDO_queries([obj:PhpSpec\Wrapper\Collaborator])

Here's my spec:

class MysqlModelSpec extends ObjectBehavior
{
    function let(\PDO $connection)
    {
        $this->beConstructedWith($connection);
    }

    function it_should_perform_PDO_queries(\PDO $connection)
    {
        $connection->prepare(
            "SELECT :key FROM :collection WHERE :where"
        )->willReturn(true);

        $connection->execute(
            array(
                'key' => 'user_name',
                'collection' => 'users',
                'where' => 'userid=1'
            )
        )->willReturn(true);

        $this->get('user_name', 'users', 'userid=1')
            ->shouldReturn(
                array('user_name' => 'seagoj')
            );
    }
}

I know Prophecy doesn't stub out any methods that don't exist, but PDO is baked into PHP and the PDO::prepare stub works fine. Thanks for any help you can give.

4

1 回答 1

2

如果这只是在后台调用 PDO,那么它没有正确使用 PDO。

核心 PDO 对象没有execute()方法。这纯粹是针对准备好的语句,这是->prepare()返回的。可以立即 ->exec()执行查询,但这不支持准备好的语句。

基本顺序是

$stmt = $pdo->prepare('...');
$stmt->execute(...);
于 2014-08-20T17:48:09.960 回答