【发布时间】: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"