seagoj
seagoj

Reputation: 259

PHPSpec: Issue stubbing out PDO::execute

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.

Upvotes: 1

Views: 530

Answers (1)

Marc B
Marc B

Reputation: 360702

If that's all just calling PDO under the hood, then it's not using PDO properly.

The core PDO object has no execute() method. That's purely for prepared statements, which is what ->prepare() returns. There IS ->exec() for immediate execution of queries, but that doesn't support prepared statements.

The basic sequence would be

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

Upvotes: 2

Related Questions