mardi 21 août 2018

How do I handle Doctrine QueryBuilder within PHPSpec?

I'm attempting to write a spec test for a class which contains a doctrine DBAL queryBuilder.

Please tell me if I'm totally missing the point of mocking, I understand that there's the phrase "don't mock tested classes" but not sure how this applies in this context.

Here's the class code:

$queryBuilder->select([
    'some',
    'field',
    'to_get'
]);
$queryBuilder->from('tableName', 'p');
$queryBuilder->innerJoin('p', 'anotherTableName', 'd', 'd.id = :dId');
$queryBuilder->where('p.id = :pId');
$queryBuilder->setParameters([
    ':dId' => 123,
    ':pId' => 456,
])
->setMaxResults(1);

$stm = $query->execute();
$result = $stm->fetch(\PDO::FETCH_ASSOC);

This is my (failing) attempt at writing the PHPSPec test for it:

$queryBuilder->select([
    'some',
    'field',
    'to_get'
])->shouldBeCalled()->willReturn($queryBuilder->getWrappedObject());
$queryBuilder->from('tableName', 'p')->shouldBeCalled()->willReturn($queryBuilder->getWrappedObject());
$queryBuilder->innerJoin('p', 'anotherTableName', 'd', 'd.id = :dId')->shouldBeCalled()->willReturn($queryBuilder->getWrappedObject());
$queryBuilder->where('p.id = :pId')->shouldBeCalled()->willReturn($queryBuilder->getWrappedObject());
$queryBuilder->setParameters([
    ':dId' => 123,
    ':pId' => 456,
])->shouldBeCalled()->willReturn($queryBuilder->getWrappedObject());

This is the error I'm receiving:

 it will handle event
      error: Object of class Prophecy\Prophecy\MethodProphecy could not be converted to string in
      <<FILEPATH OMITTED>> line 97

Anyone got any ideas?

Aucun commentaire:

Enregistrer un commentaire