【问题标题】:PHPSpec: Issue stubbing out PDO::executePHPSpec:问题存根 PDO::execute
【发布时间】:2014-08-20 17:41:45
【问题描述】:

我在使用 PHPSpec 和预言存根 PDO::execute 时遇到问题,但我不断收到以下错误:

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])

这是我的规格:

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

我知道 Prophecy 不会存根任何不存在的方法,但是 PDO 已融入 PHP 并且 PDO::prepare 存根可以正常工作。感谢您提供的任何帮助。

【问题讨论】:

  • 你不能这样做 "SELECT :key FROM :collection WHERE :where" - 最重要的是,即使你可以,你使用的是 MySQL 保留字,即 key
  • 不过,你可以"SELECT $var1 FROM $var2 WHERE $var3"

标签: php pdo bdd phpspec


【解决方案1】:

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

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

基本顺序是

$stmt = $pdo->prepare('...');
$stmt->execute(...);

【讨论】:

  • 啊,可能是这样。我在错误的对象上调用它。
  • "SELECT :key FROM :collection WHERE :where" 有什么我不知道的地方吗?
  • 我在 OP 的问题下对此发表了评论,并且正在使用 key。我认为 OP 的框架可以处理这个问题。
  • key 可能是保留字,但 :key 不是。它只是一个参数。 select key from 会失败,select :key from 不会,因为 : 会产生重大影响。
  • 是的,我知道。我是只是说,根据my comment “最重要的是,即使你可以...” ;-) 仍然无法绑定表和/或列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多